c++ - Can't connect to remote ip -
i'm trying connect client server. local address (127.0.0.1) works fine, when try use remote address, doesn't work. search thing on internet couldn't find anything.
server side:
bool start_server() { int = 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 ( ) ); if(bind(serwer,(sockaddr *)(&sockaddr), sizeof(sockaddr)) == socket_error) { cout << "couldn't bind socket" << endl; system("pause"); wsacleanup(); return 0; } for(int = 0; < 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 have accept function on program 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("*********"); // remote address while(connect(klient, (sockaddr *)(&sockaddr), sizeof(sockaddr)) != 0) { cout << "trying connect " << wsagetlasterror() << "\n"; sleep(500); } cout << "connected!\n"; }
i have added both client , server applications firewall rules , have tried connect them on couple ports. server side listening sure (here on different port):
there nothing wrong code.
not routers , network address translators (nats) support hairpinning, ability connect public ip within private network behind nat.
if nat/router support this, need configure port-forwarding rule on nat/route map incoming tcp traffic on port 14354 (your server listening port) internal ip address of 192.168.0.10. think of router perspective, if sees , incoming connection request on public ip - needs know host on private network forward connection to.
Comments
Post a Comment