473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Help need on TcpClient

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
Mar 22 '07 #1
5 4782
Check out the following:
http://samples.gotdotnet.com/quickst...imeClient.aspx

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Nobody" <no****@nowhere.orgwrote in message
news:uX**************@TK2MSFTNGP04.phx.gbl...
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

Mar 22 '07 #2

"Kevin Spencer" <un**********@nothinks.comwrote in message
news:ev**************@TK2MSFTNGP02.phx.gbl...
Check out the following:
http://samples.gotdotnet.com/quickst...imeClient.aspx
Thanks but it doesnt reply my question. Maybe I should explain it better.

Regarding the sample on the website you gave me the url, what if the
response from the server is larger than 32 bytes? How to manage response
that have unpredictable length?
Mar 22 '07 #3
Hi,

"Nobody" <no****@nowhere.orgwrote in message
news:uX**************@TK2MSFTNGP04.phx.gbl...
Hi all,
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?
Mar 22 '07 #4
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<---
Mar 22 '07 #5
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

"Nobody" <no****@nowhere.orgwrote in message
news:uX**************@TK2MSFTNGP04.phx.gbl...
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

Mar 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom Rahav | last post by:
Hello All! I need to extract Socket object from a TcpClient object, in order to get client's IP address. I've found the following article that describes how to derive from TcpClient class and...
3
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work...
2
by: mb | last post by:
got the following code snippet that i've modified off the internet to work - but damn if i know how and i dont like that. im having trouble with the following thread declaration that creates the...
2
by: Tom Rahav | last post by:
Hello All! I need to extract Socket object from a TcpClient object, in order to get client's IP address. I've found the following article that describes how to derive from TcpClient class and...
0
by: Torsten Brasch | last post by:
Hi All and Happy New Year ;) I have a very strange problem with System.Net.Sockets.TcpClient(). For some reason, the number of bytes I can receive is limited to 5460 bytes. I made sure that the...
1
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client...
5
by: Jason L James | last post by:
Hi all, as a VB.Net programmer I am finding it difficult to find a text on implementing sockets to communicate with an embedded web server. Does anyone know of a good resource, either text...
2
by: Terry Olsen | last post by:
Hoping someone can help me here. I've got this code written, and it works fine for the first connection. But if I connect another client (while the first is still connected), I get connected but...
2
by: Bonzol | last post by:
vb.net 2003 Windows application We have a Client/Server system set up by communicating through a TCPClient object. The server loops continuously using a tcplistener device and each time a client...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.