2

I have a ready-to-use application (client) which connect to a server via TCP, i've used Wireshark to see what's happening and got this :

Client send to server this packet :

|00|18|e7|96|92|13|fc|f8|ae|2b|7a|4b|08|00|45|00|00|3f|6d|d8|00|00|80|11|49|7d|c0|a8|01|07|c0|a8|01|01|c2|b3|00|35|00|2b|cc|7f|5e|fe|01|00|00|01|00|00|00|00|00|00|05|70|61|6e|65|6c|07|6d|75|66|69|62|6f|74|03|6e|65|74|00|00|01|00|01|

And server answers back with :

0xfc 0xf8 0xae 0x2b 0x7a 0x4b 0x00 0x18 0xe7 0x96 0x92 0x13 0x08 0x00 0x45 0x28 0x00 0x34 0x00 0x00 0x40 0x00 0x34 0x06 0x2a 0xa4 0x95 0xca 0xc4 0x7e 0xc0 0xa8 0x01 0x07 0x19 0x9b 0xde 0x39 0x18 0x24 0xd5 0x66 0x85 0xa3 0xb1 0x7b 0x80 0x12 0x72 0x10 0xc4 0x81 0x00 0x00 0x02 0x04 0x05 0xac 0x01 0x01 0x04 0x02 0x01 0x03 0x03 0x07

So my current server code is :

Int32 port = 6555;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
var g1 = new byte[] { 0xfc, 0xf8, 0xae, 0x2b, 0x7a, 0x4b, 0x00, 0x18, 0xe7, 0x96, 0x92, 0x13, 0x08, 0x00, 0x45, 0x28, 0x00, 0x34, 0x00, 0x00, 0x40, 0x00, 0x34, 0x06, 0x2a, 0xa4, 0x95, 0xca, 0xc4, 0x7e, 0xc0, 0xa8, 0x01, 0x07, 0x19, 0x9b, 0xde, 0x39, 0x18, 0x24, 0xd5, 0x66, 0x85, 0xa3, 0xb1, 0x7b, 0x80, 0x12, 0x72, 0x10, 0xc4, 0x81, 0x00, 0x00, 0x02, 0x04, 0x05, 0xac, 0x01, 0x01, 0x04, 0x02, 0x01, 0x03, 0x03, 0x07 };

server = new TcpListener(localAddr, port);
server.start();
while(true)
{
     TcpClient client = server.AcceptTcpClient();
     NetworkStream stream = client.GetStream();
     while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
     {
          stream.Write(g1, 0, g1.Length);
     }
 }
 stream.close();
}

So whenever the server receive something from client, it must send the g1 bytes (it's just for testing purpose), but i get this error after the client connects to the server :

Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.

Any idea would be great, thank you

Soufiane Touil
  • 803
  • 2
  • 8
  • 17
  • found some related information:http://stackoverflow.com/questions/14304658/c-sharp-an-established-connection-was-aborted-by-the-software-in-your-host-machi, not sure if it will help you though. – Giorgi Moniava Jan 23 '16 at 20:58
  • How can you tell when you receive all the data from the server? Before you send any data you must wait until all the data is received and with TCP data may come in multiple messages. GetStream() method is blocking an won't return until the stream/connection is closed. So if you then send data you will get an error because the connection is closed. – jdweng Jan 23 '16 at 21:21

1 Answers1

1

I suggestTcpClient client = server.AcceptTcpClient(); NetworkStream stream = client.GetStream(); inside the while Loop. Create a new Thread for not blocking ui while receiving message

private void GetMessageThread()
     {
         bool receive = true;
         Stream strm = client.GetStream();
         while (receive)
         {
             try
             {
                IFormatter formatter = new BinaryFormatter();
                string obj;
                if (strm.CanRead)
                {
                    obj = (string)formatter.Deserialize(strm);
                }
                else
                {
                    _receive = false;
                    break;
                }
           }
         }
    }

In this case the BinaryFormatter knows when there is the end of stream, so you don't have to give Bytes.Length and you don't have to Close the stream after each message. You're error normally Comes up when Server or Client crashed or .Close() was called on TcpListener or TcpClient. Hope this helps you.

kilian eller
  • 184
  • 2
  • 16