Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving messages as TcpClient

I have been following this tutorial "http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server" on setting up a mini server that can send and receive messages and have multiple clients connected.

Everything is working which is great.. but unfortunately one thing that is missing in this tutorial is how the client can set up a listener to listen to the server.

I have only this much:

public void SetupReceiver()
{
      TcpClient tcpClient = new TcpClient(this.Host, this.Port);
      NetworkStream networkStream = tcpClient.GetStream();

      // What next! :( or is this already wrong...
}

As far as I can imagine.. I would need to connect to the server (As a TcpClient) and get the stream (Like above). And then wait for messages and do something with it. The reason I cannot just have the client receive a message back from the server immediately after sending one is because the client will send a message to the server, and then that message will be broadcast to all the clients that are connected. So each client needs to be "listening" for messages from the server.

like image 659
Zephni Avatar asked Mar 16 '23 14:03

Zephni


1 Answers

The TCPclient class has the necessary resources to enable a connection, send and receive data to and from the server and the TCPListener class is essentially the server.

Following the general example provided in the msdn page for TCPclient and can also be used for TCPListener (of which my generalised explanation is based on!)

https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.110%29.aspx

The first part is to send data to the server:

// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         

// Get a client stream for reading and writing. 
NetworkStream stream = client.GetStream();

// Send the message to the connected TcpServer. 
stream.Write(data, 0, data.Length); //(**This is to send data using the byte method**)   

The following part is to receive data from the server:

// Buffer to store the response bytes.
data = new Byte[256];

// String to store the response ASCII representation.
String responseData = String.Empty;

// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length); //(**This receives the data using the byte method**)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); //(**This converts it to string**)

The byte method can be replaced with streamreader and streamwriter once they have been linked to the networkstream

Hope this helps!!

**PS: If you would like a more versatile coding experience with networking classes in c#, i would personally recommend looking into using sockets as it is the main class from which the tcpclient and tcplistener are born.

like image 96
Viralwarrior012 Avatar answered Mar 18 '23 20:03

Viralwarrior012