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)!