Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace ConsoleApp1
  11. {
  12.     public class Server
  13.     {
  14.         IPAddress ipAd;
  15.         bool running = false;
  16.         public Server()
  17.         {
  18.  
  19.         }
  20.  
  21.         public IPAddress IpAd { get => ipAd; set {
  22.                 if (running) ipAd = value;
  23.                 else throw new Exception("Server is running. You can't change the IP address.");
  24.             } }
  25.  
  26.         public void server()
  27.         {
  28.             try
  29.             {
  30.                 ipAd = IPAddress.Parse("127.0.0.1");
  31.  
  32.                 TcpListener myList = new TcpListener(IpAd, 8001);
  33.  
  34.                 myList.Start();
  35.                 running = true;
  36.                 Console.WriteLine("The server is running at port 8001");
  37.                 Console.WriteLine("Local end point: " +
  38.                                   myList.LocalEndpoint);
  39.                 Console.WriteLine("Waiting for a connection...");
  40.                 while (true)
  41.                 {
  42.                     TcpClient client = myList.AcceptTcpClient();
  43.                     if(client != null)
  44.                     {
  45.                         Console.WriteLine("Client connected!");
  46.                     }
  47.                     ThreadPool.QueueUserWorkItem(IO.ClientThread.ThreadProc, client);
  48.                     System.Threading.Thread.Sleep(1000);
  49.                 }
  50.  
  51.             }
  52.             catch (Exception e)
  53.             {
  54.                 Console.WriteLine("Error... " + e.StackTrace);
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement