我這樣怎么不行﹐要如何操作?
sockaddr_in addr;
int addrLen;
addr.sin_addr.s_addr=::htonl(INADDR_ANY);
addr.sin_family=AF_INET;
addr.sin_port=5000;
addrLen=sizeof(addr);CSocket s1,s2;
s1.Create();
s1.Bind((sockaddr *)&addr,addrLen);
s1.Listen();
s1.Accept(s2,(sockaddr *)&(pdlg->addr),&(pdlg->addrLen));......

解决方案 »

  1.   

    后面一句應該是﹕
    s1.Accept(s2,(sockaddr *)&addr,&addrLen);
      

  2.   

    s1.Create(); // 问题可能在这里,参考一下Create的原型:
    BOOL Create( UINT nSocketPort = 0, int nSocketType = SOCK_STREAM, long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, LPCTSTR lpszSocketAddress = NULL );在Create的时候,就应该指定端口了
      

  3.   

    s1.Create();
    这里没指定端口,系统为你产生了一个随机端口但你的client不知道端口号,connect不到
    so server段accept没反应
      

  4.   

    指定斷口號也不行啊﹐我直接用api建立的服務端和客戶斷傳送文件都沒有問題
      

  5.   

    你指的不行是指什么,如果是异常
    用wsaGetlasterror看看错误代码
      

  6.   


    我說的不行是程序運行沒錯誤﹐s1.Accept(s2)后程序應該進入等待連接的狀態﹐但是我用客戶端嘗試去connect卻沒有任何反應
      

  7.   

    关于CAsyncSocket::Accept
    它的原型是:
    virtual BOOL Accept( CAsyncSocket& rConnectedSocket, SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL );
    参数:
    rConnectedSocketA reference identifying a new socket that is available for connection.lpSockAddrA pointer to a SOCKADDR structure that receives the address of the connecting socket, as known on the network. The exact format of the lpSockAddr argument is determined by the address family established when the socket was created. If lpSockAddr and/or lpSockAddrLen are equal to NULL, then no information about the remote address of the accepted socket is returned.lpSockAddrLenA pointer to the length of the address in lpSockAddr in bytes. The lpSockAddrLen is a value-result parameter: it should initially contain the amount of space pointed to by lpSockAddr; on return it will contain the actual length (in bytes) of the address returned. 注解:
    Call this member function to accept a connection on a socket. This routine extracts the first connection in the queue of pending connections, creates a new socket with the same properties as this socket, and attaches it to rConnectedSocket. If no pending connections are present on the queue, Accept returns zero and GetLastError returns an error. The accepted socket (rConnectedSocket) cannot be used to accept more connections. The original socket remains open and listening. The argument lpSockAddr is a result parameter that is filled in with the address of the connecting socket, as known to the communications layer. Accept is used with connection-based socket types such as SOCK_STREAM. accept过的socket不能被用来接受更多的连接,所以应该新new一个socket用来accept,而原始的socket是用来侦听的。例如我的程序
    void CListenSocket::OnAccept(int nErrorCode)
    {
    CAcceptSocket *pSocket=new CAcceptSocket(m_pClientDlg);
    ……
    if(Accept(*pSocket))
    {
    ……我的后两个参数是省略的