Help need on TcpClient

N

Nobody

Hi all,

I try to write a small client that can handle some TCP communication
message.

I was wondering how I could use the TcpClient class to manage it.

At the first time, I don't want to play with the async methods. My main
issue is related about receiving informations back from the server. How can
I be sure that the server has finished sending the response? Should I set a
read timeout and catch the exception when it times out? How large should be
my buffer? Any sample?

Thanks a lot in advance
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Nobody said:
I was wondering how I could use the TcpClient class to manage it.

Of course, that is reason of its existence.
At the first time, I don't want to play with the async methods. My main
issue is related about receiving informations back from the server. How
can I be sure that the server has finished sending the response? Should I
set a read timeout and catch the exception when it times out? How large
should be my buffer? Any sample?

Your buffer size does not really matter, cause when you read the data you
especify how big it's.
You will have to check the readed unmber of bytes ( the return value) to
know how many bytes were read.

Also there is no way to knowing when the server finished sending data. For
that you may use different approaches like sending the size of the data
before sending the data.

Can you define the protocol?
 
N

Nobody

Can you define the protocol?

First, thanks for your response.

My first goal is to create a simple telnet wrapper, say in a console, able
to send and receive synchronously.

From the test I made, TcpClient.Available is not reliable. I used it in a
while loop and had times I was out the loop but with more data available.
Same problem with NetworkStream.DataAvailable.

My first goal is to build a simle telnet console client. Here's a sample
code. Feel free to correct me :)

---8<---
static void Main(string[] args)
{
TcpClient client = new TcpClient("msnews.microsoft.com", 119);
Read(client);

while (client.Connected)
{
Console.Write("C: ");
string command = Console.ReadLine();

if (!String.IsNullOrEmpty(command))
{
Send(client, command);
Read(client);
}
}

Console.WriteLine("Press any key...");
Console.Read();
}

private static void Read(TcpClient client)
{
NetworkStream stream = client.GetStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int bytesRead = stream.Read(buffer, 0, bufferSize);

Console.WriteLine("S: " + Encoding.ASCII.GetString(buffer, 0,
bytesRead).Trim('\0', '\r','\n'));
}

private static void Send(TcpClient client, string command)
{
NetworkStream stream = client.GetStream();
byte[] bytes = Encoding.ASCII.GetBytes(command + "\r\n");

stream.Write(bytes, 0, bytes.Length);
}
---8<---
 
A

Abubakar

Hi,

What server are you talking about? A web server or just a custom made
chat-kind-of server? Anyway the way I used to do once was based on the fact
that the read method of NetworkStream class (returned from tcpclient class i
think) gives us a blocking call. This means I can create a separate thread
( lets call it a mychat thread) and go in an infinite loop calling a Read
method. Whenever there is a response, I get the length of that response too
(as a read method return value) so I allocate the required buffer, read it
and the pass it to the GUI for an update, than go back into the blocking
call. In case the other end gets disconnected we are notified of it too
although I dont remember if its judged by a negative return value or an
exception.

Its something that I remember when I once made an app using .net sockets. I
hope it helps.

regards,
..ab
 

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