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

浪尽此生 提交于 2019-12-12 01:19:30

问题


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


回答1:


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().




回答2:


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.



来源:https://stackoverflow.com/questions/24071415/sending-broadcast-server-client-and-sending-back-client-server-udp-c-socke

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!