socket只响应一次FD_ACCEPT是什么有关问题
socket只响应一次FD_ACCEPT是什么问题
一个简单的网络编程的小例子,为什么我的Socket只响应一次FD_ACCEPT消息以后,不再响应任何消息..........
------解决思路----------------------
FD_ACCEPT:
a. When WSAAsyncSelect called, if there is currently a connection request available to accept.
b. When a connection request arrives, if FD_ACCEPT not already posted.
c .After accept called, if there is another connection request available to accept.
只有一个客户端去连接(客户端调用connect()) ???
OnBnClickedStartServer()
{
WSADATA wsaData;
int ret = WSAStartup(MAKEWORD(2,0),&wsaData);
if(ret)
{
AfxMessageBox(_T("初始化网络失败!"));
}
m_Socket = ::socket(AF_INET,SOCK_STREAM,0);
ASSERT(m_Socket != INVALID_SOCKET);
CString str;
char host_name[255]={NULL};
if ( !gethostname(host_name,sizeof(host_name)))
{
PHOSTENT host_info = gethostbyname(host_name);
char *ip = inet_ntoa (*(struct in_addr *)*host_info->h_addr_list);
m_addr.sin_family = AF_INET;
m_addr.sin_addr.S_un.S_addr = inet_addr(ip);
m_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
m_addr.sin_port = htons(_wtoi(m_strPCPort));
ret = bind(m_Socket,(sockaddr*)&m_addr,sizeof(m_addr));
if(ret ==SOCKET_ERROR)
{
CString strTemp(_T(""));
strTemp.Format(_T("绑定失败,错误代码%d"), GetLastError());
showMsg(strTemp);
closesocket(m_Socket);
WSACleanup();
return ;
}
ret = listen(m_Socket,5);
if(ret ==SOCKET_ERROR)
{
CString strTemp(_T(""));
strTemp.Format(_T("绑定失败,错误代码%d"), GetLastError());
showMsg(strTemp);
closesocket(m_Socket);
WSACleanup();
return ;
}
showMsg(_T("服务器启动成功.") );
WSAAsyncSelect(m_Socket,this->m_hWnd,WM_SOCK,FD_ACCEPT|FD_READ|FD_CLOSE|FD_CONNECT);
}
}
LRESULT CTransimitSMSDlg::OnSocket(WPARAM wParam, LPARAM lParam)
{
UpdateData();
char data[100];
CString strTemp, strShow;
ZeroMemory(data, 100);
int len;
SOCKET sock;
switch (lParam)
{
case FD_READ:
sock = (SOCKET)wParam;
len = recv(sock,data,100,NULL);
strShow = (_T("收到数据:"));
for ( int i = 0;i < len; i++)
{
strTemp.Format(_T("%02X "), data[i]);
strShow += strTemp;
}
case FD_ACCEPT:
// s = ::accept(m_Socket,NULL,NULL);
strShow = _T("有客户连接!");
break;
case FD_CLOSE:
strShow = _T("客户断开连接!");
break;
default:
break;
}
showMsg(strShow);
UpdateData(FALSE);
return 0;
}
一个简单的网络编程的小例子,为什么我的Socket只响应一次FD_ACCEPT消息以后,不再响应任何消息..........
------解决思路----------------------
FD_ACCEPT:
a. When WSAAsyncSelect called, if there is currently a connection request available to accept.
b. When a connection request arrives, if FD_ACCEPT not already posted.
c .After accept called, if there is another connection request available to accept.
只有一个客户端去连接(客户端调用connect()) ???