Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace MultiThreadServerEcho
- {
- public partial class Form1 : Form
- {
- private TcpListener tcpListener;
- private Thread listenThread;
- private bool _isRunning = false;
- delegate void SetTextCallback(string text);
- public Form1()
- {
- InitializeComponent();
- Console.WriteLine("Initialized in thread");
- }
- public int ServerStart()
- {
- var port = 7;
- try { port = int.Parse(this.portBox.Text); }
- catch (FormatException) { port = 7; }
- tcpListener = new TcpListener(IPAddress.Any, port);
- listenThread = new Thread(new ThreadStart(ListenForClients));
- listenThread.Start();
- listenThread.Join();
- _isRunning = true;
- return 0;
- }
- private void ListenForClients()
- {
- try
- {
- tcpListener.Start();
- Console.WriteLine("Listener started");
- }
- catch (SocketException e)
- {
- Console.WriteLine("Server could not start" + e.Message);
- this.SetTextOnListBox("server could not start, error of binding");
- return;
- }
- Console.WriteLine("Server runnin on port " + portBox.Text);
- this.SetTextOnListBox("server run on port ");
- while (true)
- {
- Console.WriteLine("IN while");
- TcpClient client = tcpListener.AcceptTcpClient();
- Console.WriteLine("accepted client");
- this.SetTextOnListBox("new clinect connected from: " + client.Client.RemoteEndPoint);
- Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
- clientThread.Start(client);
- }
- }
- private void HandleClientComm(object client)
- {
- TcpClient tcpClient = (TcpClient)client;
- String clientIP = "" + tcpClient.Client.RemoteEndPoint.AddressFamily;
- NetworkStream clientStream = tcpClient.GetStream();
- int size = 2014;
- byte[] message = new byte[size];
- int bytesRead;
- ASCIIEncoding encoder = new ASCIIEncoding();
- while (true)
- {
- bytesRead = 0;
- try
- {
- bytesRead = clientStream.Read(message, 0, size);
- clientStream.Write(message, 0, bytesRead);
- }
- catch { break; }
- if (bytesRead == 0) break;
- this.SetTextOnListBox(" Message from " + tcpClient.Client.RemoteEndPoint + ": " + encoder.GetString(message, 0, bytesRead));
- }
- tcpClient.Close();
- }
- private void SetTextOnListBox(string text)
- {
- if (this.listBox1.InvokeRequired)
- {
- this.BeginInvoke(new SetTextCallback(SetTextOnListBox), new object[] { text });
- //return 0;
- }
- else
- {
- this.listBox1.Items.Insert(0, text);
- }
- }
- private void startButton_Click(object sender, EventArgs e)
- {
- isServerRunning = !isServerRunning;
- if (isServerRunning) ServerStart();
- }
- public bool isServerRunning
- {
- get
- {
- return _isRunning;
- }
- set
- {
- _isRunning = value;
- startButton.Text = _isRunning ? "Stop" : "Start";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement