A little problem sending message to server
-
7 марта 2012 г. 9:09
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:13you 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.aspxPeter Koueik
-
7 марта 2012 г. 9:46
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:57
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 г. 10:12
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:29
Still sends HelloHelloHello... for some reason.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 г. 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
Peter Koueik
- Изменено Peter Koueik 7 марта 2012 г. 11:05
- Помечено в качестве ответа Neddy RenModerator 14 марта 2012 г. 9:47
-
7 марта 2012 г. 11:05
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
HowdyThis 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:10
you have to add something to mark the end of the stream.
check this:
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 ...
http://stackoverflow.com/questions/8270989/synchronization-logic-in-using-network-streamPeter Koueik
- Предложено в качестве ответа Neddy RenModerator 9 марта 2012 г. 9:10
- Помечено в качестве ответа Neddy RenModerator 14 марта 2012 г. 9:47
-
7 марта 2012 г. 11:13Well 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 г. 16:00Try using AutoFlush = true of the networkStream

