////the info of the server socket
g_sockServerAddr[i].sin_family = AF_INET;
g_sockServerAddr[i].sin_port=htons(nport);
g_sockServerAddr[i].sin_addr.s_addr=inet_addr(szServerIp);then connect
g_sockServerAddr[i].sin_family = AF_INET;
g_sockServerAddr[i].sin_port=htons(nport);
g_sockServerAddr[i].sin_addr.s_addr=inet_addr(szServerIp);then connect
解决方案 »
- 请教Shape这个类属于哪个头文件
- 求助一个关于VC 简单的写数据到文件指定位置的问题
- 一小问?
- 请问如何自定义打印页面大小,其中所用到的打印页面大小可能不一样?
- 怎么把CTreeView或者CTreeCtrl中的"-","+"换成自己的图片.
- 流操作拷贝文件,为什么结尾总会多一个字节?
- CHtmlView中取源代码
- 我做了个打传呼的程序(我知道用的人不多,不过我用,呵呵)谁要留个email,多提宝贵意见
- 请问菜单中那里可以设置调试时不进行assertion的检测的选项
- 用vc++做局域网内视频放系统,有那些资料可参考。
- 那里有CD转录成wav的DLL(包括函数说明)?小弟有急用。
- 菜鸟问题,我被MFC的Create弄糊涂了,帮帮我。
g_nServerPort[1]=sSecondPort;int i=0;
for(i=0;i<2;i++)////prepare to setup the 2 TCP links
{
g_ClientSocket[i] = socket(AF_INET, SOCK_STREAM, 0);
if(g_ClientSocket[i]==INVALID_SOCKET)
{
MessageBox( NULL,"create client socket error", NULL,MB_OK);
return FALSE;
}
////the info of the server socket
g_sockServerAddr[i].sin_family = AF_INET;
ulIP = inet_addr(sServerIP);
if ( INADDR_NONE != ulIP )
g_sockServerAddr[i].sin_addr.S_un.S_addr = ulIP;
else
{
MessageBox( NULL, "server IP error", NULL, MB_OK);
return FALSE;
} ////端口号
g_sockServerAddr[i].sin_port = htons(g_nServerPort[i]); int nErrorCode = WSAAsyncSelect(g_ClientSocket[i],hWnd,WM_RCV1+i*10, FD_READ);
if (nErrorCode == SOCKET_ERROR)
{
MessageBox( NULL, "asyncselect error", NULL, MB_OK);
return FALSE;
}
}int nRet;
nRet=connect(g_ClientSocket[0],(LPSOCKADDR)&g_sockServerAddr[0],sizeof(g_sockServerAddr[0]));
if (nResponse!=0)
{
nErrorCode=WSAGetLastError();
str.Format("错误号%d",nErrorCode);
MessageBox( NULL,"connect to the server error! "+str, NULL, MB_OK);
return -1;
}
bConnected[0] = TRUE;
服务器端能够acccept,connect()失败后取到的错误码是10035:引用指针错误。
connect是异步的,你可以用WSAGetLastError看看
如果是WSAEWOULDBLOCK 并不表示已经失败了,具体应该在事件消息中判断是否连接成功!
(10035) = WSAEWOULDBLOCK
Resource temporarily unavailable.
This error is returned from operations on nonblocking sockets that cannot be completed immediately, for example recv when no data is queued to be read from the socket. It is a nonfatal error, and the operation should be retried later. It is normal for WSAEWOULDBLOCK to be reported as the result from calling connect on a nonblocking SOCK_STREAM socket, since some time must elapse for the connection to be established.
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData );我说对了就给分!
异步的socket要靠消息驱动,connect调用后返回的99%都是马上返回WSAEWOULDBLOCK,
当系统连接成功后,系统会向你的监听消息的窗口发送连接成功的FD_CONNECT消息,
当有数据来到时,会收到FD_READ消息,你要在消息处理函数里读出数据当可以发送数据时(连接成功后或所有要发送的数据已经发送完后),收到FD_WRITE消息,你要发送的数据此时可以发送。当对方关闭连接时,收到FD_CLOSE消息,此时你也应该关闭连接,释放相关资源。
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData );
................. WSAAsyncSelect(pClientSock->m_hSocket,GetSafeHwnd),WM_CLIENTSOCKEVENT,FD_CONNECT|FD_READ|FD_CLOSE);
if(SOCKET_ERROR==pClientSock->Connect(m_ip,SERVER_PORT))
{
return false;
}
}afx_msg LONG CMySocket::OnClientSockEvent(UINT wParam, LONG lParam)
{
CAsyncSocket *psRecv;
int i=0;
int hlP=0;
switch(LOWORD(lParam))
{
case FD_CONNECT:
hlP=HIWORD(lParam);
if(hlP>0)
{
switch(hlP)
{
case WSAEAFNOSUPPORT:
AfxMessageBox("地址不对");
break;
case WSAECONNREFUSED:
AfxMessageBox("连接被拒绝");
break;
case WSAETIMEDOUT:
AfxMessageBox("连接超时");
break;
case WSAENETUNREACH:
AfxMessageBox("地址不对");
break;
case WSAEINVAL:
case WSAEISCONN:
AfxMessageBox("该套接字正被使用");
break;
default:
AfxMessageBox("发生错误,未能连接上");
break;
}
psRecv = CAsyncSocket::FromHandle((SOCKET)wParam);
if(psRecv==NULL) break; }
psRecv->Close();
delete psRecv;
psRecv=NULL;
break;
}
psRecv = CAsyncSocket::FromHandle((SOCKET)wParam);
if(psRecv==NULL) break;
psRecv->Send(m_ClientSendBuff,SOCK_BUFFSIZE);
break;
case FD_READ:
.......
}
}
但可不用一定在FD_WRITE消息处理函数里发送数据,收到FD_WRITE之后可以做个标记,如:BOOL canwrite=TRUE;然后在其他地方当canwrite==TRUE时发送数据,当你发送数据失败,并且返回WSAEWOULDBLOCK后,canwrite=FALSE;等下次FD_WRITE到来之后,才能再发送。我的方法:
自己准备一个buffer,要发送的数据先放到buffer里,如果canwrite==TRUE,就立即发送,直到buffer里的数据都成功发送;如果没发送完时返回WSAEWOULDBLOCK,就canwrite=FALSE;返回先。
当FD_WRITE到来时,canwrite=TRUE,然后再重复上面发送数据的过程。注意:如果发送数据都成功,没有返回WSAEWOULDBLOCK,那么,系统将不会再发送FD_WRITE消息。