IP address in TCP sockets -



IP address in TCP sockets -

i have root node(server) connected many other nodes(clients) through tcp sockets. want send info server client, info different each node , depends on ip address of node.

thus should have ip address of each node connected server. how can have information?

as long have access list of file descriptors connected tcp sockets, easy retrieve addresses of remote hosts. key getpeername() scheme call, allows find out address of remote end of socket. sample c code:

class="lang-c prettyprint-override">// ugly, simpler alternative union { struct sockaddr sa; struct sockaddr_in sa4; struct sockaddr_storage sas; } address; socklen_t size = sizeof(address); // assume file descriptor in var 'fd': if (getpeername(fd, &address.sa, &size) < 0) { // deal error here... } if (address.sa.family == af_inet) { // ip address in address.sa4.sin_addr, port in address.sa4.sin_port } else { // other kind of socket... }

sockets tcp network-programming ip-address

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -