Home > Back-end >  C network programming in the socket
C network programming in the socket

Time:09-29

If (m_serSocket. InitAndListen (this - & gt; M_hWnd, m_uport)==FALSE)
{
return;
}
The else
{
M_bInit=TRUE;
M_bClient=FALSE;
CString str;
STR="server setup success!/n ";
GetDlgItem (IDC_SHOWTEXT) - & gt; SetWindowText (STR);
}



The following is a function of their own to create a class, don't know why will appear when bind socket on a non socket tried a operation
BOOL CServer: : InitAndListen (HWND HWND, UINT port)
{
M_uPort=port;
M_hWnd=HWND;
If (m_hSocket!=NULL)
{
Closesocket (m_hSocket);
M_hSocket=NULL;
}
If (m_hSocket=NULL)
{
M_hSocket=socket (AF_INET SOCK_STREAM, 0).
ASSERT (m_hSocket!=NULL);
If (WSAAsyncSelect (m_hSocket, m_hWnd, SER_MESSAGE FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE) & gt; 0)
AfxMessageBox (" set up the event error ");

}
M_addr. Sin_family=AF_INET;
M_addr. Sin_addr. S_un. S_addr=INADDR_ANY;
M_addr. Sin_port=htons (m_uPort);
Int ret.
Ret=bind (m_hSocket, (LPSOCKADDR) & amp; M_addr, sizeof (m_addr));
If (ret=SOCKET_ERROR)
{
TCHAR szBuf [128].
LPVOID lpMsgBuf;
FormatMessage (
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError (),
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) & amp; LpMsgBuf,
0, NULL);
LpMsgBuf MessageBox (NULL, (LPCTSTR), "error", MB_OK | MB_ICONINFORMATION);
LocalFree (lpMsgBuf);
AfxMessageBox (" binding error ");
return FALSE;
}
Ret=listen (m_hSocket, 5);
If (ret=SOCKET_ERROR)
{
AfxMessageBox (" listening failure ");
return FALSE;
}
  • Related