Simple question regarding TcpClient and TcpListener  
Author Message
motorola





PostPosted: .NET Framework Networking and Communication, Simple question regarding TcpClient and TcpListener Top

Hi all,

I am new to network programming and I already read some examples related to socket on the MSDN and now I am trying to create a message sender(server) and a message receiver(client) by using TcpClient and TcpListener.

The server simply contains a TcpListener object that runs the following code when it is started:

listener.Start()

client = listener.AcceptTcpClient()

The client contains a TcpClient object that is used to connect to the server:

client.Connect(IPAddress.Parse(127.0.0.1), 2828)

After the client gets connected to the server, sometime later, the server attempts to send a message to the client by using the following code:

Dim message As String="Hello World"

Dim encoding As New System.Text.ASCIIEncoding()

Dim bytes As Byte()

bytes = encoding.GetBytes(s)

Dim ns As NetworkStream=client.GetStream()

ns.Write(s, 0, bytes.Length)

Then the client needs some codes to receive this message, the problem is how the client can know WHEN the server sends it a message so that it can call the codes that will receive the message

Thank you very much for your help.



.NET Development20  
 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, Simple question regarding TcpClient and TcpListener Top

Receiver should always be in state of receiving data, Whenever data comes it should convert and display it.

You are using NetworkStream so using Synchronous Read you wil have something like this:

while(connected)

{

//ReadStream

//Show Message etc...

}

Mind this! This code needs to run under a seperate Thread or it'll halt the UI and You can only receive but not send anything becuase it's in that loon untill it is connected to server.

with Asynchronous you need to implement BeginRead() and EndRead() it willl not halt the UI and ofcourse you dont need thread here. BeginRead() will immidiately return the control and then when data is read from the stream control will be transfered to the call back function then u'll call EndRead() on stream and in that call back function u need to call BeginRead() again so it'll become a recursive path of execution!

Best Regards,



 
 
motorola





PostPosted: .NET Framework Networking and Communication, Simple question regarding TcpClient and TcpListener Top

Hi RizwanSharp,

Thank you very very much for your reply. It's very helpful.

So if I want to achieve asynchronous so that it won't occupy the main thread of the application, I can either run the while(connected) {...} loop under a seperated thread or I can implement BeginRead(), EndRead() and a callback function that contains code to read the stream

Thanks again.


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, Simple question regarding TcpClient and TcpListener Top

Yes you can use Asynchronous Read i.e BeginRead() EndRead() and it'll not occupy Main Thread please see MSDN for example and ask if u cant get it!

Cheers



 
 
motorola





PostPosted: .NET Framework Networking and Communication, Simple question regarding TcpClient and TcpListener Top

Hi RizwanSharp,

Thanks a lot.

After the message receiver(client) gets connected to the message sender(server), then by making the message receiver(client) always in a state of receiving message, the message receiver(client) can receive messages sent by the message sender(server) at any time.

If I want the client to be also able to send message at any time to the server so that both the client and the server now become 'message sender and receiver', then both ends would need to be always in a state of receiving message, is this correct