W
wuchaa
Unregistered / Unconfirmed
GUEST, unregistred user!
我用VC2003开发了一个winsocket的 MFC Activex控件,控件中是socket 客户端,用于和服务器端进行通信,做完后在VC和VB中使用都是可以正常的发送和接收服务器端的数据,但是用ASP调用也能连接SOCKET成功,但是没有任何消息到达,我贴出来OCX中SOCKET连接的代码:LONG CAGENTOCXCtrl::CTRLConnectSocket(long nPort, BSTR Address){ AFX_MANAGE_STATE(AfxGetStaticModuleState()); if(isOnlyConnect)//该连接已建立,还没有断开 return 0; CString IpAddress; IpAddress.Format("%s",Address); _TCHAR *ip = 0; struct hostent *host = 0; struct sockaddr_in addr; ULONGdo
tIP = inet_addr(IpAddress); OnlySock = socket(AF_INET, SOCK_STREAM, 0);
if (OnlySock == INVALID_SOCKET) { shutdown(OnlySock, 0x02); closesocket(OnlySock);//释放占有的SOCK资源 return 3; } // 设定 SOCKADDR_IN 结构的内容 // 如果通讯协议是选择IP Protocol,那此值固定为AF_INET // AF_INET 与 PF_INET 这两个常量值相同 addr.sin_family = AF_INET;
addr.sin_port = htons(nPort);
addr.sin_addr.S_un.S_addr =do
tIP; if (dotIP == INADDR_NONE) { host = gethostbyname(IpAddress);
if (!host) { shutdown(OnlySock, 0x02); closesocket(OnlySock);//释放占有的SOCK资源 return 4;
};
ip = inet_ntoa(*(struct in_addr*)(*host->h_addr_list));
addr.sin_addr.S_un.S_addr = inet_addr(ip);
} //开始连线 if (connect(OnlySock, (LPSOCKADDR)&addr, sizeof(SOCKADDR))) { shutdown(OnlySock, 0x02); closesocket(OnlySock);//释放占有的SOCK资源 return 5;
} int iError=-1; if(m_hWnd) { iError = WSAAsyncSelect(OnlySock, m_hWnd, WM_MYWINSOCK, FD_READ|FD_CLOSE);
//只对网络断开和数据到达通知感兴趣 } else
{ iError = WSAAsyncSelect(OnlySock,AfxGetMainWnd()->m_hWnd,WM_MYWINSOCK, FD_READ|FD_CLOSE);
//只对网络断开和数据到达通知感兴趣 } //AfxGetMainWnd()->m_hWnd if(iError == SOCKET_ERROR)//无法绑定Winsock的事件通知 { int nRet = WSAGetLastError(); shutdown(OnlySock, 0x02); closesocket(OnlySock);//释放占有的SOCK资源 return nRet; } isOnlyConnect = true; return 1;}请大家看看帮我分析下,是哪里问题了,谢了
tIP = inet_addr(IpAddress); OnlySock = socket(AF_INET, SOCK_STREAM, 0);
if (OnlySock == INVALID_SOCKET) { shutdown(OnlySock, 0x02); closesocket(OnlySock);//释放占有的SOCK资源 return 3; } // 设定 SOCKADDR_IN 结构的内容 // 如果通讯协议是选择IP Protocol,那此值固定为AF_INET // AF_INET 与 PF_INET 这两个常量值相同 addr.sin_family = AF_INET;
addr.sin_port = htons(nPort);
addr.sin_addr.S_un.S_addr =do
tIP; if (dotIP == INADDR_NONE) { host = gethostbyname(IpAddress);
if (!host) { shutdown(OnlySock, 0x02); closesocket(OnlySock);//释放占有的SOCK资源 return 4;
};
ip = inet_ntoa(*(struct in_addr*)(*host->h_addr_list));
addr.sin_addr.S_un.S_addr = inet_addr(ip);
} //开始连线 if (connect(OnlySock, (LPSOCKADDR)&addr, sizeof(SOCKADDR))) { shutdown(OnlySock, 0x02); closesocket(OnlySock);//释放占有的SOCK资源 return 5;
} int iError=-1; if(m_hWnd) { iError = WSAAsyncSelect(OnlySock, m_hWnd, WM_MYWINSOCK, FD_READ|FD_CLOSE);
//只对网络断开和数据到达通知感兴趣 } else
{ iError = WSAAsyncSelect(OnlySock,AfxGetMainWnd()->m_hWnd,WM_MYWINSOCK, FD_READ|FD_CLOSE);
//只对网络断开和数据到达通知感兴趣 } //AfxGetMainWnd()->m_hWnd if(iError == SOCKET_ERROR)//无法绑定Winsock的事件通知 { int nRet = WSAGetLastError(); shutdown(OnlySock, 0x02); closesocket(OnlySock);//释放占有的SOCK资源 return nRet; } isOnlyConnect = true; return 1;}请大家看看帮我分析下,是哪里问题了,谢了