Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c - udp send and receive on the same socket

Tags:

c

sockets

udp

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?

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;
}
like image 588
user3574984 Avatar asked May 18 '14 21:05

user3574984


People also ask

Can UDP send and receive on same socket?

TCP vs UDP Once connected, a TCP socket can only send and receive to/from the remote machine. This means that you'll need one TCP socket for each client in your application. UDP is not connection-based, you can send and receive to/from anyone at any time with the same socket.

Can the same socket be used for sending and receiving?

You can send and receive on the same socket at the same time (via multiple threads). But the send and receive may not actually occur simultaneously, since one operation may block the other from starting until it's done.

Can you use Send with UDP?

UDP sockets can be connected or unconnected - in the first case send should be used and in the second sendto . This does not affect the protocol used, i.e. it still is unreliable UDP. It only affects where the destination is taken from: from the socket or given as argument.


1 Answers

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:

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;
}
like image 62
Remy Lebeau Avatar answered Sep 30 '22 17:09

Remy Lebeau