The server receives the data to the client receiving data is not correct, the correct should be the following
Client: both please enter message: liu,
Server: client say: liu,
The service side: both please enter the message: I
Client: server say: I
But the actual debugging with the client and server are not of the content of the right to receive keyboard input, the following
The last attached source:
Server:
#include
#include
# pragma comment (lib, "ws2_32. Lib")//loaded ws2_32. DLL library file
Int main ()
{
/*... */
/*... WSAStartup - initialize the socket character... */
/*... */
WSADATA WSADATA;//define library version information structure
WORD wVersionRequested;//define the type of version, WORD (WORD) : an unsigned short
WVersionRequested=MAKEWORD (2, 2);//version to version 2.2, the front is the low byte, followed by the high byte
Int int_WSAStartup=WSAStartup (wVersionRequested, & amp; WsaData);//the WSAStartup function returns a value of type int
If (int_WSAStartup!=0)
{
Printf (" load socket failed! \n");
return 0;
}
If (LOBYTE (wsaData wVersion)!=2 | | HIBYTE (wsaData wVersion)!=2)//load requests the version number of the judge whether the detector satisfies the requirements, | | said "or"
{
WSACleanup ();
return 0;
}
/*... */
/*... The socket - create a socket... */
/*... */
The SOCKET socket_server=SOCKET (AF_INET, SOCK_STREAM, 0);
/*... */
/*... The bind - bind socket... */
/*... */
SOCKADDR_IN server_address;
Server_address. Sin_family=AF_INET;//address family, must be AF_INET
Server_address. Sin_addr. S_un. S_addr=inet_addr (" 127.0.0.1 ");//htonl (INADDR_ANY);//host address
Server_address. Sin_port=htons (5000);//port
Int int_bind=bind (socket_server, (struct sockaddr *) & amp; Server_address, sizeof (SOCKADDR));//bind function return value is an int type
//struct sockaddr=sockaddr and struct sockaddr *=sockaddr *
If (int_bind!=0)
{
Printf (" binding failed! \n");
}
/*... */
/*... Listen - set a socket to listening state... */
/*... */
Int int_listen=listen (socket_server, 5);//listen function returns a value of type int
If (int_listen & lt; 0)//biggest client number 5
{
Printf (" listening failed! \n");
}
/*... */
/*... The accept - receiving a client request... */
/*... */
Int length=sizeof (SOCKADDR);
SOCKADDR_IN client_address;
The SOCKET socket_data_transfer;
Socket_data_transfer=accept (socket_server, (struct sockaddr *) & amp; Client_address, & amp; Length);//the accept function return value is a new socket type
If (socket_data_transfer==SOCKET_ERROR)
{
Printf (" receive with the client's connection fails! \n");
}
/*... */
/*... Recv and send - to chat... */
/*... */
While (1)
{
/*... Recv - receiving data... */
Char Receivebuf [100].//receiving data buffer
Int int_recv=recv (socket_data_transfer Receivebuf, 100, 0);//recv function returns a value of type int
If (int_recv & lt; 0)
{
Printf (" receive client data failed! \n");
Printf (" exit the server-side program! \n");
break;
}
The else
{
Printf (" the client say: % s \ n ", Receivebuf);
}
/*... The send - send data... */
Char Sendbuf [100].//send data buffer
Printf (" do enter the message: ");
Scanf_s (" % s ", Sendbuf);
Int int_send=send (socket_data_transfer, Sendbuf, 100, 0);//send function return value is an int type
If (int_send & lt; 0)
{
Printf (" send data on the server failed! \n");
}
}
/*... */
/*... Closesocket/WSACleanup - release socket, closed ws2_32. DLL dynamic library... */
/*... */
Closesocket (socket_data_transfer);
Closesocket (socket_server);
WSACleanup ();
return 0;
}
Client:
#include
#include
# pragma comment (lib, "ws2_32. Lib")//loaded ws2_32. DLL library file
Int main ()
{
/*... */
/*... WSAStartup - initialize the socket character... */
/*... */
WSADATA WSADATA;//define library version information structure
WORD wVersionRequested;//define the type of version, WORD (WORD) : an unsigned short
WVersionRequested=MAKEWORD (2, 2);//version to version 2.2, the front is the low byte, followed by the high byte
Int int_WSAStartup=WSAStartup (wVersionRequested, & amp; WsaData);//the WSAStartup function returns a value of type int
If (int_WSAStartup!=0)
{
Printf (" load socket failed! \n");
return 0;
}
If (LOBYTE (wsaData wVersion)!=2 | | HIBYTE (wsaData wVersion)!=2)//load requests the version number of the judge whether the detector satisfies the requirements, | | said "or"
{
WSACleanup ();
return 0;
}
/*... */
/*... The socket - create a socket... */
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull