用CAsyncSocket类进行TCP通信时,客户端执行Connect()方法老是报错:
首先程序加入了#include "afxSock.h",AfxSocketInit();一。我是继承了两个CAsyncSocket类,服务端执行了:
Create(9001);
Listen();
然后在重载的OnAccept()事件中执行:
CAsyncSocket aSocket;
if (!Accept(aSocket))
{return;}
char aa[200];
memset(aa,0,200);
Receive(aa,200,FD_READ);
CAsyncSocket::OnAccept(nErrorCode);客户端:
CAsyncSocket cs;
if (!cs.Create())
{
AfxMessageBox("创建Socket失败!");
return;
}BOOL dd = cs.Connect("192.168.0.1",9001); //这是我本机的IP,,执行到这里,返回FALSE
if (!dd)
{
DWORD res = GetLastError();  //返回10035
AfxMessageBox("连接失败!");
return;
}请问这是为什么,几天了都没搞定

解决方案 »

  1.   

    楼上的,
    服务端执行了Create()方法,这自动调用了Bind(),
    这个你应该知道吧??两个星星怎么来的?
      

  2.   

    listen 正常吗?用tcpview看看是否在监听
      

  3.   

    Create不会自动调用bindCall the Create member function after constructing a socket object to create the Windows socket and attach it. Create then calls Bind to bind the socket to the specified address. The following socket types are supported
      

  4.   

    to:楼上的,
    我也调用了Bind,但也还是Connect失败,
      

  5.   

    to: Caps77, 你也是星星啊,MSDN的原话你看懂没?晕Res
    Create then calls Bind to bind the socket to the specified address. The following socket types are supported: SOCK_STREAM   Provides sequenced, reliable, full-duplex, connection-based byte streams. Uses the Transmission Control Protocol (TCP) for the Internet address family. 
    SOCK_DGRAM   Supports datagrams, which are connectionless, unreliable packets of a fixed (typically small) maximum length. Uses the User Datagram Protocol (UDP) for the Internet address family.
      

  6.   

    异步类的连接不会检查是否成功,执行连接操作后就返回,所以他的返回永远是false,改成CSocket就没问题了!
      

  7.   

    to:Caps77(厉兵秣马)
    跟端口无关,我试了好些端口,,to:zwzz541(木鱼)
    就算Connect返回FALSE,我再调用Send(),但是服务端收不到信息
      

  8.   

    哈哈……返回false代表不能连接了。
    这个都不懂四个三角怎么混的????
      

  9.   

    MFC的网络类在线程或者DLL里是很不好用的,遇到问题要解决很困难(诸如遇到Create死掉,Connect不能连,这些问题我至今也没找着原因),建议还是使用API吧。或者弄个API的 connect看看是这个类的问题还是机子或者网络的问题。PS:楼主如果不是在线程或DLL里调用。当我的话没说!
      

  10.   

    是在exe中,不是在dll里。也没用线程。。
      

  11.   

    to:zwzz541(木鱼) (你写了一个测试的吗?
    或者谁帮我写一个用CAsyncSocket类,并使用TCP的通信方式的小程序
    ,发我email: [email protected]收到后100分相送...
      

  12.   

    CAsyncSocket aSocket;作用域错了(我认为)应该将其变为可见,当ONACCEPT()退出时,aSocket销毁了,发送数据的套接字不存在了所以报错。
      

  13.   

    tcpview是个察看你机器上那个进程打开了哪些端口的小工具,网上搜,也可以找相关的替代工具看一看,用它看木马很方便
      

  14.   

    lwg0869(lwg) 
    说的应该对
      

  15.   

    我是用公司的开发工具些的!是VC7。0不知道你可以用不!
    又看了下你的程序!
    CAsyncSocket aSocket;
    if (!Accept(aSocket))
    {return;}
    char aa[200];
    memset(aa,0,200);
    Receive(aa,200,FD_READ);            ——————aSocket.Receive(aa,200,FD_READ);
    CAsyncSocket::OnAccept(nErrorCode);
      

  16.   

    connect('127.0.0.1' , 
    看看
      

  17.   

    我试过:connect('127.0.0.1' ),一样的问题
      

  18.   

    我的也出现了这样的问题,但是不用管它,依然可以发送数据包
    就是把下面的判断不要
    if (!dd)
    {
    DWORD res = GetLastError();  //返回10035
    AfxMessageBox("连接失败!");
    return;
    }
      

  19.   

    我能否就这样认为
    CAsyncSocket m_Sock;
    m_Sock.Create();
    int i = m_Sock.Connect("127.0.0.1",700);
    Connect返回的就是0?(尽管和MSDN上面不相符合)
      

  20.   

    用CAsyncSocket类的话,Connect()会马上返回,是否成功要看CAsyncSocket::OnConnect(int nErrorCode );
    nErrorCode = 0表示成功
      

  21.   

    无法立即完成一个非阻挡性套接字操作。 On a blocking socket, the return value indicates success or failure of the connection attempt.With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three possible scenarios:
    Use the select function to determine the completion of the connection request by checking to see if the socket is writeable. 
    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). 
    If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that the connect operation is complete (successfully or not).
      

  22.   

    我给一个权威的答案吧。执行connect()后会马上返回来的,由于是异步的,所以马上返回来的值肯定是0,但是并不是说不成功,是否成功需要等 CONNECT 事件回调后才知道。在此可以使用 GetLastError()函数判断。如下:
    if (WSAConnect(nSocket, sockAddr, socklen, NULL, NULL, NULL, NULL) == SOCKET_ERROR)
        {
            int wsaError = WSAGetLastError();
            if (wsaError != WSAEWOULDBLOCK)
            {
                 //连接失败
            }
        }