C语言 c - udp 在同一个套接字上发送和接收

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23726931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:05:52  来源:igfitidea点击:

c - udp send and receive on the same socket

csocketsudp

提问by user3574984

I would like to send and receive packets on the same socket, is it possible or I have to create two socket, one to send and one to receive? If yes, can you give me an example?

我想在同一个套接字上发送和接收数据包,是否可能或者我必须创建两个套接字,一个发送一个接收?如果是的话,你能给我举个例子吗?

Another question: how can I get the source ip from a received packet?

另一个问题:如何从接收到的数据包中获取源 ip?

EDIT (code example):

编辑(代码示例):

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;
}

回答by Remy Lebeau

Yes, you can use the same socket for sending and receiving. recvfrom()tells you the IP/port of the sender. Simply sendto()that IP/port using the same socket that you use with recvfrom(), eg:

是的,您可以使用相同的套接字进行发送和接收。recvfrom()告诉您发件人的 IP/端口。简单地sendto()使用与您一起使用的相同套接字的 IP/端口recvfrom(),例如:

int main(void) {
    struct sockaddr_in si_me, si_other;
    int s, i, blen, slen = sizeof(si_other);
    char buf[BUFLEN];

    s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (s == -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, (struct sockaddr*) &si_me, sizeof(si_me))==-1)
        die("bind");

    int blen = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &si_other, &slen);
    if (blen == -1)
       diep("recvfrom()");

    printf("Data: %.*s \nReceived from %s:%d\n\n", blen, buf, inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));

    //send answer back to the client
    if (sendto(s, buf, blen, 0, (struct sockaddr*) &si_other, slen) == -1)
        diep("sendto()");

    close(s);
    return 0;
}