Problem, the client disconnects after even not on the service side, have to restart the server
DWORD WINAPI SrvThread (LPVOID lpParam)
{
//TODO: Add your the control notification handler code here
WSADATA WSADATA;
Int port=50005;//port
Cstrings A0001Str A0002Str, Errstr;
SendA0002 char sendA0001 [1024], [1024], recvbuf [1024].//sending and receiving data buffer
While (1)
{
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData)!=0)
{
Errstr. The Format (" % d ", WSAGetLastError ());
PDcvp - & gt; WriteLog (" server failed, error code: "+ Errstr);
continue;
}
//create listens for socket
The SOCKET sockSrv=SOCKET (AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
AddrSrv. Sin_family=AF_INET;
AddrSrv. Sin_port=htons (port);//more than 1024 port
AddrSrv. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);
Int retVal=bind (sockSrv, (LPSOCKADDR) & amp; AddrSrv, sizeof (SOCKADDR_IN));
If (retVal==SOCKET_ERROR) {
Errstr. The Format (" % d ", WSAGetLastError ());
PDcvp - & gt; WriteLog (" error code: "+ Errstr);
continue;
}
If (listen (sockSrv, 10)==SOCKET_ERROR) {
Errstr. The Format (" % d ", WSAGetLastError ());
PDcvp - & gt; WriteLog (" error code: "+ Errstr);
continue;
}
SOCKADDR_IN addrClient;
Int len=sizeof (SOCKADDR);
The SOCKET sockConn;
SockConn=accept (sockSrv, (SOCKADDR *) & amp; AddrClient, & amp; Len);
While (sockConn!=INVALID_SOCKET)
{
//receiving data
Memset (recvbuf, 0, sizeof (recvbuf));
If (recv (sockConn, recvbuf, sizeof (recvbuf), 0)==SOCKET_ERROR)
{
Errstr. The Format (" % d ", WSAGetLastError ());
PDcvp - & gt; WriteLog (" receiving data error, error code 2: "+ Errstr);
continue;
}
Strcpy (sendA0001 A0001Str);
If (! STRCMP (" A0001 ", recvbuf))
{
//send data
If (send (sockConn sendA0001, sizeof (sendA0001), 0)==SOCKET_ERROR)
{
Errstr. The Format (" % d ", WSAGetLastError ());
PDcvp - & gt; WriteLog (" send A0001 error, error code: "+ Errstr);
continue;
}
}
Else if (! STRNCMP (" A0002 ", recvbuf, 5))
{
Memset (sendA0002, 0, sizeof (sendA0002));
Strcpy (sendA0002 A0002Str);
//send data
If (send (sockConn sendA0002, sizeof (sendA0002), 0)==SOCKET_ERROR)
{
//sockConn=accept (sockSrv, (SOCKADDR *) & amp; AddrClient, & amp; Len);
Errstr. The Format (" % d ", WSAGetLastError ());
PDcvp - & gt; WriteLog (" send A0002 error, error code: "+ Errstr);
continue;
}
}
}
If (sockConn!=INVALID_SOCKET)
{
Closesocket (sockConn);
SockConn=INVALID_SOCKET;
}
Closesocket (sockSrv);
WSACleanup ();
}
return 0;
CodePudding user response:
Don't know how many predecessors in the TCP SocketSend (many) send (less) send (wealth)
Has been so much illness recv (), recv (wealth)
Inside the trap!
http://bbs.csdn.net/topics/380167545
CodePudding user response:
Code logic is wrong, learn to debug wrong