socket programming in C#

  • Thread starter Thread starter sebastian.harko
  • Start date Start date
S

sebastian.harko

Hello,
I have a question on socket programming in C#.
I have this code and it doesn't work correctly.

It sends the message correctly but it does not get the message that
the server sends back ( the program bellow gets blocked ). I know for
sure
that the server sends back a message ( I'm using a traffic sniffer and
I can
see what's being sent and received ) .'
....
So what's wrong here ?

best regards


TcpClient client = new TcpClient(hostName, pacsPort);

try
{
NetworkStream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string response = sr.ReadLine(); // program blocks
here
Console.Write(response);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}
 
Hello,
I have a question on socket programming in C#.
I have this code and it doesn't work correctly.

It sends the message correctly but it does not get the message that
the server sends back ( the program bellow gets blocked ). I know for
sure
that the server sends back a message ( I'm using a traffic sniffer and
I can
see what's being sent and received ) .'
...
So what's wrong here ?

Does the server include a carriage return or line feed?

Jon
 
Hi,

I don't know ... How could I check and what could I do about it ?

The problem is that you are decorating the NetworkStream with a
StreamReader, ReadLine() blocks and keep reading until an
Environment.NewLine string is received, if it's never received it will be
blocked for ever.

Do this, read one byte at a time and dump it in a StringBuilder or some
similar structure. Also you must know if the source is sending UNICODE (2
bytes per char) or ASCII (1 byte per char).
 
Do this, read one byte at a time and dump it in a StringBuilder or some
similar structure. [...]

For testing purposes, this might be appropriate. Otherwise, no...don't do
this.

It is unclear from the original post exactly what the data is to look
like. If it _will_ include a CR/LF to terminate a line, then using
ReadLine() isn't all that bad. If the format of the data is unknown,
other than knowing for sure that it's some kind of character format, then
using the Read() method would be better. You will have to loop, as Read()
will return each time there's _some_ data that's been received.

This all assumes that the character encoding for the stream is the same on
both ends, of course, as Ignacio points out.

Pete
 
Hello,
I have a question on socket programming in C#.
I have this code and it doesn't work correctly.
It sends the message correctly but it does not get the message that
the server sends back ( the program bellow gets blocked ). I know for
sure
that the server sends back a message ( I'm using a traffic sniffer and
I can
see what's being sent and received ) .'
....
So what's wrong here ?
best regards
TcpClient client = new TcpClient(hostName, pacsPort);
try
{
NetworkStream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string response = sr.ReadLine(); // program blocks
here
Console.Write(response);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}
<hr>

You want to connect to the client before you attach the streamreader and -writer. Besides that it is good practice to set the encoding of the streamreader to ASCII.

Leon
 
Back
Top