Home > Back-end >  Every time when socket programming read data read only once
Every time when socket programming read data read only once

Time:09-28

This is the server code, when I was in the client sends data, after why accept only once, the subsequent accept not to





Unsigned int StartServer (LPVOID lParam)
{
if (! AfxSocketInit ())
{
AfxMessageBox (IDP_SOCKETS_INIT_FAILED);
return 1;
}

M_exit=false;

LParam CServerDlg * aDlg=(CServerDlg *);

Cstrings strPort;

ADlg - & gt; GetDlgItemText (IDC_EDIT_PORT strPort);

UINT nPort=7600;

//socket -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

CSocket aSocket, serverSocket;

if (! ASocket. Socket ())
{
Char szError [256]={0};

Sprintf (szError, "Create Faild: % d", GetLastError ());

AfxMessageBox (szError);

return 1;
}

BOOL bOptVal=TRUE;
Int bOptLen=sizeof (BOOL);

ASocket. SetSockOpt (SO_REUSEADDR, (void *) & amp; BOptVal bOptLen, SOL_SOCKET);


if (! ASocket. Bind (nPort))
{
Char szError [256]={0};

Sprintf (szError, "Bind Faild: % d", GetLastError ());

AfxMessageBox (szError);

return 1;
}

if(! ASocket. Listen (10))
{
Char szError [256]={0};

Sprintf (szError, "Listen Faild: % d", GetLastError ());

AfxMessageBox (szError);

return 1;
}

Cstrings strText;

ADlg - & gt; GetDlgItemText (IDC_EDIT_LOG strText);

StrText +="Server Start! \r\n";

ADlg - & gt; SetDlgItemText (IDC_EDIT_LOG strText);

while(! M_exit)
{

if(! ASocket. Accept (serverSocket))
{
continue;
}
The else
{
Char szRecvMsg [50]={0};


ServerSocket. The Receive (szRecvMsg, 50);




ServerSocket. Close ();
}

}

ASocket. Close ();
ServerSocket. Close ();



return 0;
}

CodePudding user response:

You are
 serverSocket. Close (); 
the case, how can receive?
  • Related