Home > Software engineering >  Wrote a socket communication program (two computers connected always is not successful, can be on th
Wrote a socket communication program (two computers connected always is not successful, can be on th

Time:10-14

Socket Client and Server on your computer can connect successfully, but with two computers even by:
Code:
Client:


 # include & lt; Winsock2. H> 
#include
using namespace std;
Int main ()
{
WSADATA WSADATA;
The SOCKET client;
SOCKADDR_IN addr.
Char buf [128].
If (WSAStartup (0 x202, & amp; WsaData)!=0)
{
Cout<& lt;" Initiate Winsock DLL erro "& lt; return -1;
}
Client=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);
Cout<& lt;" Server IP: ";
Cin. Getline (buf, 128, '\ n');
Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_addr=inet_addr (buf);
Addr. Sin_port=htons (6666);
If (connect (client, (SOCKADDR *) & amp; Addr, sizeof (addr))==SOCKET_ERROR)
{
Cout<& lt;" Connect failed "& lt; WSACleanup();
return -1;


}
The else cout<& lt;" The connection is successful "& lt;

While (1)
{
Cin. Getline (buf, 128, '\ n');
If (STRCMP (buf, "quit")==0) break;
Send (client, buf, sizeof (buf), 0).
If (recv (client, buf, sizeof (buf), 0) & gt; 0)
{
CoutMemset (buf, 0128);
}
}


Closesocket (the client);
WSACleanup();
return 0;


}



 Server: 
#include
#include
using namespace std;
Int main ()
{
WSADATA WSADATA;
PHOSTENT hostinfo;
The SOCKET server, accSock;
SOCKADDR_IN addr, addrout;
Int port, len=sizeof (SOCKADDR);
Char buf [128], * IP;
If (WSAStartup (0 x202, & amp; WsaData)!=0)
{
Cout<& lt;" The error "& lt; return -1;
}
Server=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);
Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_addr=htonl (INADDR_ANY);
Addr. Sin_port=htons (6666);
Bind (server, (SOCKADDR *) & amp; Addr, sizeof (addr));
Gethostname (buf, sizeof (buf));
Hostinfo=gethostbyname (buf);
IP=inet_ntoa (* (IN_ADDR *) * hostinfo - & gt; H_addr_list);
Listen (server, 1);
Cout<& lt;" Receiver: "& lt; AccSock=accept (server, (SOCKADDR *) & amp; Addrout, & amp; Len);
IP=inet_ntoa (addrout. Sin_addr);
Port=htons (addrout. Sin_port);
Cout<& lt;" Sender: "& lt;



While (1)
{int flag=0;
Flag=recv (accSock, buf, sizeof (buf), 0).
If (flag> 0)
{
CoutCin. Getline (buf, 128);
Send (accSock, buf, sizeof (buf), 0).
Memset (buf, 0128);
}


If (STRCMP (buf, "quit")==0) break;




}
Closesocket (accSock);
Closesocket (server);
WSACleanup();


return 0;


}

CodePudding user response:

If the Server and the Client is not in the same network segment within the local area network (LAN), may need to complete the network IP and port can be accessed from the Server

CodePudding user response:

Firewall Settings!!!!!!

CodePudding user response:

Connect the connection failed, please check the WSAGetLastError () what is the return value,

CodePudding user response:

Look at the client iP Settings are correct

CodePudding user response:

A local area network (LAN) communication, then depends on the specific error message, and whether the machine has a firewall intercept processing

CodePudding user response:

After the client connect error code?
10054? 10061?
First check whether the network is normal, check the firewall

CodePudding user response:

Your IP has a problem, you wrote in the client feel about IP function is to obtain the IP address of the client machine, is not to get the address of the server, so you are on the same machine no problem, there is a problem on two machines

CodePudding user response:

Two computers if they are in the same local area network (LAN), close the firewall can, if it is a public network, need to do port mapping

CodePudding user response:

1. Can try IP ping
2. Connect return error, using GetWSALastError return an error code to see,

CodePudding user response:

refer to 7th floor u011673029 response:
your IP has a problem, you wrote in the client feel about IP function is to obtain the IP address of the client machine, is not to get the address of the server, so you are on the same machine no problem, on the two machines have problems

No, he is to enter the IP address of the server,
Estimate the cause of the firewall, the largest native computer how make, which can just make others,

CodePudding user response:

The building Lord why I can't use your communication is about to hand in the homework to do I see I QQ1269766840

CodePudding user response:

The
refer to the original poster Ginsmile1224830613 response:
Socket on the Client and Server on your computer can connect successfully, but with two computers even not on:

Your computer connection with two computers to connect the biggest difference is that IP is different, this machine is 127.0.0.1, and I'm curious, you IP is how to set up between the two machine?

CodePudding user response:

The
refer to 12 floor wxhxj0268 reply:
Quote: refer to the original poster Ginsmile1224830613 response:

Socket Client and Server on your computer can connect successfully, but with two computers even by:

Your computer connection with two computers to connect the biggest difference is that IP is different, this machine is 127.0.0.1, and I'm curious, you IP is how to set up between the two machine?

That if it is between two computers, how to set up IP?

CodePudding user response:

Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_addr=inet_addr (buf);
Addr. Sin_port=htons (6666);
If (connect (client, (SOCKADDR *) & amp; Addr, sizeof (addr))==SOCKET_ERROR)

nullnullnullnullnullnullnullnullnullnullnull