UDP 소켓을 사용하여 서버에서 클라이언트로 메시지를 보내는 것을 실험하고 있지만 클라이언트에 메시지가 수신되지 않습니다. 모든 의견은 크게 감사하겠습니다!서버에서 클라이언트로 UDP 메시지 보내기
EDIT : 어쩌면 메시지가 성공적으로 전송 된 것으로보아야하지만 클라이언트를 실행할 때 데이터를 기다리는 데 방해가 될 수 있습니다 ... 포인터가 왜 이런 일이 일어 났는지 평가할 수 있습니다!
UDP 서버에
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#define SERVER "127.0.0.1"
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to listen for incoming data
void die(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_other;
int s, i, slen = sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];
//create a UDP socket
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
// zero out the structure
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(SERVER , &si_other.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
//bind socket to port
if(bind(s , (struct sockaddr*)&si_other, sizeof(si_other)) == -1)
{
die("bind");
}
else
{
printf ("Success!\n");
}
while(1)
{
printf("Enter message : ");
gets(message);
//send the message
if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
{
die("sendto()");
}
else
{
printf ("Success!\n");
}
}
close(s);
return 0;
}
UDP의 CLIENT
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to send data
void die(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other), recv_len;
char buf[BUFLEN];
char message[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
// zero out the structure
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
while(1)
{
printf("Waiting for data...");
fflush(stdout);
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)
{
die("recvfrom()");
}
//print details of the client/peer and the data received
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Data: %s\n" , buf);
}
close(s);
return 0;
}
'int s, i, slen = sizeof (si_other), recv_len;': 나는 무엇인가? 시도 :'socklen_t slen;'과'slen = sizeof si_other' – jhscheer
나는 그것을 작동하게 만들었습니다 ... 나는 클라이언트가 아닌 클라이언트로부터 bind()를 호출해야합니다 ... – user1816546
그 대답으로 쓰십시오. (귀하는 귀하의 질문에 답변 할 수 있습니다). – JeremyP