Socket programming question

S

sebastian.harko

Hello,
I have a question related to socket programming.
I have this code:

TcpClient client = new TcpClient(hostName, port);

try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string result = sr.ReadLine();
Console.Write(result);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}


And the thing is that it doesn't work ...
 
N

Nicholas Paldino [.NET/C# MVP]

You didn't mention ^how^ it didn't work. What were your expectations,
and how were they not met?

A few things to point out, you should really be using the using
statement for things that implement IDisposable (the Stream, StreamReader,
etc, etc).

Additionally, I believe the StreamReader performs some buffering. If
so, then it's possible that it is trying to read bytes that haven't been
sent on the stream yet.
 
S

sebastian.harko

Sorry ... it was an incomplete message.. I removed it and posted a new
message ....

You didn't mention ^how^ it didn't work. What were your expectations,
and how were they not met?

A few things to point out, you should really be using the using
statement for things that implement IDisposable (the Stream, StreamReader,
etc, etc).

Additionally, I believe the StreamReader performs some buffering. If
so, then it's possible that it is trying to read bytes that haven't been
sent on the stream yet.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hello,
I have a question related to socket programming.
I have this code:
TcpClient client = new TcpClient(hostName, port);
try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string result = sr.ReadLine();
Console.Write(result);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}
And the thing is that it doesn't work ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top