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?)
char a="12345";
- error. Orchar *a
, orchar a[]
) - alexlz