I want other people in different local networks to connect to my server. That is, I first have to go to the router, create a virtual server with the IP address of my computer in DHCP (192.168.1.101), with port 1111, with TCP and UDP protocols. Then, on the server side (Server.cpp) I have to set the parameter 127.0.0.1 and port 1111 in inet_addr (), and on the client side I have to specify the white IP address in the inet_addr () (that can be found in https: // 2ip. ru / ) and port 1111. As a result, I start the server and it starts normally, but I launch the client and after 10 seconds it says that it cannot connect to the server. I can not understand what I'm doing wrong ...

Server.cpp

#include <iostream> #include <WinSock2.h> #include <string> #pragma comment(lib, "ws2_32.lib") #pragma warning (disable: 4996) using namespace std; SOCKET users[10]; string users_name[10]; int user_num = 0; void Users_Msgs(int index) { char msg1[256]; while (true) { recv(users[index], msg1, sizeof(msg1), 0); for (int j = 0; j < user_num; j++) { if (j == index) continue; send(users[j], users_name[index].c_str(), users_name[index].length() + 1, 0); Sleep(250); send(users[j], msg1, sizeof(msg1), 0); } } } int main() { setlocale(LC_ALL, "ru"); WSAData wsaData; WORD dll_ver = MAKEWORD(2, 1); if (WSAStartup(dll_ver, &wsaData) != 0) { cout << "Error\n"; exit(1); } SOCKADDR_IN addr; int size_of_addr = sizeof(addr); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_port = htons(1111); addr.sin_family = AF_INET; SOCKET Listen = socket(AF_INET, SOCK_STREAM, 0); bind(Listen, (SOCKADDR*)&addr, sizeof(addr)); listen(Listen, SOMAXCONN); SOCKET newConnection; for (int i = 0; i < 10; i++) { newConnection = accept(Listen, (SOCKADDR*)&addr, &size_of_addr); if (newConnection == 0) cout << "Error connecting to client\n"; else { cout << "Connection to client " << i << " succesfully\n"; users[i] = newConnection; user_num++; char his_name[256]; recv(newConnection, his_name, sizeof(his_name), 0); users_name[i] = his_name; cout << "His name is " << his_name << "\n"; CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Users_Msgs, (LPVOID)(i), 0, 0); } } return 0; } 

Client.cpp

 #include <iostream> #include <WinSock2.h> #include <string> #include <windows.h> #pragma comment(lib, "ws2_32.lib") #pragma warning (disable: 4996) using namespace std; SOCKET Connection; void Msg_Rec() { char msg[256], name[256]; while(true) { recv(Connection, name, sizeof(name), 0); recv(Connection, msg, sizeof(msg), 0); cout << "\n" << name << ": " << msg << "\n"; } } int main() { setlocale(LC_ALL, "ru"); WSAData wsaData; WORD dll_ver = MAKEWORD(2, 1); if (WSAStartup(dll_ver, &wsaData) != 0) { cout << "Error\n"; exit(1); } SOCKADDR_IN addr; int size_of_addr = sizeof(addr); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_port = htons(1111); addr.sin_family = AF_INET; Connection = socket(AF_INET, SOCK_STREAM, 0); if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) { cout << "Error connecting to server\n"; system("pause"); } cout << "Connection to server succesfully\n"; cout << "Enter your name: "; char your_name[256]; cin >> your_name; send(Connection, your_name, sizeof(your_name), 0); CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Msg_Rec, 0, 0, 0); while(true) { char msg[256]; cout << "Enter your message: "; cin.getline(msg, sizeof(msg)) >> msg; send(Connection, msg, sizeof(msg), 0); Sleep(100); } system("pause"); } 

Configured virtual server on the router:

https://i.stack.imgur.com/sfR21.jpg


The issue is resolved (in the comments)!

  • The address 127.0.0.1 is used only within the local machine. If you understand the server on it, you cannot connect to it from outside. It is necessary to do a bind on 0.0.0.0 to expect connections on all the ip that is on the machine, or to indicate clearly the ip on which calls from outside will actually come. In your case, apparently 192.168.1.101 - Mike
  • That is, on the server side inet_addr (192.168.1.101), and on the client inet_addr (white ip)? - Steg_Brind
  • Yes, yes. Although on the server more often they still put 0.0.0.0 in order not to think about what ip is worth - Mike
  • Everything exactly does not work imgur.com/a/y0wO88L - Steg_Brind
  • one
    Yes. The realist will only come from somewhere outside (with a router). Within the machine, you can connect only to addresses that are picked up on the machine itself on any interface. - Mike

0