none
A little problem sending message to server

    Вопрос

  • 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?
    7 марта 2012 г. 9:09

Ответы

  • 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



    • Изменено Peter Koueik 7 марта 2012 г. 11:05
    • Помечено в качестве ответа Neddy Ren 14 марта 2012 г. 9:47
    7 марта 2012 г. 10:58
  • 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

    • Предложено в качестве ответа Neddy Ren 9 марта 2012 г. 9:10
    • Помечено в качестве ответа Neddy Ren 14 марта 2012 г. 9:47
    7 марта 2012 г. 11:10

Все ответы

  • 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

    7 марта 2012 г. 9:13
  • 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 :/?
    7 марта 2012 г. 9:46
  • 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

    7 марта 2012 г. 9:57
  • 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

    7 марта 2012 г. 10:12
  • 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.
    7 марта 2012 г. 10:29
  • 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



    • Изменено Peter Koueik 7 марта 2012 г. 11:05
    • Помечено в качестве ответа Neddy Ren 14 марта 2012 г. 9:47
    7 марта 2012 г. 10:58
  • 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 :/


    • Изменено Jarzka 7 марта 2012 г. 11:06
    7 марта 2012 г. 11:05
  • 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

    • Предложено в качестве ответа Neddy Ren 9 марта 2012 г. 9:10
    • Помечено в качестве ответа Neddy Ren 14 марта 2012 г. 9:47
    7 марта 2012 г. 11:10
  • Well let's see about that. I will try it later. Thanks for help :)
    • Предложено в качестве ответа Jain Mehul 7 марта 2012 г. 14:22
    • Отменено предложение в качестве ответа Jain Mehul 7 марта 2012 г. 15:59
    7 марта 2012 г. 11:13
  • Try using AutoFlush = true of the networkStream
    7 марта 2012 г. 16:00