struct ip_mreq mreq; // Used in adding or dropping
// multicasting addresses
SOCKADDR_IN local_sin; // Local socket's address
// recv_sin; // Holds the source address upon
// recvfrom function returns
WSADATA WSAData; // Contains details of the
// Winsock implementation
// Fill out the local socket's address information.
local_sin.sin_family = AF_INET;
local_sin.sin_port = htons (udpSetting.intDestPort);
local_sin.sin_addr.s_addr = htonl (INADDR_ANY);
// Associate the local address with Sock.
if (bind (sktRec,
(struct sockaddr FAR *) &local_sin,
sizeof (local_sin)) == SOCKET_ERROR)
{
wsprintf (szError, TEXT("Binding socket failed! Error: %d"),
WSAGetLastError ());
AfxMessageBox (szError, MB_OK);
closesocket (sktRec);
return 3;
}
// Join the multicast group from which to receive datagrams.
mreq.imr_multiaddr.s_addr = inet_addr (udpSetting.chrRecvIP);
mreq.imr_interface.s_addr = INADDR_ANY;
//关闭SOCKET
void UDP_CloseSocket(SOCKET &sktRec)
{
int intCycle=0;
// Disable sending on Sock before closing it.
while (intCycle<5) {
if(shutdown (sktRec, 0x01)==0){
break;
}
intCycle++;
}
intCycle=0;
// Close Sock.
while (intCycle<5) {
if(closesocket(sktRec)==0){
break;
}
intCycle++;
}
while (intCycle<5) {
if(WSACleanup()==0){
break;
}
intCycle++;
}
}
UINT CTelnetCtrl::SocketThread(LPVOID pParam)
{
CTelnetCtrl *pTelnetCtrl = (CTelnetCtrl*)pParam;
pTelnetCtrl->m_bThreadAlive = TRUE;
memset(pTelnetCtrl->m_pReadBuffer, 0, MAX_TELREAD_SIZE);
while(1)
{
DWORD m_Flag=WSAWaitForMultipleEvents(2,pTelnetCtrl->m_hEventArray,false,-1,false);
switch(m_Flag)
{
case WSA_WAIT_EVENT_0://退出
pTelnetCtrl->m_bThreadAlive = FALSE;
AfxEndThread(100);
break;
case WSA_WAIT_EVENT_0+1://Socket消息
{
WSANETWORKEVENTS we;
int iRet=WSAEnumNetworkEvents(pTelnetCtrl->m_Socket,pTelnetCtrl->m_hSocketEvent,&we);
if(we.lNetworkEvents&FD_READ)
{//读取
pTelnetCtrl->RecvTransData();
// printf("FD_READ\n");
}
if(we.lNetworkEvents&FD_ACCEPT)
{
//printf("accept\n");
//OnAccept();
}
if(we.lNetworkEvents&FD_CLOSE)
{
//printf("Close\n");
//pTelnetCtrl->DisConnect();
pTelnetCtrl->Close();
pTelnetCtrl->m_bThreadAlive = FALSE;
AfxEndThread(100);
}
if(we.lNetworkEvents&FD_CONNECT)
{
//printf("accept\n");
//OnConnect();
}
}//end case wsa_wait_event_0
break;
case WSA_WAIT_TIMEOUT: //do timeout
// printf("timeout\n");
break;
case WAIT_IO_COMPLETION:// One or more I/O completion routines are queued for execution.
//printf("One or more I/O completion routines are queued for execution. \n");
break;
case WSANOTINITIALISED:// A successful WSAStartup call must occur before using this function.
//printf("A successful WSAStartup call must occur before using this function. \n");
break;
case WSAENETDOWN:// The network subsystem has failed.
//printf("The network subsystem has failed. \n");
break;
case WSAEINPROGRESS:// A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
//printf("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. \n");
break;
case WSA_NOT_ENOUGH_MEMORY: //Not enough free memory available to complete the operation.
//printf("Not enough free memory available to complete the operation. \n");
break;
case WSA_INVALID_HANDLE: //One or more of the values in the lphEvents array is not a valid event object handle.
//printf("One or more of the values in the lphEvents array is not a valid event object handle. \n");
break;
case WSA_INVALID_PARAMETER:// The cEvents parameter does not contain a valid handle count.
//printf("The cEvents parameter does not contain a valid handle count. \n");
break;
default:
break;