T
Tomasz J
Hello developers,
I am trying to create a simple TCP chat-like communicator.
It will communicate with one peer only sending and receiving messages on the
same port. I guess, this requirement is important. Once the communication is
established it will not receive any other calls, so, I guess, I do not have
to use TcpListener.
I created a TcpClient object. In a infinite a loop running on a separate
thread I try to read incoming data using BinaryReader (cross-thread
communication is handled). Whenever I want to send a message I do it
synchronously on the main thread using the same TcpClient object and
BinaryWriter.
First of all, I am not sure if approach the problem the right way. This is
my first application of that sort.
Second, trying to read incoming data often I receive exceptions like:
A first chance exception of type 'System.IO.EndOfStreamException' occurred
in mscorlib.dll
at System.IO.__Error.EndOfFile()
at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
at System.IO.BinaryReader.ReadUInt32()
Perhaps, I am doing something wrong.
Could anyone advise? Not sure if this is important: I use .Net Framework
3.5.
I will appreciate any hints.
Thomas
The code goes more or less like this:
TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork);
tcpClient.Connect(someEndPoint);
NetworkStream clientSockStream = tcpClient.GetStream();
BinaryReader reader = new BinaryReader(clientSockStream);
BinaryWriter writer = new BinaryWriter(clientSockStream);
Thread listenerThread;
listenerThread = new Thread(new ThreadStart(ListenToPeer));
listenerThread.Start();
void SendSomeData(int myData) {
writer.Wtite(myData);
}
void ListenToPeer()
{
while(true) {
int data = reader.ReadInt32();
// process data here
}
}
I am trying to create a simple TCP chat-like communicator.
It will communicate with one peer only sending and receiving messages on the
same port. I guess, this requirement is important. Once the communication is
established it will not receive any other calls, so, I guess, I do not have
to use TcpListener.
I created a TcpClient object. In a infinite a loop running on a separate
thread I try to read incoming data using BinaryReader (cross-thread
communication is handled). Whenever I want to send a message I do it
synchronously on the main thread using the same TcpClient object and
BinaryWriter.
First of all, I am not sure if approach the problem the right way. This is
my first application of that sort.
Second, trying to read incoming data often I receive exceptions like:
A first chance exception of type 'System.IO.EndOfStreamException' occurred
in mscorlib.dll
at System.IO.__Error.EndOfFile()
at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
at System.IO.BinaryReader.ReadUInt32()
Perhaps, I am doing something wrong.
Could anyone advise? Not sure if this is important: I use .Net Framework
3.5.
I will appreciate any hints.
Thomas
The code goes more or less like this:
TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork);
tcpClient.Connect(someEndPoint);
NetworkStream clientSockStream = tcpClient.GetStream();
BinaryReader reader = new BinaryReader(clientSockStream);
BinaryWriter writer = new BinaryWriter(clientSockStream);
Thread listenerThread;
listenerThread = new Thread(new ThreadStart(ListenToPeer));
listenerThread.Start();
void SendSomeData(int myData) {
writer.Wtite(myData);
}
void ListenToPeer()
{
while(true) {
int data = reader.ReadInt32();
// process data here
}
}