Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Object kritiskSektion = new Object();
- bool aktiv = true;
- public static void DoBeginAcceptTcpClient()
- {
- while (aktiv)
- {
- // Set the event to nonsignaled state.
- tcpClientConnected.Reset();
- // Start to listen for connections from a client.
- Console.WriteLine("Waiting for a connection...");
- // Accept the connection.
- // BeginAcceptSocket() creates the accepted socket.
- server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback),
- server);
- // Wait until a connection is made and processed before
- // continuing.
- tcpClientConnected.WaitOne();
- }
- }
- public static void DoAcceptTcpClientCallback(IAsyncResult ar)
- {
- Console.WriteLine("DoAcceptTcpClientCallback started");
- // Get the listener that handles the client request.
- TcpListener listener = (TcpListener)ar.AsyncState;
- TcpClient client = listener.EndAcceptTcpClient(ar);
- // Lock clientList from other threads and add the new client to it
- lock (kritiskSektion)
- {
- clientList.Add(client);
- }
- // Process the connection here. (Add the client to a
- // server table, read data, etc.)
- Console.WriteLine("Client connect completed");
- // Signal the calling thread to continue.
- tcpClientConnected.Set();
- }
- // Bliver kaldt når brugeren lukker programmet
- static public void Close()
- {
- aktiv = false;
- acceptClientsThread.Abort();
- server.Stop();
- }
Advertisement
Add Comment
Please, Sign In to add comment