Click here to Skip to main content
15,846,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have created a Client side socket Application that listens for messages from a server, and saves them into the database.
I've used Asynchronous client sockets to receive the text being sent from the server.
The problem i am facing appears that i am not receiving all the messages (serversends about 5-10 messages a second, whereby i reply with a message from the client back to the server. For each message the server sends, it is expecting a response back from the client and if no response was received in 15 seconds the server sends a second message to the client to inform no response was received. I am responding to all my messages from the client side but appears 2/10 messages i am missing the initial message from the server.
The basic code i used is similar to :
http://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx

Any advise will be appreciated

Update:
I have also tried TCPClient, but having the same affect:
C#
Int32 port = 7003;
              TcpClient client = new TcpClient("xxx.xxx.xxx.xxx", port);
              IPAddress ipAddress = System.Net.IPAddress.Parse("xxx.xxx.xxx.xxx");
              IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
              // Translate the passed message into ASCII and store it as a Byte array.
              Byte[] data = System.Text.Encoding.ASCII.GetBytes("connect1111");

              // Get a client stream for reading and writing.
              //  Stream stream = client.GetStream();
              NetworkStream stream = client.GetStream();

              // Send the message to the connected TcpServer.
              stream.Write(data, 0, data.Length);

              Console.WriteLine("Sent: {0}", "connect1111");

              // Receive the TcpServer.response.

              // Buffer to store the response bytes.
              data = new Byte[8192];

              // String to store the response ASCII representation.
              String responseData = String.Empty;
              while (true)
              {
                  // Read the first batch of the TcpServer response bytes.
                  Int32 bytes = stream.Read(data, 0, data.Length);
                  responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                  Console.WriteLine("Received: {0}", responseData);
                  //*************************************
                  //Here i send a response back again using an unique code that was
                  //received
                  //*************************************
              }
Posted
Updated 18-Dec-14 10:56am
v3
Comments
virang_21 18-Dec-14 17:09pm    
May be it has to do with the speed at which your server is writing data to NetworkStream. 5 to 10 messages a second. It may be the case that before your client read the data server has already appended the NetworkSteam with next message.
Ideally you want
Sever Send
Client Read
Client Ack
Sever Send
Client Read
Client Ack

but what may be happening is
Server Send Msg 1
Client Read Msg 1
Server Send Msg 2
Client Send Act 1
Server Send Msg 3
Client Read Msg 2 & Msg 3 together from Stream
JP Veldtman 18-Dec-14 23:47pm    
This is exactly the problem, but how do i work around (like adding threading or something) to the Receive section, or lets say to build to wait for 10 responses and the server triggers one of them and 9 is still available to receive data immediately? I don't have control over the messages i am receiving
virang_21 19-Dec-14 0:02am    
One solution is if your message that you are getting from server is of fixed size and you receive two message together then split the message and send two acknowledgements to the sever.
JP Veldtman 19-Dec-14 0:08am    
Thanks, the messages are not a fixed size. But with sockets i know it will combine messages, which i am handling already. But I can see messages that was send(from the server that i am waiting for messages) which i never receive on my side. Hope it all makes sense
virang_21 19-Dec-14 0:20am    
Maybe server is flushing the buffer on acknowledgement before putting next message in the buffer. In that case

Server Send Msg1
Client Read Msg1
Server Send Msg2
Client Send Ack1
Server Flush Buffer and Send Msg 3
Client Read Msg3 and missed Msg2

This is just a guess

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900