Page 135 - CSharp/C#
P. 135
prevent anyone from stealing your package.
Examples
Asynchronous Socket (Client / Server) example.
Server Side example
Create Listener for server
Start of with creating an server that will handle clients that connect, and requests that will be send.
So create an Listener Class that will handle this.
class Listener
{
public Socket ListenerSocket; //This is the socket that will listen to any incoming
connections
public short Port = 1234; // on this port we will listen
public Listener()
{
ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
}
}
First we need to initialize the Listener socket where we can listen on for any connections. We are
going to use an Tcp Socket that is why we use SocketType.Stream. Also we specify to witch port
the server should listen to
Then we start listening for any incoming connections.
The tree methods we use here are:
1. ListenerSocket.Bind();
This method binds the socket to an IPEndPoint. This class contains the host and local or
remote port information needed by an application to connect to a service on a host.
2. ListenerSocket.Listen(10);
The backlog parameter specifies the number of incoming connections that can be queued for
acceptance.
3. ListenerSocket.BeginAccept();
The server will start listening for incoming connections and will go on with other logic. When
there is an connection the server switches back to this method and will run the
AcceptCallBack methodt
public void StartListening()
https://riptutorial.com/ 81

