A little problem sending message to server

Answered A little problem sending message to server

  • Wednesday, March 07, 2012 9:09 AM
     
      Has Code

    Hi

    I'm trying to develop a simple console chat application. It works fine but here is a little problem. Here is a method that sends message to the server:

    private bool SendMessageToServer(string message)
    {
    	byte[] buffer = unicodeEncoding.GetBytes(message);
    
    	try
    	{
    		networkStream.Write(buffer, 0, buffer.Length);
    		networkStream.Flush();
    	}
    	catch (Exception e)
    	{
    		return false;
    	}
    
    	return true;
    }
    The problem is that if I call this method too often the message gets merged. For example I loop this method 10 times and each time I want to send single message "Hello". However the final result is "HelloHelloHelloHello...". If I pause the thread for some time (Thread.Sleep(10)) it works fine but I'm wondering why this happens and is there better way to fix it?

All Replies

  • Wednesday, March 07, 2012 9:13 AM
     
     
    you should make the call of the function asynchronous...
    check these links:
    http://support.microsoft.com/kb/315582
    http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx

    Peter Koueik

  • Wednesday, March 07, 2012 9:46 AM
     
      Has Code

    I tried to call the function like this:

    AsyncSendMessageToServer aSyncSendMessageToServer = new AsyncSendMessageToServer(SendMessageToServer);
    AsyncSendMessageToServer aSyncSendMessageToServer2 = new AsyncSendMessageToServer(SendMessageToServer);
    aSyncSendMessageToServer.BeginInvoke("Hello", null, null);
    aSyncSendMessageToServer2.BeginInvoke("Hello", null, null);
    But it still sends HelloHello :/?
  • Wednesday, March 07, 2012 9:57 AM
     
     

    suppose you have that loop your talking about:
    AsyncSendMessageToServer aSyncSendMessageToServer = new AsyncSendMessageToServer(SendMessageToServer);
    for (int i =0; i<10; i++)
    {

    aSyncSendMessageToServer.BeginInvoke("Hello", null, null);

    }try this...


    Peter Koueik

  • Wednesday, March 07, 2012 10:12 AM
     
      Has Code

    Hi,

    try this:

    byte[] buffer = unicodeEncoding.GetBytes(message);
    
    	try
    	{
    		using(BinaryFormatter bForm = new BinaryFotmatter()
    {
    bForm.Serialize(networkStream, buffer);
    }
    	}
    	catch (Exception e)
    	{
    		return false;
    	}

    use BinaryFormatter.Deserialize on your server side for deserializing the message. You can also serialize the entire string instead of byte array. ex:

    bForm.Serialize(networkStream, message);

    and deserialize using

    string message = (string)bForm.Serialize(networkStream);


    Bilhan silva

  • Wednesday, March 07, 2012 10:29 AM
     
     

    suppose you have that loop your talking about:
    AsyncSendMessageToServer aSyncSendMessageToServer = new AsyncSendMessageToServer(SendMessageToServer);
    for (int i =0; i<10; i++)
    {

    aSyncSendMessageToServer.BeginInvoke("Hello", null, null);

    }try this...


    Peter Koueik

    Still sends HelloHelloHello... for some reason.
  • Wednesday, March 07, 2012 10:58 AM
     
     Answered

    you have to add something to mark the end of the stream.

    check this:
    http://stackoverflow.com/questions/8270989/synchronization-logic-in-using-network-stream


    Peter Koueik



  • Wednesday, March 07, 2012 11:05 AM
     
     

    Yes. That is the message the servers get from the user.

    However one way to fix this would be adding some kind of suffix to the end of the message, like EOM. When the server gets the message, it would be something like this:

    helloEOMhelloEOMhelloEOM

    or if we send something else:

    hiEOMhelloEOMHowdyEOM

    It could split the message using EOM as the separator and get the final result:

    hi
    hello
    Howdy

    This would be one way to fix the problem. However I don't understand why the NetworkStream does not send the message immediately. It seems that it waits a little bit until someone calls the SendMessage method again and it just adds the new message to the end of the old message and then sends it. And the result is OldmessageNewmessage :/


    • Edited by Jarzka Wednesday, March 07, 2012 11:06 AM
    •  
  • Wednesday, March 07, 2012 11:10 AM
     
     Answered

    you have to add something to mark the end of the stream.

    check this:
    http://stackoverflow.com/questions/8270989/synchronization-logic-in-using-network-stream

    no actually it sends the message immediately but if you don't delimit the end of the stream it will append the following result to the end ...

    Peter Koueik

  • Wednesday, March 07, 2012 11:13 AM
     
     
    Well let's see about that. I will try it later. Thanks for help :)
    • Proposed As Answer by Jain Mehul Wednesday, March 07, 2012 2:22 PM
    • Unproposed As Answer by Jain Mehul Wednesday, March 07, 2012 3:59 PM
    •  
  • Wednesday, March 07, 2012 4:00 PM
     
     
    Try using AutoFlush = true of the networkStream