Home > Back-end >  For help since writing about VC 6.0 client and server problems
For help since writing about VC 6.0 client and server problems

Time:09-24

The following is a server-side code:
# include "stdafx. H"
# include
# pragma comment (lib, "ws2_32. Lib")
# include

Int main (int arg c, char * argv [])
{//load the Winsock library code, successfully returns 0
//failures are calling WSAGetLastError
WSADATA WSADATA;
WORD sockVersion=MAKEWORD (2, 2);
//load the Winsock library
If (WSAStartup (sockVersion, & amp; WsaData)!=0)
return 0;


/* socket function prototype is as follows:
The SOCKET SOCKET {
Int af.//the specified socket address format, it can only use AF_INET
The int type.//specify the type of the socket
Int protocol;//TCP or udp
} */
//create a socket
SOCKET sListen=SOCKET (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (sListen==INVALID_SOCKET) {
Printf (" socket error \ n ");
return 0;
}


//in the sockaddr_in structure load address information
Sockaddr_in sin;
Sin. Sin_family=AF_INET;
Sin. The sin_port=htons (4500);//short htons function will host an unsigned integer into the network byte order
Sin. The sin_addr. S_un. S_addr=INADDR_ANY;


//make a socket and local address binding
If (bind (sListen, (LPSOCKADDR) & amp; Sin, sizeof (sin))==SOCKET_ERROR)
{
Printf (" bind error \ n ");
Closesocket (sListen);
return 0;
}



//set the socket into listening mode
If (listen (sListen, 5)==SOCKET_ERROR) {
Printf (" listen error \ n ");
Closesocket (sListen);
return 0;
}



//loop accept client connection requests
Sockaddr_in remoteAddr;
The SOCKET sClient;
Int nAddrLen=sizeof (remoteAddr);
Char revData [255].
While (true)
{
//to accept a new connection
SClient=accept (sListen, (SOCKADDR *) & amp; RemoteAddr, & amp; NAddrLen);
//the accept function call failure continues to wait for link
If (sClient==INVALID_SOCKET)
{
Printf (" the accept () error ");
continue;
}


//print out the connection of IP
Printf (" receives a connection: % s \ r \ n ", inet_ntoa (remoteAddr. Sin_addr));
//until receive valid data printed
Int ret=recv (sClient revData, 255, 0);
If (ret> 0)
{
//in order to prevent the printing error, the string end set to 0 x00;
RevData (ret)=0 x00;
The printf (revData);
}
Char * buff="\ r \ n hackers programming, I'm coming \ r \ n";

//send data
Send (sClient, buff, strlen (buff), 0).
//close the socket handles to end the conversation
Closesocket (sClient);
}
Closesocket (sListen);
WSACleanup();
return 0;
}
Below is the client:
# include "stdafx. H"
# include
# pragma comment (lib, "ws2_32")
# include

Int main (int arg c, char * argv [])
{
//load the Winsock library code, successfully returns 0
//failures are calling WSAGetLastError
WSADATA WSADATA;
WORD sockVersion=MAKEWORD (2, 2);
//load the Winsock library
If (WSAStartup (sockVersion, & amp; WsaData)!=0)
return 0;
/* socket function prototype is as follows:
The SOCKET SOCKET {
Int af.//the specified socket address format, it can only use AF_INET
The int type.//specify the type of the socket
Int protocol;//TCP or udp
} */
//create a socket
SOCKET sClient=SOCKET (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (sClient==INVALID_SOCKET)
{
Printf (" socket error \ n ");
return 0;
}


//in the sockaddr_in structure load address information
Sockaddr_in servAddr;
ServAddr. Sin_family=AF_INET;
ServAddr. Sin_port=htons (4500);//short htons function will host an unsigned integer into the network byte order
ServAddr. Sin_addr. S_un. S_addr=inet_addr (" 127.0.0.1 ");//the server IP connection server


//make a socket and local address binding
If (connect (sClient, (sockaddr *) & amp; ServAddr, sizeof (servAddr))==SOCKET_ERROR)
{
Printf (" connect error \ n ");
Closesocket (sClient);
return 0;
}
Int ret=recv (sClient revData, 255, 0);
If (ret> 0)
{
//in order to prevent the printing error, the string end set to 0 x00;
RevData (ret)=0 x00;
The printf (revData);
}
Char * buff="\ r \ n hackers programming, I'm coming \ r \ n";

//send data
Send (sClient, buff, strlen (buff), 0).
Char revData [255].
//until receive valid data printed

Closesocket (sClient);
WSACleanup();
return 0;
}
But my client is always prompt stdafx. H header file can't find, can't open, no to files.

CodePudding user response:

# include "stdafx. H"
Try this line commented out
Or in VS 2010
  • Related