创建socket时,这里发生错误,返回一个int的最大值,也就是4294967295SOCKETsock=socket(nFamily,nType,0);整体代码是这样的...
创建socket时,这里发生错误,返回一个int的最大值,也就是4294967295
SOCKET sock = socket(nFamily, nType, 0);
整体代码是这样的:
bool CSocketComm::ConnectTo(LPCTSTR strDestination, LPCTSTR strServiceName, int nFamily, int nType)
{
// 如果socket已经打开
// Socket is already opened
if ( IsOpen() )
return false;
SOCKET sock = socket(nFamily, nType, 0); //这里出错——————————
if (INVALID_SOCKET != sock)
{
// Associate a local address with the socket
SockAddrIn sockAddr;
if (false == sockAddr.CreateFrom(NULL, TEXT("0"), nFamily))
{
closesocket( sock );
return false;
}
if ( SOCKET_ERROR == bind(sock, sockAddr, sockAddr.Size() ))
{
closesocket( sock );
return false;
}
// Now get destination address & port
sockAddr.CreateFrom( strDestination, strServiceName );
// try to connect - if fail, server not ready
if (SOCKET_ERROR == connect( sock, sockAddr, sockAddr.Size()))
{
closesocket( sock );
return false;
}
// Success, now we may save this socket
m_hComm = (HANDLE) sock;
}
return (INVALID_SOCKET != sock);
}
这段代码就是初始化的,SOCKET sock = socket(nFamily, nType, 0);
nFamily = AF_INET;
nType = SOCK_STREAM;
在XXXApp类的Init函数中调用 AfxSocketInit()
这个函数才是初始化socket环境,你说的那句是创建socket
没有这个函数AfxSocketInit()
我写的win32程序 不是MFC的
那好吧,你看看这个
http://blog.csdn.net/jeffrey_ming/article/details/6002005
非常感谢 问题解决了!
int nerr;
WORD dwVersionRequested = MAKEWORD( 2, 2 );
nerr = WSAStartup( dwVersionRequested, &m_wsData );