Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could implement a class that encapsulates TCP behavior. Check this class:</p> <pre><code>public class SimpleListener { private System.Net.Sockets.TcpListener _tcpListen; //declare delegate to handle new connections public delegate void _new_client(System.Net.Sockets.TcpClient tcpclient); //declare a clients container (or something like...). OPTION 1 List&lt;System.Net.Sockets.TcpClient&gt; _connected_clients; //declare an event and event handler (the same for _new_client) for new connections OPTION 2 public event _new_client new_tcp_client; //public (The list of connected clients). public List&lt;System.Net.Sockets.TcpClient&gt; ConnectedClients { get { return _connected_clients; } } public SimpleListener(string ip, int listenport) { System.Net.IPAddress ipAd = System.Net.IPAddress.Parse(ip); _tcpListen = new System.Net.Sockets.TcpListener(new System.Net.IPEndPoint(ipAd,listenport)); _connected_clients = new List&lt;System.Net.Sockets.TcpClient&gt;(); } //Fire this method to start listening... public void Listen() { _tcpListen.Start(); _set_listen(); } //... and this method to stop listener and release resources on listener public void Stop() { _tcpListen.Stop(); } //This method set the socket on listening mode... private void _set_listen() { //Let's do it asynchronously - Set the method callback, with the same definition as delegate _new_client _tcpListen.BeginAcceptTcpClient(new AsyncCallback(_on_new_client), null); } //This is the callback for new clients private void _on_new_client(IAsyncResult _async_client) { try { //Lets get the new client... System.Net.Sockets.TcpClient _tcp_cl = _tcpListen.EndAcceptTcpClient(_async_client); //Push the new client to the list _connected_clients.Add(_tcp_cl); //OPTION 2 : Fire new_tcp_client Event - Suscribers will do some stuff... if (new_tcp_client != null) { new_tcp_client(_tcp_cl); } //Set socket on listening mode again... (and wait for new connections, while we can manage the new client connection) _set_listen(); } catch (Exception ex) { //Do something...or not } } } </code></pre> <p>You could use this in your code:</p> <pre><code> //SERVER // instantiate variables such as tempIp, port etc... // ... // ... SimpleListener server = new SimpleListener(tempIp, port); //tempIp is the ip address of the server. //add handler for new client connections server.new_tcp_client += server_new_tcp_client; // Start listening for client requests. server.Listen(); .... //No need to loop. The new connection is handled on server_new_tcp_client method void server_new_tcp_client(System.Net.Sockets.TcpClient tcpclient) { // Buffer for reading data Byte[] bytes = new Byte[MaxChunkSize]; String data = null; Console.WriteLine("Connected!"); // Get a stream object for reading and writing System.IO.Stream stream = tcpclient.GetStream(); // now that the connection is established start listening though data // sent through the stream.. int i; try { // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); // etc.. .... } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload