Advertisement
MagnusArias

PS1 | Serwer Echo (MultiThread)

Apr 25th, 2017
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 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.Windows.Forms;
  9.  
  10. namespace MultiThreadServerEcho
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private TcpListener tcpListener;
  15.         private Thread listenThread;
  16.         private bool _isRunning = false;
  17.         delegate void SetTextCallback(string text);
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.             Console.WriteLine("Initialized in thread");
  23.         }
  24.  
  25.         public int ServerStart()
  26.         {
  27.             var port = 7;
  28.  
  29.             try { port = int.Parse(this.portBox.Text); }
  30.             catch (FormatException) { port = 7; }
  31.  
  32.             tcpListener = new TcpListener(IPAddress.Any, port);
  33.             listenThread = new Thread(new ThreadStart(ListenForClients));
  34.             listenThread.Start();
  35.             listenThread.Join();
  36.             _isRunning = true;
  37.             return 0;
  38.         }
  39.  
  40.         private void ListenForClients()
  41.         {
  42.             try
  43.             {
  44.                 tcpListener.Start();
  45.                 Console.WriteLine("Listener started");
  46.             }
  47.             catch (SocketException e)
  48.             {
  49.                 Console.WriteLine("Server could not start" + e.Message);
  50.                 this.SetTextOnListBox("server could not start, error of binding");
  51.                 return;
  52.             }
  53.             Console.WriteLine("Server runnin on port " + portBox.Text);
  54.             this.SetTextOnListBox("server run on port ");
  55.  
  56.             while (true)
  57.             {
  58.                 Console.WriteLine("IN while");
  59.                 TcpClient client = tcpListener.AcceptTcpClient();
  60.                 Console.WriteLine("accepted client");
  61.                 this.SetTextOnListBox("new clinect connected from: " + client.Client.RemoteEndPoint);
  62.                 Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
  63.                 clientThread.Start(client);
  64.             }
  65.         }
  66.  
  67.         private void HandleClientComm(object client)
  68.         {
  69.             TcpClient tcpClient = (TcpClient)client;
  70.             String clientIP = "" + tcpClient.Client.RemoteEndPoint.AddressFamily;
  71.             NetworkStream clientStream = tcpClient.GetStream();
  72.  
  73.             int size = 2014;
  74.             byte[] message = new byte[size];
  75.             int bytesRead;
  76.             ASCIIEncoding encoder = new ASCIIEncoding();
  77.  
  78.             while (true)
  79.             {
  80.                 bytesRead = 0;
  81.                 try
  82.                 {
  83.                     bytesRead = clientStream.Read(message, 0, size);
  84.                     clientStream.Write(message, 0, bytesRead);
  85.                 }
  86.                 catch { break; }
  87.  
  88.                 if (bytesRead == 0) break;
  89.  
  90.                 this.SetTextOnListBox("   Message from " + tcpClient.Client.RemoteEndPoint + ": " + encoder.GetString(message, 0, bytesRead));
  91.                
  92.             }
  93.             tcpClient.Close();
  94.         }
  95.  
  96.        
  97.         private void SetTextOnListBox(string text)
  98.         {
  99.             if (this.listBox1.InvokeRequired)
  100.             {
  101.                 this.BeginInvoke(new SetTextCallback(SetTextOnListBox), new object[] { text });
  102.                 //return 0;
  103.             }
  104.             else
  105.             {
  106.                 this.listBox1.Items.Insert(0, text);
  107.             }
  108.         }
  109.  
  110.  
  111.  
  112.         private void startButton_Click(object sender, EventArgs e)
  113.         {
  114.             isServerRunning = !isServerRunning;
  115.  
  116.             if (isServerRunning) ServerStart();
  117.         }
  118.  
  119.         public bool isServerRunning
  120.         {
  121.             get
  122.             {
  123.                 return _isRunning;
  124.             }
  125.             set
  126.             {
  127.                 _isRunning = value;
  128.                 startButton.Text = _isRunning ? "Stop" : "Start";
  129.             }
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement