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:
// 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
Post a Comment