Thanks to everyone who helped !!! Understood everything and understood a lot of new things. Thanks also for the tips helped a lot. Thank you all again !!!

Here is the source:

**КЛИЕНТ**///////////////////////////////////////////////////////////////////////////// #include <cstdlib> #include <iostream> #include <winsock.h> using namespace std; int main(){ setlocale(LC_ALL,"rus"); char url[100]; int port; char my_name[30] = "ASA"; std::cout << "Url: "; cin >> url; cout << endl; std::cout << "Port: "; cin >> port; cout << endl; WSADATA WsaData; if (int err = WSAStartup (MAKEWORD(2, 0), &WsaData) != 0) { std::cout << "Socket not Loaded!\n"; }else{ std::cout << "Socket Loaded \n"; } int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1){ std::cout << "Error! Socket no created.\n" ; }else{ std::cout << "Socket Create.\n"; } sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_port=htons(port); addr.sin_addr.s_addr=inet_addr(url); int locate; locate = connect(sock, (sockaddr *)&addr, sizeof(addr)); if (locate < 0){ std::cout << "Fatal Error!\n"; system("pause"); }else{ char cut[10000]; char get[10000]; send(sock, my_name, 30, 0); recv(sock, get, 10000, 0); cout << get << endl; std::cout<<"Enter get: "; std::cin>>cut; send(sock, cut, 10000, 0); system("pause"); } } ///////////////////////////////////////////////////////////////////////// 

SERVER ///////////////////////////////////////////////// ////////////////////////////

  #include <cstdlib> #include <iostream> #include <winsock.h> 

using namespace std;

int main() { char client_name[30]; char sv_name[30];
WSADATA WsaData; if (int err = WSAStartup(MAKEWORD(2, 0), &WsaData) != 0) { std::cout << "Socket not Loaded!n"; } else { std::cout << "Socket Loaded n"; } gethostname(sv_name, 30); int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* исправлено sock*/ if (sock == -1) { /* исправлено sock */ std::cout << "Error! Socket no created.\n"; } else { std::cout << "Socket Create.\n"; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8080); /* исправлено */ addr.sin_addr.s_addr = htonl(INADDR_ANY); /* исправлено */ int bindet = bind(sock, (sockaddr *)&addr, sizeof(addr)); if (bindet == -1){ std::cout << "Binding Error!n"; system("pause"); }else { int listening = listen (sock, 100); std::cout << "Server Name: " << sv_name << endl << "Wait for connecting ...\n"; int acc = accept(sock, (sockaddr*)&addr, 0); recv(acc, client_name, 30, 0); cout <<"Connected: " << client_name << "\n"; if (client_name == "ASA"){ char urls[100]; send(acc, sv_name, strlen(sv_name) + 1, 0); int rec = recv(acc, urls, 100, 0); if (rec > 0){ int i; while(i < 100){ send(acc, urls, 30, 0); ++i; } system("pause"); } }} }

 int main() { char client_name[30]; char sv_name[30]; 
WSADATA WsaData; if (int err = WSAStartup(MAKEWORD(2, 0), &WsaData) != 0) { std::cout << "Socket not Loaded!n"; } else { std::cout << "Socket Loaded n"; } gethostname(sv_name, 30); int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* исправлено sock*/ if (sock == -1) { /* исправлено sock */ std::cout << "Error! Socket no created.\n"; } else { std::cout << "Socket Create.\n"; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8080); /* исправлено */ addr.sin_addr.s_addr = htonl(INADDR_ANY); /* исправлено */ int bindet = bind(sock, (sockaddr *)&addr, sizeof(addr)); if (bindet == -1){ std::cout << "Binding Error!n"; system("pause"); }else { int listening = listen (sock, 100); std::cout << "Server Name: " << sv_name << endl << "Wait for connecting ...\n"; int acc = accept(sock, (sockaddr*)&addr, 0); recv(acc, client_name, 30, 0); cout <<"Connected: " << client_name << "\n"; if (client_name == "ASA"){ char urls[100]; send(acc, sv_name, strlen(sv_name) + 1, 0); int rec = recv(acc, urls, 100, 0); if (rec > 0){ int i; while(i < 100){ send(acc, urls, 30, 0); ++i; } system("pause"); } }} }

