I'm trying to connect client with server. With local address (127.0.0.1) everything works perfectly fine, but when i try to use my remote address, it doesn't work. I search this thing all over the internet but i couldn't find anything.
Server side:
bool start_server()
{
int i = 1;
if(WSAStartup(MAKEWORD(2,0),&WsaDat) != 0)
{
cout << "WSA error!" << endl;
system("pause");
return FALSE;
}
if ( LOBYTE( WsaDat.wVersion ) != 2 || HIBYTE( WsaDat.wVersion ) != 0 )
{
cout << "Bad version of winsocket" << endl;
system("pause");
WSACleanup();
return 0;
}
serwer = socket(AF_INET, SOCK_STREAM, 0);
if(serwer == INVALID_SOCKET)
{
cout << "Can't create socketa!" << endl;
system("pause");
WSACleanup();
return 0;
}
int port;
cout << "Port input " << endl;
cin >> port; // 13056
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(port);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = INADDR_ANY;
setsockopt( serwer, SOL_SOCKET, SO_REUSEADDR, ( char * ) &i, sizeof ( i ) );
if(bind(serwer,(SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
cout << "Couldn't bind socket" << endl;
system("pause");
WSACleanup();
return 0;
}
for(int i = 0; i < MAX_CLIENTS; i++)
client[i].connected = FALSE;
if(listen(serwer,SOMAXCONN)==SOCKET_ERROR)
{
cout << "Listening error!" << endl;
system("pause");
WSACleanup();
return 0;
}
cout << "Serwer created!" << endl;
unsigned long b = 0;
ioctlsocket ( serwer, FIONBIO, &b );
return 1;
}
After this i have accept function on which program is blocking:
accept(serwer,(SOCKADDR *)(¤t_client->address),¤t_client->address_length);
Client side:
bool start_client()
{
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,0),&WsaDat) != 0)
cout << "WSA error!\n";
if ( LOBYTE( WsaDat.wVersion ) != 2 || HIBYTE( WsaDat.wVersion ) != 0 )
{
cout << "Wrong version of winsocket\n";
WSACleanup();
return false;
}
SOCKET Klient;
Klient = socket(AF_INET, SOCK_STREAM, 0);
if(Klient == INVALID_SOCKET)
{
cout << "Can't create socket socketa!\n";
WSACleanup();
return false;
}
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(PORT); // 13056
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_addr = inet_addr("*********"); // my remote address
while(connect(Klient, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) != 0)
{
cout << "Trying to connect " << WSAGetLastError() << "\n";
Sleep(500);
}
cout << "Connected!\n";
}
I have added both client and server applications to firewall rules and i have also tried to connect them on couple ports. Server side is listening for sure (here on different port):