Guest User

Untitled

a guest
Mar 7th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Threading;
  13.  
  14. namespace testServer
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         const int PORT = 5454;
  19.         System.Threading.Thread ThreadForServer = null;
  20.         List<User> users = new List<User>();
  21.  
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.             ThreadForServer = new System.Threading.Thread(startServer);
  27.             ThreadForServer.Start();
  28.         }
  29.  
  30.         public void startServer(object i)
  31.         {
  32.             string hostName = Dns.GetHostName();
  33.             IPHostEntry ipEntry = Dns.GetHostByName(hostName);
  34.             IPAddress[] ipAdresses = ipEntry.AddressList;
  35.  
  36.             IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ipAdresses[0].ToString()), PORT);
  37.  
  38.             Socket sListener = new Socket(IPAddress.Parse(ipAdresses[0].ToString()).AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  39.  
  40.             try
  41.             {
  42.                 sListener.Bind(ipEndPoint);
  43.                 sListener.Listen(10);
  44.  
  45.                 while (true)
  46.                 {
  47.                     Socket handler = sListener.Accept();
  48.                     string data = null;
  49.  
  50.                     byte[] bytes = new byte[1024];
  51.                     int bytesRec = handler.Receive(bytes);
  52.  
  53.                     data += Encoding.UTF8.GetString(bytes, 0, bytesRec);
  54.                     clientQuery(data, handler);
  55.                 }
  56.  
  57.             }
  58.             catch (Exception ex)
  59.             {
  60.                 updateLog(ex.ToString());
  61.             }
  62.  
  63.         }
  64.  
  65.         private void clientQuery(string query, Socket client)
  66.         {
  67.             string queryCode = query.Substring(1, 3);
  68.             string userName = query.Substring(6, query.Length - 6);
  69.             byte[] serverAnswer = null;
  70.  
  71.  
  72.             switch (queryCode)
  73.             {
  74.                 case "001":
  75.                     User user = new User(users.Count, userName, client);
  76.                     users.Add(user);
  77.  
  78.                     updateLog("New client connected. ID: " + user.ID + ". Name: " + user.Name);
  79.                     serverAnswer = Encoding.UTF8.GetBytes("[001] Connect successfull.");
  80.                     client.Send(serverAnswer);
  81.  
  82.                     Action action = () =>
  83.                     {
  84.                         activeUsersLabel.Text = "Active users: " + users.Count;
  85.                     };
  86.                     Invoke(action);
  87.                     break;
  88.             }
  89.  
  90.             client.Shutdown(SocketShutdown.Both);
  91.             client.Close();
  92.         }
  93.  
  94.  
  95.         private void updateLog(string logData)
  96.         {
  97.             Action action = () =>
  98.             {
  99.                 logTB.AppendText(logData + Environment.NewLine);
  100.             };
  101.             Invoke(action);
  102.         }
  103.     }
  104.     class User
  105.     {
  106.         public int ID { get; set; }
  107.         public string Name { get; set; }
  108.         public Socket Socket { get; set; }
  109.  
  110.         public User(int id, string name, Socket socket)
  111.         {
  112.             ID = id;
  113.             Name = name;
  114.             Socket = socket;
  115.         }
  116.     }
  117. }
Add Comment
Please, Sign In to add comment