Good evening!

There was another stupid problem that I just can’t solve ... There are a lot of examples on the network with the input of a server port like this: servAddr.sin_port = htons (12345); And how to push there the port entered from the keyboard? How to do this:

char a="12345"; servAddr.sin_port=htons(a); 
  • Is this a question how to convert char [] to short int? atoi will not work? (By the way, char a="12345"; - error. Or char *a , or char a[] ) - alexlz

2 answers 2

Only the port is very simple

 .... int port; cin >> port; servAddr.sin_port=htons(port); .... 

In general, I use this function

 #include <netdb.h> #include <arpa/inet.h> #include <string.h> // returns 1 OK, 0 ERR int make_ipaddr (char *host, int port, struct sockaddr_in *a) { struct hostent *phe; a->sin_family = AF_INET; a->sin_port = htons(port); a->sin_addr.s_addr = INADDR_ANY; if (host && host[0]) { if (phe = gethostbyname(host)) memcpy (&a->sin_addr, phe->h_addr, phe->h_length); else if ((a->sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) return 0; } return 1; } 

Here, the host can be either a name (such as www.mail.ru or localhost) or an IP address (such as 127.0.0.1). How to read a line from the keyboard, I think it is clear.

Call for example so

 .... // запуск потоков данных клиента pid_t runtcli (int n, char *host, int port) { pid_t p = fork(); if (p) return p; struct sockaddr_in sadr; if (!make_ipaddr(host,port,&sadr)) fatal("Cli make addr"); .... 

(And when, in the end, will this “editor” take the tabs on copy-paste correctly?)

  • Thank. In order not to litter the forum with stupid questions, I will ask you ... There is a SOCKET usersList [maxusernum]; How to pull out the client's ip from the server and convert it to char? - Alerr
  • As far as I understand in usersList [] open TCP descriptors. Then you can write char * getchars_sock_addr (int sockfd) {struct sockaddr addr; socklen_t addrlen = sizeof (addr); if (getpeername (sockfd, & addr, & addrlen)) return 0; // errno is set return inet_ntoa (((struct sockaddr_in *) (& addr)) -> sin_addr); } Just keep in mind that the address is returned to the static buffer in inet_ntoa () and the data in it will be ground by another call to inet_ntoa (). Accordingly, either the second argument and copy there, or return strdup (inet_ntoa (...)); - avp
  • Places in the comments, as always a little. Call, for example, char * ipstr = getchars_sock_addr (usersList [i]); cout << "Client IP:" << ipstr? ipstr: strerror (errno) << '\ n'; if you made return (strdup (..., then free (ipstr)); // in Linux you can not check for NULL Yes, for getchars_sock_addr () #include <arpa / inet.h> and for strdup () #include < string.h> By the way, I just noticed the winsock label. Examples are from * nix, but I hope it will work in Windows too - avp
 #include <iostream> using namespace std; int main(){ char c[6]; cin >> c; .... = htons(с); return 0; } 

probably so

  • No, not so ... (error C2664: 'htons': cannot convert parameter 1 from 'char [10]' to 'u_short' - Alerr
  • then char c [6]; replace with u_short c; - perfect