udp 소켓을 통해 16 진수 값의 배열을 보내려고하고 있지만 0x22 바이트 만받을 수는 없습니다. 뭐가 문제 야?? 미리 감사드립니다 !!!UDP 소켓을 통해 16 진수 값 보내기
PD : 16 진수 값으로 배열을 인쇄하려면 어떻게해야합니까?
/* UDP client in the internet domain */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <time.h>
void error(const char *);
int main()
{
int sock, n;
unsigned int length;
struct sockaddr_in server;
struct hostent *hp;
char buffer[13]={0x22,0x00,0x0d,0xf4,0x35,0x31,0x02,0x71,0xa7,0x31,0x88,0x80,0x00};
hp = gethostbyname("127.0.0.1");
if (hp==0) error("Unknown host");
sock= socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) error("socket");
server.sin_family = AF_INET;
bcopy((char *)hp->h_addr,
(char *)&server.sin_addr,
hp->h_length);
server.sin_port = htons(atoi("6666"));
length=sizeof(struct sockaddr_in);
while (1) {
n=sendto(sock,buffer,strlen(buffer),0,(const struct sockaddr *)&server,length);
if (n < 0) error("Sendto");
printf("Sending Packet...\n");
sleep(1);
}
close(sock);
return 0;
}
void error(const char *msg)
{
perror(msg);
exit(0);
}
괄호는 'sizeof (typename)'에만 필요합니다. 여기에서 생략 할 수 있습니다 :'sizeof buffer' – wildplasser
@wildplasser : 하나의 문자를 덜 입력하는 것 외에 무엇을 얻습니까? –
1) 그것은 의도를 분명하게합니다. 2) 'typedef struct {...}'와 같은 오류를 피합니다. 아이템 * 아이템; item = malloc (sizeof (* Item));'(지난 주 실제로 본 적이 있는데, 괄호를 사용하지 않으면 구문 오류가 발생합니다) – wildplasser