The client and the server respectively on two computers can't communication, why? Ask for advice,,,
Client:
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Winsock2. H>
# include & lt; String. H>
# define MAXSIZE 100
//refer to external need plus the extern keyword function, define the default function are extern
Extern int getline (char * s, int Max);
Int main (int arg c, char * argv [])
{
//initialize winsock structure, load winsock dynamic library
WSAData WSAData;
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData)!=0) {
Printf (" WSAStartup failed! \n");
return -1;
}
//create a socket
The SOCKET sockfd;
If ((sockfd=socket (AF_INET SOCK_STREAM, IPPROTO_TCP))==INVALID_SOCKET) {
Printf (" socket failed! \n");
return -1;
}
//initialize the server address structure
Struct sockaddr_in servaddr;
Memset (& amp; Servaddr, 0, sizeof (servaddr));
Servaddr. Sin_family=AF_INET;
Servaddr. Sin_port=htons (9999);
Servaddr. Sin_addr. S_un. S_addr=inet_addr (" 127.0.0.1 ");
//a connection request
If (connect (sockfd, (struct sockaddr *) & amp; Servaddr, sizeof (servaddr))=={SOCKET_ERROR)
Printf (" connect failed! \n");
Closesocket (sockfd);
WSACleanup ();
return -1;
}
The else
Printf (" Connection build... \n");
//send data to the server
Char buff [MAXSIZE];
Memset (buff, 0, MAXSIZE);
While (STRCMP (buff, "quit")!=0 & amp; & STRCMP (buff, "shutdown")!=0) {
Printf (" both Please input a string to send: ");
Getline (buff, MAXSIZE);
If (send (sockfd, buff, strlen (buff), 0) & lt;=0) {
Printf (" send failed! \n");
Closesocket (sockfd);
WSACleanup ();
return -1;
}
//initialize the buffer of 0
Memset (buff, 0, MAXSIZE);
If (recv (sockfd, buff, MAXSIZE, 0) & lt;=0) {
Printf (" recv failed! \n");
Closesocket (sockfd);
WSACleanup ();
return -1;
}
//print the received echo string
Char MSG [MAXSIZE];
Memset (MSG, 0, MAXSIZE);
Sprintf (MSG, "Received from the Server [] % s: % d: % s \ n", inet_ntoa (servaddr. Sin_addr), ntohs (servaddr. Sin_port), buff);
Printf (" % s \ n ", MSG);
}
//release resources
Closesocket (sockfd);
WSACleanup ();
system("pause");
return 0;
}
//getline functions definition:
Int getline (char * s, int Max)
{
int i=0;
char c;
While (- Max & gt; 0 & amp; & (c=getchar ())!=EOF & amp; & C!!! )
='\ n'S [i++]=c;
If (c='\ n')
S [I]='\ 0';
return i;
}
Server:
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Winsock2. H>
# include & lt; String. H>
//add the default link library
# pragma comment (lib, "ws2_32. Lib")
# define bzero memset (a, b) (a, 0, b)//vc without bzero definition, use memset instead of
//void * memset (void * DST, int c, size_t len)
DWORD WINAPI AnswerThread (LPVOID lparam);//thread function declaration
Struct sockaddr_in cliaddr;//external variables for thread function call
Int main (int arg c, char * argv [])
{
//initialize winsock version information, loading the dynamic link library (DLL)
WSADATA wsaData;
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData)!=0) {
Printf (" WSAStartup failed! \n");
return -1;
}
//create a listening socket
The SOCKET sockfd;
If ((sockfd=socket (AF_INET SOCK_STREAM, IPPROTO_TCP))==INVALID_SOCKET) {
Printf (" socket failed! \n");
WSACleanup ();
return -1;
}
//set the address of the server
Struct sockaddr_in servaddr;
Bzero (& amp; Servaddr, sizeof (servaddr));
Servaddr. Sin_family=AF_INET;
Servaddr. Sin_port=htons (9999);
Servaddr. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);
//bind socket address structure to the listening socket
If (bind (sockfd, (struct sockaddr *) & amp; Servaddr, sizeof (servaddr))!=0) {
Printf (" bind failed! \n");
Closesocket (sockfd);
WSACleanup ();
}
//on the Server to monitor
If (listen (sockfd, 4)!=0) {
Printf (" listen failed! \n");
Closesocket (sockfd);
WSACleanup ();
return -1;
}
//to accept the client's connection request
Printf (" TCP Server Start... \n");
Int cliaddrlen=sizeof (cliaddr);//cliaddrlen requires initial value, as a result a value - parameters in function
Bzero (& amp; Cliaddr, sizeof (cliaddr));//memset (& amp; Cliaddr, 0, sizeof (cliaddr));
The SOCKET connfd;
//loop awaiting
While (true)
{
If ((connfd=accept (sockfd, (struct sockaddr *) & amp; Cliaddr, & amp; Cliaddrlen))=={INVALID_SOCKET)
Printf (" accept failed! \n");
Closesocket (sockfd);
WSACleanup ();
return -1;
}
//create a new thread
DWORD ThreadID;//double word
CreateThread (NULL, 0, AnswerThread, (LPVOID) connfd, 0, & amp; ThreadID);
}
}
//thread function AnswerThread
DWORD WINAPI AnswerThread (LPVOID lparam)
{
//between the Server and the Client to send and receive data
Char buff [100].
Extern sockaddr_in cliaddr;
The SOCKET connfd=(SOCKET) (LPVOID lparam);
While (true)
{
Int ret=recv (connfd, buff, sizeof (buff), 0).
If (ret==SOCKET_ERROR) {
Printf (" recv failed! \n");
Closesocket (connfd);
WSACleanup ();
return -1;
}
//in order to avoid the typing errors, the string buff end set to 0 x00
Buff (ret)=0 x00;
//get the current system time
SYSTEMTIME st.
GetLocalTime (& amp; St);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull