tags:

views:

146

answers:

4

I am sending and receiving bytes between a server and a client. The server regularly sends some message in the form of bytes and client receives them.

Message format is below: {Key:Value,Key:Value,Key:Value}

Now at the client side instead of receiving this message, I am receiving multiple copies of this message which is not suitable for this.

The client is receiving like this: {Key:Value,Key:Value,Key:Value} {Key:Value,Key:Value,Key:Value} {Key:Value,Key:Value,Key:Value} {Key:Value,Key:Value,Key:Value} {Key:Value,Key:Value,Key:Value} {Key:Value,Key:Value,Key:Value} {Key:Value,Key:Value,

Can someone help me figure out the problem?

Updated


This code is sending instructions.

var client = (param as System.Net.Sockets.Socket);
            while (true)
            {
                try
                {
                    var instructions = "{";
                    instructions += "Window:" + window + ",";
                    instructions += "Time:" + System.DateTime.Now.ToShortTimeString() + ",";
                    instructions += "Message:" + msgToSend + "";
                    instructions += "}";

                    var bytes = System.Text.Encoding.Default.GetBytes(instructions);
                    client.Send(bytes, 0, bytes.Length, System.Net.Sockets.SocketFlags.None);
                }
                catch (Exception ex)
                {
                    continue;
                }
            }

This code is receiving at client side.

while (true)
            {
                try
                {
                    var data = new byte[tcpClient.ReceiveBufferSize];
                    stream.Read(data, 0, tcpClient.ReceiveBufferSize);
                    instructions = System.Text.Encoding.Default.GetString(data.ToArray());
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
+1  A: 

You've got:

while (true)

In the sender: it's just going to keep sending the same thing over and over...

Also, if you get an exception trying to send or receive the data, you can't just try again and expect it to work. Depending on the exact error, you might need to reestablish the connection, or it might be that the network has gone away completely. In any case, simply retrying again is almost always going to be the wrong thing to do.

Dean Harding
Yes. Is there a way I can send data to the client and then client will ask for next instruction?
Umair Ashraf
+5  A: 

Okay, a few problems with this code:

  • You're using Encoding.Default, which is almost certainly not what you want to do
  • You're always decoding the whole string, rather than just the amount you've actually managed to read - you're ignoring the return value of stream.Read
  • You're just continuing after an exception, with no logging, error handling or anything
  • As Dean says, you're repeatedly sending the same data

Ideally, it would be useful for your messages to have a prefix saying how long each one is, in bytes. Then in the receiving side you can read that length, then loop to repeatedly read into a buffer until you've read all the data you need. Then perform the decoding.

If you can't change the protocol, you'll still need to loop round, but checking for the end delimiter ("}" presumably) explicitly - and noting that you may receive data from the next message which you'll have to store until you next want to read.

Jon Skeet
Good point on the decoding the whole array. You also cannot assume that you've received a complete message, so some kind of buffering until a complete message (taking into account you might even get 1 1/2 messages in a single call to `socket.Read`) is received will be required.
Dean Harding
A: 

Problem has figured out like Dean Harding said.
But beside you should be more clearly about "client" or "server".
Basicaly:
  Only server side should wait (by a loop) for msgs. Client (sender) sends msgs when needed or in condition.
You can sending msg in loop but should control and regulate it by a "Sleep" or "Timer". In this way, you can spare resource and give more time for receiver can process msg completely.

pinichi
A: 

Your are sending your data through TCP. TCP is a stream-oriented protocol, so you know the client will receive the same stream of bytes in the same order, but you loose the packet boundaries. Your protocol seems to be packet-oriented instead. Then you have the choice:

  • switch to a packet-oriented protocol (UDP) or
  • delimit the packets yourself at the receiving side (as Jon Skeet said, by looking for the delimiters).

Keep in mind that TCP has some reliability features not found in UDP. If reliability is not a concern, switch to UDP. Otherwise, finding the delimiters at the client side should be easier than implementing your own reliability layer.

Edgar Bonet