none
About Socket Thread And an error RRS feed

  • Pregunta

  • I have this code to the socket server.


    try  {
    ASCIIEncoding^ ascii = gcnew ASCIIEncoding;

    // Set the TcpListener on port 13000.
    Int32 port = 13000;
    IPAddress^ localAddr = IPAddress::Parse( "127.0.0.1" );
    // TcpListener* server = new TcpListener(port);
    TcpListener^ server = gcnew TcpListener( localAddr,port );

    // Start listening for client requests.
    server->Start();

    // Buffer for reading data
    array<Byte>^bytes = gcnew array<Byte>(256);
    String^ data = nullptr;

          // Enter the listening loop.
    while ( true ) {

    //consoletxt->Text = ( "Waiting for a connection... " );

    // Perform a blocking call to accept requests.
    // You could also user server.AcceptSocket() here.
    TcpClient^ client = server->AcceptTcpClient();
    //consoletxt->Text = ( "Connected!" );
    data = nullptr;

    // Get a stream Object* for reading and writing
    NetworkStream^ stream = client->GetStream();
    Int32 i;

    // Loop to receive all the data sent by the client.
    while ( i = stream->Read( bytes, 0, bytes->Length ) )
    {
    data = ascii->GetString( bytes, 0, i );
    //consoletxt->Text = ( "Received: {0}", data );

    data = data->ToUpper();
    array<Byte>^msg = ascii->GetBytes( data );

    stream->Write( msg, 0, msg->Length );
    //consoletxt->Text = ( "Sent: {0}", data );
    }

    // Shutdown and end connection
    client->Close();
    }
    }
    catch ( SocketException^ e )    {
    //Console::WriteLine( "SocketException: {0}", e );
    }

    I think that works, because the cliente recieve the data that had send, but in the moment that client it´s killed. The server... well, crash and tell me that error.


    Excepción no controlada del tipo 'System.IO.IOException' en System.dll

    Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. <-- Can you help me.

    miércoles, 28 de octubre de 2009 12:04

Respuestas