Home > other >  The socket small white for help!
The socket small white for help!

Time:09-18

Wrote a echo client and the server, the server will string from the client to send back to the client,
But when recv len set size and is not the same as the client send have a problem, for the first time receive well, playing a line of string for a second time, the client sent the function will return 1, error number 10038,, but if the buf size set on both sides of the agreement is no problem, I'm not sure where is wrong, the man to help me see,
The client
# include & lt; stdio.h>
# include & lt; Stdlib. H>
# include & lt; Winsock2. H>
# define MAX_LEN 1024
Int main (int arg c, char * argv [])
{
//printf (" % s ", argv [0]).
WSADATA WSADATA;
//there is no special meaningThe SOCKET hSocket;//socket handles
SOCKADDR_IN servAddr;
Char message [30].
Char info [MAX_LEN];
Int strLen;
Int strlen=0;
int count=0;
If (arg c!
=3){
Usage: printf (" % s \ n ", argv [0]);
exit(1);
}
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData)!=0) {//must call this function input version information
Printf (" error: WSAStartup fail!" );
exit(1);
}
HSocket=socket (PF_INET SOCK_STREAM, 0).//ipv4, streaming socket, since the configuration type
If (hSocket==INVALID_SOCKET) {
Printf (" error: Socket fail!" );
exit(1);
}
Memset (& amp; ServAddr, 0, sizeof (servAddr));
ServAddr. Sin_family=AF_INET;
ServAddr. Sin_addr. S_un. S_addr=inet_addr () (const char * argv [1]).//converts a string to integer and at the same time to convert the byte order
ServAddr. Sin_port=htons (atoi (const char *) argv [2]));//byte order conversion
Int the error=connect (hSocket, (SOCKADDR *) & amp; ServAddr, sizeof (servAddr));
If (error==SOCKET_ERROR) {//a connection request
Int err=WSAGetLastError ();
Printf (" error: Connect the fail! # # % d ", err);

exit(1);
}
//receiving data

While (1)
{
Fputs (" Input message (Q to quit: ", stdout);//print, from the standard output
The fgets (info, 1024, stdin);//read from standard input
if (! STRCMP (info, "q \ n"))
break;
//while strlen (=recv (hsocket, message + strlen, 1, 0)) {
//if (strlen==1) {
//printf (" error: read fail!" );
////exit (1);
//}
//strlen +=strlen;
//}
Int str_len=send (hSocket, info, MAX_LEN, 0);//why don't send a second time even?
If (str_len==1) {
Int ret2=WSAGetLastError ();
Printf (" % d ", ret2);
}
Printf (" \ n % d ", str_len);
Int recv_len=0;
Int recv_cnt=0;
Memset (info, 0, MAX_LEN);
While (recv_len & lt; Str_len) {
Recv_cnt=recv (hSocket, info + recv_len MAX_LEN, 0).
Printf (" \ n % d ", recv_cnt);
Recv_len +=recv_cnt;
}

Printf (" Meesage is % s \ n ", the info).
//printf (" the Message fom the server: % s \ n ", Message);

}
Closesocket (hSocket);
WSACleanup ();
system("pause");
return 0;


}
The service side
#include
#include
# include & lt; WinSock2. H>
Void ErrorHandling (const char * message);
# define MAX_LEN 600
Int main (int arg c, char * argv [])
{
int count=0;
WSADATA WSADATA;
The SOCKET hServSock hClntSock;
SOCKADDR_IN servAddr clntAddr;
Char info [MAX_LEN];
Int szClntAddr;
Char message []="Hello World!" ;
If (arg c!
=2){
Usage: printf (" % s & lt; Port> \ n ", argv [0]);
exit(1);
}
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData)!=0)
ErrorHandling (" WSAStarup () error!" );

HServSock=socket (PF_INET SOCK_STREAM, 0).
If (hServSock==INVALID_SOCKET)
ErrorHandling (" socket () error ");
Memset (& amp; ServAddr, 0, sizeof (servAddr));
ServAddr. Sin_family=AF_INET;
ServAddr. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);//machines may be more than one network card
ServAddr. Sin_port=htons (atoi (const char *) argv [1]));
If (bind (hServSock, (SOCKADDR *) & amp; ServAddr, sizeof (servAddr))==SOCKET_ERROR)
ErrorHandling (" bind () error ");
If (listen (hServSock, 5)==SOCKET_ERROR)
ErrorHandling (" listen () error ");
SzClntAddr=sizeof (clntAddr);
While (count<5) {
Printf (" listening... \n");
HClntSock=accept (hServSock, (SOCKADDR *) & amp; ClntAddr, & amp; SzClntAddr);
If (hClntSock==INVALID_SOCKET)
ErrorHandling (" the accept () error ");
The else
Printf (" the connect client % d \ n ", count);
//send (hClntSock, message, sizeof (the message), 0).
Int ret.
Int str_len=0;
While ((ret=recv (hClntSock, info, sizeof (info), 0)) & gt; 0)//send buffer and receive buffer consistent best
{
Printf (" \ n % d ", ret);
Send (hClntSock, info, sizeof (info), 0).
}

Closesocket (hClntSock);
printf("\n");
count++;
}
Closesocket (hServSock);
WSACleanup ();
return 0;



}


Void ErrorHandling (const char * message)
{
The fputs (message, stderr);
Fputc (' \ n 'stderr);
exit(1);
}

CodePudding user response:

The MAX_LEN if the server is set to 600 or 2048 will appear afore-mentioned problems

CodePudding user response:

I think now is the client's

Recv_cnt=recv (hSocket, info + recv_len MAX_LEN, 0).
This sentence has a problem, comment out this sentence do not have a thing, if receive 600 for the first time, the second time receiving 600, buffer of crossing the line, lead to can't connect?

CodePudding user response:

reference
while ((ret=recv (hClntSock, info, sizeof (info), 0)) & gt; nullnullnullnullnullnullnull
  • Related