int main() { char client_name[30]; char sv_name[30];
WSADATA WsaData; if (int err = WSAStartup(MAKEWORD(2, 0), &WsaData) != 0) { std::cout << "Socket not Loaded!n"; } else { std::cout << "Socket Loaded n"; } gethostname(sv_name, 30); int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* исправлено sock*/ if (sock == -1) { /* исправлено sock */ std::cout << "Error! Socket no created.\n"; } else { std::cout << "Socket Create.\n"; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8080); /* исправлено */ addr.sin_addr.s_addr = htonl(INADDR_ANY); /* исправлено */ int bindet = bind(sock, (sockaddr *)&addr, sizeof(addr)); if (bindet == -1){ std::cout << "Binding Error!n"; system("pause"); }else { int listening = listen (sock, 100); std::cout << "Server Name: " << sv_name << endl << "Wait for connecting ...\n"; int acc = accept(sock, (sockaddr*)&addr, 0); recv(acc, client_name, 30, 0); cout <<"Connected: " << client_name << "\n"; if (client_name == "ASA"){ char urls[100]; send(acc, sv_name, strlen(sv_name) + 1, 0); int rec = recv(acc, urls, 100, 0); if (rec > 0){ int i; while(i < 100){ send(acc, urls, 30, 0); ++i; } system("pause"); } }} }

 int main() { char client_name[30]; char sv_name[30]; 
WSADATA WsaData; if (int err = WSAStartup(MAKEWORD(2, 0), &WsaData) != 0) { std::cout << "Socket not Loaded!n"; } else { std::cout << "Socket Loaded n"; } gethostname(sv_name, 30); int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* исправлено sock*/ if (sock == -1) { /* исправлено sock */ std::cout << "Error! Socket no created.\n"; } else { std::cout << "Socket Create.\n"; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8080); /* исправлено */ addr.sin_addr.s_addr = htonl(INADDR_ANY); /* исправлено */ int bindet = bind(sock, (sockaddr *)&addr, sizeof(addr)); if (bindet == -1){ std::cout << "Binding Error!n"; system("pause"); }else { int listening = listen (sock, 100); std::cout << "Server Name: " << sv_name << endl << "Wait for connecting ...\n"; int acc = accept(sock, (sockaddr*)&addr, 0); recv(acc, client_name, 30, 0); cout <<"Connected: " << client_name << "\n"; if (client_name == "ASA"){ char urls[100]; send(acc, sv_name, strlen(sv_name) + 1, 0); int rec = recv(acc, urls, 100, 0); if (rec > 0){ int i; while(i < 100){ send(acc, urls, 30, 0); ++i; } system("pause"); } }} }
  • one
    @Denis Fazil, please correct the code formatting. - Dex
  • one
    Analyze the error code in errno (print error text - perror). And yet, why do you need privileged ports, i.e. with a number less than 1024? - alexlz
  • socket = socket (PF_INET, SOCK_STREAM, 0); The AF_INET and PF_INET constants are equal, but ideologically it is more correct to write PF_INET (although it naturally will not affect the result). You just missed the function call. Ochepyatka - alexlz
  • And you do not see the differences. In one case, it is a call to the socket procedure with three parameters, in the other, the assignment of the variable socket (AF_INET, ..). What value is assigned: 0 or AF_INET - I do not remember, see the description of the operation "," (in C / C ++ this is an operation). - alexlz
  • @Denis Fazil, judging by the corrections, you haven’t read anything about sockets since you were given the answers. INADDR_ANY in connect () cannot naturally be used. For connect (), you need to register the server address (IP). About INADDR_ANY it was said that it can be used in bind (). Once again, I advise you to write one client program and one server. Client: socket () connect () send () Server: socket () bind () listen () accept () recv () - avp

2 answers 2

  1. bind() for the client socket you do not need here !!!

