Failed to connect Udp socket

I am trying to connect a UDP server running Node.js using

int socketDs = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

struct sockaddr_in socket;
memset(&socket, 0, sizeof(socket));
socket.sin_family = AF_INET;
socket.sin_addr.s_addr = inet_addr("SERVER.IP");
socket.sin_port = htons(PORT);

long r = bind(socketDs, (struct sockaddr *)&socket, sizeof(socket));
NSLog(@"Sockect bind: %ld   %s", r, strerror(errno));

      

Could not bind to it, returns with Can't assign requested address

. However, sendto works fine without binding to it.

What could be the problem. Also I am not getting a "private" event on Node.js

Here is my server code

var dgram = require("dgram");
var server = dgram.createSocket("udp4");

var clients = new Array();

server.on("listening", function () {
    var address = server.address();
    console.log("UDP Server listening on " + address.address + ":" + address.port);
});

server.on("message", function (message, remote) {
    console.log(remote.address + ':' + remote.port +' - ' + message);
    }
});

server.on("close") {
    console.log("close");
});

server.on("error") {
    console.log("error");
});

server.bind(PORT);

      

+3


source to share


1 answer


The first part of the code you talked about relates to the client. And you are trying to bind to SERVER_IP. This will obviously lead to the error bind

you are seeing. You are trying to bind a client to an IP address that belongs to an external system.

And sendto

great, because there is an implicit binding on the client side.



Clients clearly don't need it bind

. You can end this part.

0


source







All Articles