6
동일한 소켓에서 패킷을 보내고 받고 싶습니다. 가능하거나 두 개의 소켓, 하나를 보내고받을 수 있습니까? 그렇다면 예를 들어 주시겠습니까?c - udp 동일한 소켓에서 보내고 받으십시오
또 다른 질문 : 어떻게 수신 된 패킷에서 소스 IP를 얻을 수 있습니까?
EDIT (코드 예제) :
는int main(void) {
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
die("socket");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(1234);
si_me.sin_addr.s_addr = htonl(192.168.1.1);
if (bind(s, &si_me, sizeof(si_me))==-1)
die("bind");
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
diep("recvfrom()");
printf("Data: %s \nReceived from %s:%d\n\n", buf, inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
//now I want the server to answer back to the client
close(s);
return 0;
}
당신이 예를 게시하시기 바랍니다 수 있습니까? – user3574984
무엇? 송수신? 또는 소스 IP/포트를 가져 오는 중입니까? 주위를 둘러 보면 온라인에서 두 가지 예가 많이 있습니다. –
서버는 동일한 udp 소켓에서 패킷을 받음 => 답장을받습니다 ... 수신 방법과 보내는 방법을 알고 있지만, 같은 소켓으로 어떻게해야 하는지를 알고 있습니다! – user3574984