The local address for this socket will be automatically assigned to connect() . If you still want to assign a specific address to the client, then in the addr.sin_addr.s_addr field you need to place one of the local IP (or the constant INADDR_ANY ).

  1. In memcpy() you must copy the server's IP into the addr.sin_addr.s_addr field, and not into sin_family .
  • Well, it is not clear there, he has both client and server parts in one program (which he will have to somehow carry somewhere). And about the addresses s_addr and s_addr.sin_family - ugly, but the address is the same. - alexlz
  • I look at the code (in question) and I do not see accept () (and here is connect () is). I conclude that this is the client part, and bind () here from the series " I heard a bell, but I don’t know where it is." - avp
  • Yes, I already wrote to him in reply. But all sorts of gethostname, gethostbyname ... Maybe I'm wrong, but it’s not very clear what he wants. (probably some kind of academic work - for yourself or for prep.) - alexlz
  • Well, one more tip: deal with writing the server and client separately, but for now write one, use netcat ( ru.wikipedia.org/wiki/Netcat ) for debugging - alexlz
  • Read books. accept — accepts the connection by the server (after establishing a tcp connection with the port waiting for the connection after calling the listen). accept returns a socket (other) through which the exchange with the client is already in progress (no exchange via the server socket). Those. for the server, the socket-> bind-> listen-> accept chain (there may be a lot of them). For client socket-> connect. Well, the filling of different structures. - alexlz

To begin with: You forgot to call the socket function in the string socket = (AF_INET,SOCK_STREAM,0); (Well, my comment remains relevant) Next, HostName is read from cin and gethostname is filled. What for? In general, it is strange that you have in one program (and one thread / thread) both a server and a client. Well, listen and accept, apparently will be later.

Additive:

 #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/socket.h> #include <iostream> #include <string.h> #include <stdio.h> int main() { int sock; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { perror("socket"); std::cout << "Error! Socket no created.\n"; } else { std::cout << "Socket Create.\n"; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(4000); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); int connection = connect(sock, (sockaddr *)&addr, sizeof(addr)); if (connection < 0) { std::cout << "Fatal Error!\n"; } else { char message[1000]; std::cout << "Enter get: "; std::cin >> message; send(sock, message, strlen(message), 0); } } 

Corrected your creation. But I have no definition of SOCKET, replaced with int. The name socket is used for the system call, so the variable is called sock. The address 127.0.0.1 transforms the function inet_addr. Added perror call on socket creation error. Changed the port number to 4000 (unprivileged). In the send, the length of the message is calculated using strlen. Yes, and sockets must be closed. Here it closes when you exit the program, and in general it is desirable to do this explicitly.

Option 01/26/12:

 #include <iostream> #include <winsock2.h> #include <string.h> int main() { setlocale(LC_ALL,"rus"); WSADATA WsaData; if (int err = WSAStartup(MAKEWORD(2, 0), &WsaData) != 0) { std::cout << "Socket not Loaded!\n"; } else { std::cout << "Socket Loaded \n"; } SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); /* исправлено sock*/ if (sock == -1) { /* исправлено sock */ std::cout << "Error! Socket no created.\n"; } else { std::cout << "Socket Create.\n"; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(4000); /* исправлено */ addr.sin_addr.s_addr = inet_addr("127.0.0.1"); /* исправлено */ int connection; connection = connect(sock, (sockaddr *)&addr, sizeof(addr)); /* исправлено sock */ if (connection < 0) { std::cout << "Fatal Error!\n"; //system("pause"); /* а так лучше не делать. Вызывать cmd.exe... Ну введите что-нибудь с cin */ } else { char message[1000]; std::cout << "Enter get: "; std::cin >> message; send(sock, message, strlen(message), 0); /* исправлено sock, strlen -- иначе на сервер приходит куча мусора */ } } 

Translated by i586-mingw32msvc-g ++ (GCC) 4.4.2, compiled with both libwsock32.a and libws2_32.a (I saw somewhere that ws2_32 is preferable). Checked on nc -l 4000 in another window.

  • In Windows, you should definitely start with WSAStartup (), and perror () for socket functions prints nonsense in it (they don’t change errno in Windows). There is some function that returns a code for errors with a socket, but I did not have enough patience to convert it to text (therefore I don’t remember her name). - avp pm
  • alexlz, thanks for the amendment BUT! Checked the code when starting the program the socket is loaded for about 2 seconds and still Error !!!!! - pro
  • What can I say. Show the program text (in its entirety, with #include 's). I'll try to run under wine (WinXP doesn’t load at all). - alexlz 2:22 pm
  • Completed the answer. - alexlz
  • one
    I had a friend in my student days who was 7 in the morning late and 7 in the morning in the morning. I could not understand when it is light in winter. - alexlz