Sending broadcast server -> client and sending back client -> server UDP C Socket

I want to ask if there is a possibility and if there is how do I send broadcast from my UDP server to client and after that send back response from client to server.

I need to implement an application where server sends broadcast on a subnetwork and clients are supposed to receive that message somehow and in that way would get server address so that they could send some messages back to server.

I created a socket with specified server port say 2000 and set setsockopt to SO_BROADCAST and sent some message with sendto but my client hangs on recvfrom forever. Seems like it does not receive anything from server's broadcasting. How to fix this?

SERVER:

    struct sockaddr_in addr;
    int socketfd,t=1;

    socketfd = socket(PF_INET,SOCK_DGRAM,0);
    memset(&addr, 0, sizeof(struct sockaddr_in));

    addr.sin_family = AF_INET;
    addr.sin_port = htons("2000");
    addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);

    if (setsockopt(socketfd, SOL_SOCKET, SO_BROADCAST ,&t, sizeof(t)))
         ERR("setsockopt");
    if(bind(socketfd,(struct sockaddr*) &addr,sizeof(addr)) < 0)
         ERR("bind");

    char *buf = "HelloFromServer!";

    if(sendto(socketfd, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr)) <= 0)
    {
        perror("something went wrong..");

        return EXIT_FAILURE;
    }

CLIENT:

    struct sockaddr_in addr;
    int socketfd,t=1;

    socketfd = socket(PF_INET,SOCK_DGRAM,0);
    memset(&addr, 0, sizeof(struct sockaddr_in));

    addr.sin_family = AF_INET;
    addr.sin_port = htons("2000");
    addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);

    if (setsockopt(socketfd, SOL_SOCKET, SO_BROADCAST ,&t, sizeof(t)))
         ERR("setsockopt");

    char data[12];
    struct sockaddr_in serv_addr;
    socklen_t size=sizeof(serv_addr);

    recvfrom(fd, (char *)data, sizeof(char[12]), 0, &serv_addr, &size);

And my client hangs on this recvfrom... and therefore I cannot get serv_addr to be stored so that I could send some message back to the server

Upvotes: 0

Views: 2264

Answers (2)

Armali
Armali

Reputation: 19375

The error in both server and client is in

    addr.sin_port = htons("2000");

htons() takes an integer, not a string. Write

    addr.sin_port = htons(2000);

instead.

Upvotes: 1

Brendon Boldt
Brendon Boldt

Reputation: 235

Clients are supposed to receive that message somehow and in that way would get server address so that they could send some messages back to server.

I believe there is a problem here: for the clients to receive the message broadcasted by the server, they must first have the server's address. The only time that a socket can receive data/communications from an unknown address is a server using listen() on a port which has port forwarding set up (courtesy of the router). Thus, unless the clients already have the server address, it seems the client receiving the data would be impossible, hence the hanging on recvfrom().

Upvotes: 1

Related Questions