Question

I know that this is obviously elementary question and I know that there are many tutorials and ready-to-go examples but I must missing something. I am trying to send for example text (char *) via UDP socket to other machine in local network. So far I tried some tutorials like http://gafferongames.com/networking-for-game-programmers/sending-and-receiving-packets/ and so on but I always get error in bind() function with errno "Cannot assign requested address".

I just have some data in char array and I want to push them via network to another host. Could someone please point me to the right direction? Do I need socket server or client? Do I need to bind the socket to some interface?

This is my playground:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>

int handle;

int init_socket()
{
        handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

        if (handle <= 0)
        {
                printf("failed to create socket\n");
                return 1;
        }

        printf("sockets successfully initialized\n");

        return 0;
}

int main ()
{
        unsigned short port = 30000;
    char * data = "hovno";

    init_socket();

        struct sockaddr_in address;

    memset((char *) &address, 0, sizeof(address));

        address.sin_family = AF_INET;
        address.sin_addr.s_addr = inet_addr("192.168.11.129"); // this is address of host which I want to send the socket
        address.sin_port = htons(port);


    printf("handle: %d\n", handle); // prints number greater than 0 so I assume handle is initialized properly

        if (bind(handle, (const struct sockaddr*) &address, sizeof(struct sockaddr_in)) < 0)
        {
                printf("failed to bind socket (%s)\n", strerror(errno)); // Cannot assign requested address
                return 1;
        }

        int nonBlocking = 1;
        if (fcntl(handle, F_SETFL, O_NONBLOCK, nonBlocking) == -1)
        {
                printf("failed to set non-blocking\n");
                return 2;
        }

        int sent_bytes = sendto(handle, data, strlen(data), 0, (const struct sockaddr*) &address, sizeof(struct sockaddr_in));

        if (sent_bytes != strlen(data))
        {
                printf("failed to send packet\n");
                return 3;
        }

    return 0;
}
Was it helpful?

Solution

bind is called for the local address (one you intend to recv packets to). The IP address must be a local IP address of the machine, or (most frequently) INADDR_ANY.

Normally you don't have to use bind on the client side at all. The system will pick a suitable free port for you automatically.

To specify the remote address for a UDP socket, use sendto, not send.

If you search Google for udp client c code, one of the first results is this one. You can see that the networking part is basically just two calls, socket and sendto.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top