Friday 15 February 2013

c++ - Error with bind() and listen() functions (WinSock) -


I am working with WinSock and I have a problem: if I I define MY_IP to be "127.0.0.1" This works but I do not want to set MY_IP to "109.95.202.122" but it is not working is.

I have failed: failed bind () and listen to error

  #pragma comment (lib, "Ws2_32.lib") # include & lt; Sdkddkver.h & gt; # Include & lt; Conio.h & gt; # Include & lt; Stdio.h & gt; #include & lt; WinSock2.h & gt; #include & lt; Windows.h & gt; # Include & lt; Iostream & gt; #include & lt; String & gt; # Include & lt; Process.h & gt; #my_IP "109.95.202.122" using the namespace, std; Int main () {WSADATA wsaData; Complete result = WSAStartup (MacWorld (2, 2), and Wisadata); If (result! = NO_ERROR) cout & lt; & Lt; "Initial error." & Lt; & Lt; Endl; Socket Main Socket = Socket (AFINETE, SOCKRRM, IPPRTOOTCPCP); If (main socket == INVALID_SOCKET) {printf ("Error creating socket:% ld \ n", WSAGetLastError ()); WSACleanup (); Return 1; } Sockaddr_in service; Mails (& amp; service, 0, size (service)); Service.sin_family = AF_INET; Service.sin_addr.s_addr = inet_addr (MY_IP); Service.sin_port = htons (27015); If (bind (main socket, (Sokkadar *) and service, size (service)) == SOCKET_ERROR {cout & lt; & Lt; "Failed Tie ()" & lt; & Lt; Endl; Port (main socket); } (Listen (main socket, 1) == SOCKET_ERROR) {cout & lt; & Lt; "Listen Error"; } Cout & lt; & Lt; "Okezywani na Poljegenti" & lt; & Lt; Endl; Socket swisset = SOCKET_ERROR; While (acceptSocket == SOCKET_ERROR) {acceptSocket = Accept (main socket, rick, tap); } If (Acceptance! = SOCKET_ERROR) {cout & lt; & Lt; "Connected !: D" & lt; & Lt; Endl; For (;;) {four chidedomosac [1000]; RCV (Main Socket, Chydomos, Volume (CHDOSOSCO), Faucet); }}    

If any dam () or <

If a server is connected to itself 127.0.0.1, then the client will only be able to connect to 127.0.0.1 If customers need to connect to 109.95.202.122, then the server is required to provide local IP You will need to bind which client will accept or specify INADDR_ANY to bind all available local IPs. If 109.95.202.122 server has a local IP then all is well. However, if 109.95.202.122 is actually the public IP of a router / net that the server is running behind, then the server needs to bind to the local IP which is configured for the router / NAT port-forward inbound requests. Tie () can only be linked to the local IP which is related to the machine which is running dam () .

No comments:

Post a Comment