Socket高手救我,我为什么老是connect失败???急。。。。

hecCIBN 2002-06-04 02:28:29
我在做一个面向TCP(stream式)的链接,我是客户端,我为什么老是connect失败???我的代码如下:

g_nServerPort[0]=sFirstPort;
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;
}

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:引用指针错误。


...全文
2300 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
taolei 2002-06-04
  • 打赏
  • 举报
回复
一定要等到FD_WRITE消息收到后才能发送数据,否则一定会失败。
但可不用一定在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消息。
hecCIBN 2002-06-04
  • 打赏
  • 举报
回复
to taolei(实在无聊) :我必须等到收到FD_WRITE消息后才能发送数据吗?

to qiufuwang(呆呆瓜) :谢谢你贴出了代码,我回记着给你的分的。



qiufuwang 2002-06-04
  • 打赏
  • 举报
回复
我的程序片段:
{
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:
.......
}
}
taolei 2002-06-04
  • 打赏
  • 举报
回复
调用WSAAsyncSelect后,socket就自动改成异步的了,前面有两位朋友已经指出问题所在。
异步的socket要靠消息驱动,connect调用后返回的99%都是马上返回WSAEWOULDBLOCK,
当系统连接成功后,系统会向你的监听消息的窗口发送连接成功的FD_CONNECT消息,
当有数据来到时,会收到FD_READ消息,你要在消息处理函数里读出数据

当可以发送数据时(连接成功后或所有要发送的数据已经发送完后),收到FD_WRITE消息,你要发送的数据此时可以发送。

当对方关闭连接时,收到FD_CLOSE消息,此时你也应该关闭连接,释放相关资源。
hecCIBN 2002-06-04
  • 打赏
  • 举报
回复
to qiufuwang(呆呆瓜) :这一步我已经做过了

to ychener(贫血) , florist2000(善良的石头) :你们的意见我感觉很有道理,我马上试一下。

谢谢大家
qiufuwang 2002-06-04
  • 打赏
  • 举报
回复
在程序的最开始的地方加入:
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData );

我说对了就给分!
florist2000 2002-06-04
  • 打赏
  • 举报
回复
你采用的是非阻塞的套节字模式(异步选择模型)
当然一般返回错误了。但并不代表真正的连接失败

ychener 2002-06-04
  • 打赏
  • 举报
回复
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.
ychener 2002-06-04
  • 打赏
  • 举报
回复
MSDN上这样写到

If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not).
ychener 2002-06-04
  • 打赏
  • 举报
回复
你用的是AsyncSelect IO 模型 所以
connect是异步的,你可以用WSAGetLastError看看
如果是WSAEWOULDBLOCK 并不表示已经失败了,

具体应该在事件消息中判断是否连接成功!

snsins 2002-06-04
  • 打赏
  • 举报
回复
nRet=connect(g_ClientSocket[0],(struct sockaddr *)&g_sockServerAddr[0],sizeof(g_sockServerAddr[0]));
hecCIBN 2002-06-04
  • 打赏
  • 举报
回复
to Kevin_qing() ,youyi(youyiY) :我少copy了一句设置端口号,

重新贴代码:

g_nServerPort[0]=sFirstPort;
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:引用指针错误。
snsins 2002-06-04
  • 打赏
  • 举报
回复
////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
youyi 2002-06-04
  • 打赏
  • 举报
回复
连接的端口服务器和客户机要一至.服务器收到连接要有反馈.
Kevin_qing 2002-06-04
  • 打赏
  • 举报
回复
你的端口都没有填,系统怎么知道你要连接什么地方?

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