server, server socket can listen from client, and accept this client socket call, when server use error=recv() function to receive data, error=0 which means connection is closed from the client? why?the source code for client socket:IPEndPoint ipe = new IPEndPoint(address, Port);
Socket tempSocket = 
new Socket(ipe.AddressFamily,  SocketType.Stream, ProtocolType.Tcp);tempSocket.Connect(ipe);
if(tempSocket.Connected)
{
s = tempSocket;
Byte[] bytesSent = Encoding.ASCII.GetBytes(TrackCode);
s.Send(bytesSent, bytesSent.Length, 0);
}Any body can help me with these? thanks a lot.

解决方案 »

  1.   

    There is no problem with server socket.the result from server is:connection closed from client.
    source code for server socket:    // create the server socket
        s = socket( AF_INET, SOCK_STREAM, 0 );
        if ( s < 0 )
        {        ExitProcess( -1 );
        }
        dontblock = 1;
        ioctlsocket( s, FIONBIO, ( u_long* )&dontblock );    // bind to port and start listening
        memset( &addr, 0, sizeof( addr ) );
        addr.sin_family = AF_INET;
        addr.sin_port = htons( port );
        addr.sin_addr.s_addr = INADDR_ANY;    if ( bind( s, ( struct sockaddr* )&addr, sizeof( addr ) ) < 0 )
        {
            ExitProcess( -1 );
        }
        if ( listen( s, SOMAXCONN ) != 0 )
        {
            ExitProcess( -1 );
        }
     while ( !kill )
        {
            err = accept( s, NULL, NULL );
     
            if ( err < 0 )
            {
                Sleep( 200 );
                continue;
            }        temp = err;        err = sizeof( addr );
            if ( getpeername( temp, ( struct sockaddr* )&addr, &err ) == 0 )
            {
            len = 32;
                WSAAddressToString( ( struct sockaddr* )&addr, sizeof( addr ), NULL, ipbuf, &len );
                if (t3dnsIpList.isInList(std::wstring(ipbuf)))
                {
                 // It's a 3DNS IP, just close socket and do nothing.
                shutdown( temp, SD_BOTH );
                closesocket( temp );
                }
                else
                {
                // check for valid ip address
                for ( i = ips.begin(); i != ips.end(); i++ )
                {
                    if ( addr.sin_addr.s_addr == i->addr )
                    {
                        sac = new SACStruct();
                        wcsncpy( sac->ip, ipbuf, 24 );
                        sac->ip[15] = 0;
                        sac->s = temp;
                        sac->addr = addr.sin_addr.s_addr;

                        _beginthread( SACClientThread, 0, sac );

                        break;
                    }
                }
    SACClientThread:   while ( !prockill )
        {
            // look for data        if ( !kill )
            {
                err = recvSACmsg( s, &data, 0 );            if ( err > 1 ) // good read
                {
                    lastmsg = time( NULL );                if ( err > 3 )
                    {
                        MultiByteToWideChar( CP_ACP, 0, data, -1, data2, 2048 );                    // check sequence
                        wcsncpy( buf, &data2[3], 4 );
                        buf[4] = 0;
                        i = _wtoi( buf );
                        if ( i != ( rcvseq + 1 ) )
                        {
                            swprintf( buf, L"Message sequence error reading from SAC %s.  Expected %d, recieved %d.", sac->ip, rcvseq + 1, i );
                            logger.LogError( 129, buf );
                        }                    central.pushdirected( L"MASTER", data2, sac->addr );
                        logger.LogTrace( data2 );                    rcvseq = i;
                        if ( rcvseq >= 9999 )
                            rcvseq = 0;
                    }
                }
                else if ( err < 0 ) // error
                {
                    swprintf( buf, L"Socket error reading from SAC %s.", sac->ip );
                    logger.LogError( 122, buf );
                    break;
                }
                else if ( err == 0 ) // connection closed
                {
                    swprintf( buf, L"Connection from SAC %s closed.", sac->ip );
                    logger.LogAudit( 232, buf );
                    break;
                }
                else if ( ( lastmsg + 60 ) < time( NULL ) )
                {
                    swprintf( buf, L"No messages from SAC %s for more than 60 seconds.", sac->ip );
                    logger.LogError( 124, buf );
                    break;
                }            if ( data )
                    delete data;
            }
      

  2.   

    socket.connected=true in the whole process.
      

  3.   

    楼主真的是老外?
    靠..
    你发送的SOCKET是个局部变量啊.发送完后,
    s这个套接字资源被系统回收了,当然就断开连接了啊.