Answer the question
In order to leave comments, you need to log in
Winsocket won't connect to remote IP. Why?
Good day, experts!
Wrote two programs: server and client. The server opens a socket and listens on the port that is set after startup, the client connects to the IP and port, which are also prescribed after startup. I start my server, open some port, connect from the client to 127.0.0.1 and to the specified port; send messages; profit. If my friend starts a server, and I try to connect from the client to his IP and the port that he opened, the program waits for 15 seconds, then gives an error 10060 (connection timed out); if I start a server, open a port, and try to connect to my ip and port (NOT 127.0.0.1), the same thing happens.
I enclose the source codes of both programs:
Server
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#pragma comment (lib,"Ws2_32.lib")
using namespace std;
#define DEFAULT_BUFLEN 512
int main()
{
WSADATA wsaData;
int result;
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(result != 0)
{
cout << "Error WSAStartup: " << result << endl;
return -1;
}
struct addrinfo *aiResult = NULL, *ptr = NULL, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
char* port;
cout << "Set port: ";
cin >> port;
result = getaddrinfo(NULL, port, &hints, &aiResult);
if (result != 0)
{
cout << "Error in getaddrinfo(): " << WSAGetLastError() << endl;
WSACleanup();
return -1;
}
SOCKET listenSocket = INVALID_SOCKET;
listenSocket = socket(aiResult->ai_family, aiResult->ai_socktype, aiResult->ai_protocol);
if(listenSocket == INVALID_SOCKET)
{
std::cout << "Error in socket(): " << WSAGetLastError();
freeaddrinfo(aiResult);
WSACleanup();
return 1;
}
result = bind(listenSocket, aiResult->ai_addr, aiResult->ai_addrlen);
if(result == SOCKET_ERROR)
{
cout << "Bind failed with error: " << WSAGetLastError() << endl;
freeaddrinfo(aiResult);
closesocket(listenSocket);
WSACleanup();
return -1;
}
freeaddrinfo(aiResult);
if(listen(listenSocket, SOMAXCONN) == SOCKET_ERROR)
{
cout << "Listen failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return -1;
}
SOCKET ClientSocket = INVALID_SOCKET;
ClientSocket = accept(listenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET)
{
cout << "Accept failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return -1;
}
while(true)
{
char recvbuf[DEFAULT_BUFLEN] = "";
int recvbuflen = DEFAULT_BUFLEN;
result = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (result > 0)
{
// cout << "Accepted " << result << " bytes" << endl;
cout << "Received: " << recvbuf << endl;
}
}
system("pause");
return 0;
}
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#pragma comment (lib,"Ws2_32.lib")
using namespace std;
int main()
{
WSADATA wsaData;
int result;
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(result != 0)
{
cout << "Error WSAStartup: " << result << endl;
return -1;
}
SOCKET clientSocket = INVALID_SOCKET;
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientSocket == INVALID_SOCKET)
{
cout << "Error in socket(): " << WSAGetLastError() << endl;
WSACleanup();
return -1;
}
string ip;
int port;
cout << "Connect to:" << endl;
cout << "IP: ";
cin >> ip;
cout << "Port: ";
cin >> port;
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(ip.c_str());
clientService.sin_port = htons(port);
result = connect(clientSocket, reinterpret_cast<SOCKADDR*>(&clientService), sizeof(clientService));
if(result != 0)
{
cout << "Error in connect(): " << WSAGetLastError() << endl;
WSACleanup();
return -1;
}
while(true)
{
string data = "";
cout << "Enter a string: ";
cin >> data;
result = send(clientSocket, data.c_str(), data.size(), 0);
if(result < 0)
{
cout << "Error in send(): " << WSAGetLastError() << endl;
WSACleanup();
return -1;
}
}
closesocket(clientSocket);
WSACleanup();
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
hmm...so you both might be behind NAT.
Buy beer and a cable 1-2 meters UTP crimped ...
Connect two computers, one 192.168.0.1 another 192.168.0.2 put the port for 20-30k
Test / thump.
In addition to forwarding the port to a remote PC through NAT (if it is behind a router), you also need to make sure that this port is not blocked by a firewall on that PC (disable the firewall / firewall / antivirus or add the port to the allowed list) .. But your PC also needs a port allow in firewall.
How is your friend connected to the Internet? Your scheme will only work if the friend's PC has the same IP you are trying to connect to.
If the PC is connected through a router, and the IP belongs to it, you need to set up port forwarding. If the router also does not have the same IP, but, for example, 10.х.х.х, there is NAT and you will have to organize port forwarding on the side of the Internet provider. (or engage in hole-punching when two PCs behind NAT are connected using an external server with a real IP)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question