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 ServerCodePudding 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 correctCodePudding user response:
A local area network (LAN) communication, then depends on the specific error message, and whether the machine has a firewall intercept processingCodePudding 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 machinesCodePudding 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 mappingCodePudding user response:
1. Can try IP ping2. Connect return error, using GetWSALastError return an error code to see,
CodePudding user response: