W
WP
Hello, I'm very new at C# and I'm writing a simple card game where two
players can play a simple card game against each other. Communication
is done through a server.
I'm using the TcpClient class and I'm trying to do asynchronous reads
using TcpClient.GetStream().BeginRead().
Anyway, my problem is that the first message sent to the client is
lost. This started to happen when I changed to reading what's on the
networkstream line by line. First I read whatever bytes where there
which worked then - I got each "message" alone and in its entirety but
as soon as I started to send messages fast they got mixed up in each
other. So I changed to reading and writing line by line but now the
first message is lost. An ugly workaround is of course to send the
very first message twice, that seems to work, but I'd like to fix it
properly.
Here's the client read callback:
private void OnReadMessage(IAsyncResult ar)
{
StreamReader sr = new StreamReader(this.tcp_client.GetStream());
String line;
do
{
line = sr.ReadLine();
this.tracebox.WriteLine(line); // Displays the message
} while (line != null);
int bytes_read = this.tcp_client.GetStream().EndRead(ar);
this.tcp_client.GetStream().BeginRead(this.message, 0,
this.message_length, new AsyncCallback(this.OnReadMessage), null);
}
Here's how the server sends a message:
public void SendMessage(String outgoing_message)
{
StreamWriter sr = new StreamWriter(this.tcp_client.GetStream());
sr.WriteLine(outgoing_message);
sr.Flush();
}
Let me know if you need to see any other code.
I looked at an example in my MSDN installation and there they call a
method called WaitOne() on an object they call tcpClientConnected (but
the example doesn't show what that type that object is). The comment
says: Wait until a connection is made and processed before continuing.
Hmm, that sounds like that could be my problem but I didn't need to do
that before and when I search for WaitOne the docs point me to
documentation for an abstract class I don't know how to use...
Thanks for reading and thanks in advance for any help!
- Eric
players can play a simple card game against each other. Communication
is done through a server.
I'm using the TcpClient class and I'm trying to do asynchronous reads
using TcpClient.GetStream().BeginRead().
Anyway, my problem is that the first message sent to the client is
lost. This started to happen when I changed to reading what's on the
networkstream line by line. First I read whatever bytes where there
which worked then - I got each "message" alone and in its entirety but
as soon as I started to send messages fast they got mixed up in each
other. So I changed to reading and writing line by line but now the
first message is lost. An ugly workaround is of course to send the
very first message twice, that seems to work, but I'd like to fix it
properly.
Here's the client read callback:
private void OnReadMessage(IAsyncResult ar)
{
StreamReader sr = new StreamReader(this.tcp_client.GetStream());
String line;
do
{
line = sr.ReadLine();
this.tracebox.WriteLine(line); // Displays the message
} while (line != null);
int bytes_read = this.tcp_client.GetStream().EndRead(ar);
this.tcp_client.GetStream().BeginRead(this.message, 0,
this.message_length, new AsyncCallback(this.OnReadMessage), null);
}
Here's how the server sends a message:
public void SendMessage(String outgoing_message)
{
StreamWriter sr = new StreamWriter(this.tcp_client.GetStream());
sr.WriteLine(outgoing_message);
sr.Flush();
}
Let me know if you need to see any other code.
I looked at an example in my MSDN installation and there they call a
method called WaitOne() on an object they call tcpClientConnected (but
the example doesn't show what that type that object is). The comment
says: Wait until a connection is made and processed before continuing.
Hmm, that sounds like that could be my problem but I didn't need to do
that before and when I search for WaitOne the docs point me to
documentation for an abstract class I don't know how to use...
Thanks for reading and thanks in advance for any help!
- Eric