int ret;
WSADATA wsaData;
eventTotal = 0;//³õʼ»¯Ê¼þµÄ×ÜÊý
InitializeCriticalSection(&cSection);
if((ret = WSAStartup(MAKEWORD(2,2),&wsaData)) != 0)
{
AfxMessageBox("´íÎó£ºWSAStartup failed with error %d\n", ret);
return 0;
}
SOCKET sListen;
SOCKADDR_IN inetAddr;
DWORD dwFlags;
DWORD dwRecvBytes;
//´´½¨socket
if((sListen = WSASocket(AF_INET,SOCK_STREAM,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED))
== INVALID_SOCKET)
{
AfxMessageBox("´íÎó£ºcreate socket");
WSACleanup();
return 0;
}
//°ó¶¨Ö÷»úµØÖ·
inetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
inetAddr.sin_family = AF_INET;
inetAddr.sin_port = htons(SERVERPORT);
if(bind(sListen,(sockaddr *)&inetAddr,sizeof(inetAddr)) == SOCKET_ERROR)
{
AfxMessageBox("´íÎó£ºbind in start");
WSACleanup();
return 0;
}
//¿ªÊ¼¼àÌý
if(listen(sListen,5) !=0 )
{
AfxMessageBox("´íÎó:listen failed in start");
return 0;
}
//´´½¨Ò»¸öclient
if((sAccept = WSASocket(AF_INET,SOCK_STREAM,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED))
== INVALID_SOCKET)
{
AfxMessageBox("´íÎó£ºcan not create sAccept Socket");
return 0;
}
//´´½¨µÚÒ»¸öÊÖ¶¯ÖØÖöÔÏó
if((eventArray[eventTotal] = WSACreateEvent()) == WSA_INVALID_EVENT)
{
AfxMessageBox("´íÎó£ºcan not create eventArray[0]");
return 0;
}
HANDLE handle;
DWORD dwThreadID;
if((handle = CreateThread(NULL,0,ProcessThreadIO,(LPVOID)this,0,&dwThreadID))
== NULL)
{
AfxMessageBox("´íÎó:can not create thread");
return 0;
}
CloseHandle(handle);
memset(&overlapped,0,sizeof(WSAOVERLAPPED));//³õʼ»¯²ÎÊý
overlapped.hEvent = eventArray[eventTotal];
eventTotal++;
dataBuf.buf = buffer;
dataBuf.len = DATA_BUFSIZE;
//½ÓÊܸ÷¸öclientµÄÁ¬½Ó
while(true)
{
//¿ªÊ¼½ÓÊÕÁ¬½Ó
int sockLen = sizeof(inetAddr);
if((sAccept = accept(sListen,(sockaddr*)&inetAddr,&sockLen))
== INVALID_SOCKET)
{
AfxMessageBox("²»ÄܽÓÊÕÁ¬½ÓÇëÇó");
return 0;
}
//·¢³ö½ÓÊÕÇëÇó
int error = 0;
if(WSARecv(sAccept,&dataBuf,1,&dwRecvBytes,&dwFlags,&overlapped,NULL)
== SOCKET_ERROR)
{
if ((error = WSAGetLastError()) != ERROR_IO_PENDING)
{
AfxMessageBox("·¢³ö½ÓÊÕÇëÇóʧ°Ü");
return 0;
}
}
}
return 1;
不知道为什么在执行到if(WSARecv(sAccept,&dataBuf,1,&dwRecvBytes,&dwFlags,&overlapped,NULL)
== SOCKET_ERROR)时取到的错误代码是10045的错误,The attempted operation is not supported for the type of object referenced.
哪位大虾能够帮帮我,感激不尽!
--------------------next---------------------
阅读(1701) | 评论(0) | 转发(0) |