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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -