connect to remote server using sockets unix -
in unix, want make client program connect server running on different machine. this, need enter ip address of server through keyboard , pass ip address in connect() system call of client. tried reading string, , passing it.but didnt work. there specific way pass ip address?
assuming ipv4, function you're looking inet_addr
, converts string representation of ipv4 address numerical value can passed various socket functions:
int get_connection(const char *ip, int port) { int sock; struct sockaddr_in sin; bzero(&sin,sizeof(sin)); sin.sin_family = af_inet; sin.sin_addr.s_addr = inet_addr(ip); sin.sin_port = htons(port); if ((sock=socket(af_inet,sock_stream,0))==-1) { perror("error creating socket"); return -1; } if (connect(sock,(struct sockaddr *)&sin,sizeof(sin))==-1) { perror("couldn't connect"); close(sock); return -1; } return sock; }
Comments
Post a Comment