July 2017
Beginner to intermediate
390 pages
10h 53m
English
We need to handle the incoming connections. To do this, we need to use the following code:
public class ConnectionHandler
{
TcpListener _listener;
public void HandleConnections(object dataToPass)
{
_listener = dataToPass as TcpListener;
if (_listener == null)
return;
_listener.Start();
Console.WriteLine("We are waiting for a connection");
// More code follows here
}
}
This is the start of our ConnectionHandler class. We have a private field listener of type TcpListener. We have the HandleConnections method with a parameter called dataToPass. Since we call this from main and give it the TcpListener, we can cast that parameter to the right type and store it. Then we tell the listener to start listening.
Read now
Unlock full access