2, not the server and the client connection, shown as the server of the black box in a constant state of display is listening, can't show that the connection is successful; The client's black box shows data sent successfully at the same time,,,
Program the following
The client
//* client
# define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include
#include
//Need to link with Ws2_32. Lib, Mswsock. Lib, and Advapi32. Lib
# pragma comment (lib, "Ws2_32. Lib")
# pragma comment (lib, "Mswsock. Lib")
# pragma comment (lib, "AdvApi32. Lib")
# define DEFAULT_BUFLEN 512
# define DEFAULT_PORT "27015"
Int __cdecl main (int arg c, char * * argv)
{
WSADATA WSADATA;
The SOCKET ConnectSocket=INVALID_SOCKET;
Struct addrinfo * result=NULL,
* PTR=NULL,
Hints.
Char * sendbuf="this is a test";
Char recvbuf [DEFAULT_BUFLEN];
Int iResult;
Int recvbuflen=DEFAULT_BUFLEN;
Printf (" the from client: \ n ");
//Validate the parameters
/* if (arg c!=2) {
Printf (" server - the name usage: % s \ n ", argv [0]).
return 1;
} */
//Initialize Winsock
//The WSAStartup function is called to initiate The use of WS2_32. DLL.
IResult=WSAStartup (MAKEWORD (2, 2), & amp; WsaData);
If (iResult!=0) {
Printf (" WSAStartup failed with error: % d \ n ", iResult);
return 1;
}
ZeroMemory (& amp; Hints, sizeof (hints));
Hints. Ai_family=AF_UNSPEC;
Hints. Ai_socktype=SOCK_STREAM;
Hints. Ai_protocol=IPPROTO_TCP;
//Resolve the server address and port
IResult=getaddrinfo (" localhost ", DEFAULT_PORT, & amp; Hints, & amp; The result);
If (iResult!=0) {
Printf (" getaddrinfo failed with error: % d \ n ", iResult);
WSACleanup ();
return 1;
}
//Attempt to connect to an address until one succeeds
For (PTR=the result; PTR!=NULL; PTR=PTR - & gt; Ai_next) {
//Create a SOCKET for connecting to the server
ConnectSocket=socket (PTR - & gt; Ai_family, PTR - & gt; Ai_socktype,
PTR - & gt; Ai_protocol);
If (ConnectSocket==INVALID_SOCKET) {
Printf (" socket failed with error: % ld \ n ", WSAGetLastError ());
WSACleanup ();
return 1;
}
//Connect to server.
IResult=connect (ConnectSocket, PTR - & gt; Ai_addr, (int) PTR - & gt; Ai_addrlen);
If (iResult==SOCKET_ERROR) {
Closesocket (ConnectSocket);
ConnectSocket=INVALID_SOCKET;
continue;
}
break;
}
Freeaddrinfo (result);
Printf (" jump out of the loop! \n");
If (ConnectSocket==INVALID_SOCKET) {
Printf (" Unable to connect to server! \n");
WSACleanup ();
return 1;
}
Printf (" connect success \ n ");
//Send an initial buffer
IResult=send (ConnectSocket, sendbuf, (int) strlen (sendbuf), 0).
If (iResult==SOCKET_ERROR) {
Printf (" send failed with error: % d \ n ", WSAGetLastError ());
Closesocket (ConnectSocket);
WSACleanup ();
return 1;
}
Printf (" Bytes Sent: % ld \ n ", iResult);
Printf (" sending content: % s \ n ", sendbuf);
//shutdown the connection since no more data will be sent
IResult=shutdown (ConnectSocket SD_SEND);
If (iResult==SOCKET_ERROR) {
Printf (" shutdown failed with error: % d \ n ", WSAGetLastError ());
Closesocket (ConnectSocket);
WSACleanup ();
return 1;
}
//the Receive until the peer closes the connection
Do {
IResult=recv (ConnectSocket, recvbuf recvbuflen, 0).
If (iResult & gt; 0)
Printf (" Bytes received: % d \ n ", iResult);
Else if (iResult==0)
Printf (" Connection closed \ n ");
The else
Printf (" recv failed with error: % d \ n ", WSAGetLastError ());
} while (iResult & gt; 0);
//the cleanup
Closesocket (ConnectSocket);
WSACleanup ();
getchar();
return 0;
}
The server side
UNICODE
# undef
# define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include
#include
//Need to link with Ws2_32. Lib
# pragma comment (lib, "Ws2_32. Lib")
//# pragma comment (lib, "Mswsock. Lib")
# define DEFAULT_BUFLEN 512
# define DEFAULT_PORT "27015"
Int __cdecl main (void)
{
WSADATA WSADATA;
Int iResult;
The SOCKET ListenSocket=INVALID_SOCKET;
The SOCKET ClientSocket=INVALID_SOCKET;
Struct addrinfo * result=NULL;
Struct addrinfo hints.
Int iSendResult;
Char recvbuf [DEFAULT_BUFLEN];
Int recvbuflen=DEFAULT_BUFLEN;
Printf (" the from server: \ n ");
//Initialize Winsock
IResult=WSAStartup (MAKEWORD (2, 2), & amp; WsaData);
If (iResult!=0) {
Printf (" WSAStartup failed with error: % d \ n ", iResult);
return 1;
}
ZeroMemory (& amp; Hints, sizeof (hints));
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull