Home > other >  Recv not receive data and receiving data is broken?
Recv not receive data and receiving data is broken?

Time:10-12

//socket_sever. CPP: defines the entry point of the console application,
//

# include "stdafx. H"
//# include & lt; Windows. H>
#include

Typedef struct _socket_list
{
The SOCKET MainSock deterministically.
Int num.
The SOCKET sock_array [64].
} socket_list;
Void init_list (socket_list * list)
{
int i;
The list - & gt; MainSock deterministically=0;
The list - & gt; num=0;
For (I=0; I & lt; 64; I + +) {
The list - & gt; Sock_array [I]=0;
}
}
Void insert_list (SOCKET s, socket_list * list)
{
int i;
For (I=0; I & lt; 64; I++)
{
If (the list - & gt; Sock_array [I]==0)
{
The list - & gt; Sock_array [I]=s;
The list - & gt; Num +=1;
break;
}
}
}
Void delete_list (SOCKET s, socket_list * list)
{
int i;
For (I=0; I & lt; 64; I++)
{
If (the list - & gt; Sock_array [I]==s)
{
The list - & gt; Sock_array [I]=0;
The list - & gt; Num -=1;
break;
}
}
}
Void make_fdlist (socket_list * list, fd_set * fd_list)
{
int i;
FD_SET (list - & gt; MainSock deterministically, fd_list);
For (I=0; I & lt; 64; I++)
{
If (the list - & gt; Sock_array [I] & gt; 0)
{
FD_SET (list - & gt; Sock_array [I], fd_list);
}
}
}


Int _tmain (int arg c, _TCHAR * argv [])
{
WORD wVersionRequested;
WSADATA WSADATA;
Int err;
WVersionRequested=MAKEWORD (2, 2);
Err=WSAStartup (wVersionRequested, & amp; Wsadata);
If (err!=0)
{
Printf (" socket load character failure \ n ");
return false;
}
The SOCKET SeverSocket=SOCKET (AF_INET, SOCK_STREAM, 0);
//u_long u=1;
//ioctlsocket (SeverSocket FIONBIO, & amp; U);

SOCKADDR_IN socksin;
Socksin. Sin_family=AF_INET;
Socksin. Sin_port=htons (6688);
Socksin. Sin_addr. S_addr=htonl (INADDR_ANY);
//socksin. Sin_addr. S_addr=inet_addr (" 192.168.22.5 "); Binding a fixed IP
Bind (SeverSocket, (sockaddr *) & amp; Socksin, sizeof (socksin));
Listen (SeverSocket, 5);//listen

//clean up
//fd_set fdSocket;
//FD_ZERO (& amp; FdSocket);
//FD_SET (SeverSocket, & amp; FdSocket);
Socket_list sock_list;
Init_list (& amp; Sock_list);
Sock_list. MainSock deterministically=SeverSocket;

The SOCKET sNew;
Timeval time={5, 0};

SOCKADDR_IN addrRemote;
Int len=sizeof (addrRemote);
While (1)
{
Fd_set fdRead;
FD_ZERO (& amp; FdRead);
Make_fdlist (& amp; Sock_list, & amp; FdRead);
Int nRet=select (0, & amp; FdRead, NULL, NULL, & amp; Time);
If (nRet==SOCKET_ERROR)
{
Printf (" select error! Errcode=% d \ n ", WSAGetLastError ());
break;
}
If (nRet==0)
{
Printf (" timeout \ n ");
continue;
}
If (FD_ISSET (sock_list MainSock deterministically, & amp; FdRead))
{
SNew=accept (SeverSocket, (sockaddr *) & amp; AddrRemote, & amp; Len);//to accept links
Insert_list (sNew, & amp; Sock_list);
Have client link printf (" % s \ n ", inet_ntoa (addrRemote. Sin_addr));
}
for (int i=0; i<64; I++)
{
If (sock_list sock_array [I]==0) continue;
If (FD_ISSET (sock_list sock_array [I], & amp; FdRead))
{
Char szText [256].//cannot be initialized to 0, 0, can't accept below data
Int nRecv=recv (sock_list sock_array [I], szText, strlen (szText), 0).
If (nRecv==0)
{
//connections. Close to restart. The interrupt.
Closesocket (sock_list sock_array [I]);
Delete_list (sock_list sock_array [I], & amp; Sock_list);
//fdSocket fd_array [I]=0;
Printf (" disconnect links \ n ");
continue;

}
Else if (nRecv==SOCKET_ERROR)
{
If (WSAGetLastError ()==WSAEWOULDBLOCK)
Closesocket (sock_list sock_array [I]);
Printf (" recv err \ n ");
Delete_list (sock_list sock_array [I], & amp; Sock_list);//after the connection is broken, remove from the queue the socket
continue;
}

SzText [nRecv]='\ 0';
Printf (" % s \ n the received data, szText);
}
}


}
Closesocket (SeverSocket);
WSACleanup ();
getchar();
return 0;
}


The select model.

Char szText [256].//cannot be initialized to 0, 0, can't accept below data
Int nRecv=recv (sock_list sock_array [I], szText, strlen (szText), 0).
SzText [256]={0}; This assignment below nRecv are 0, szText="";
With the above code can receive data again. Why???????

The client sends data, several times the server to accept a few times, (server side) to accept data is incomplete, in a few times, a little data won't be able to receive????????????????

CodePudding user response:

Int nRecv=recv (sock_list sock_array [I], szText, strlen (szText), 0).
Depressed, strlen (szText), the egg pain well
  • Related