Advertisement
Guest User

sever

a guest
Oct 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 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 Tuan3_Bai1
  15. {
  16.  
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.         private void Form1_Load(object sender, EventArgs e)
  24.         {
  25.             CheckForIllegalCrossThreadCalls = false;
  26.         }
  27.  
  28.         /*********** Data structure ***********/
  29.         public IPAddress ipAd;
  30.         public int port;
  31.         public TcpListener server;
  32.         public int count = 0;
  33.         public Socket[] socket = new Socket[100];
  34.         public string[] sum = new string[100];
  35.         public List<int> isUsed = new List<int>();
  36.         public bool check = true;
  37.         private void Btn_Start_Click(object sender, EventArgs e)
  38.         {
  39.             Btn_Start.Enabled = false;
  40.             StartServer();
  41.             //serves multiple clients  
  42.             Thread t = new Thread(connect);
  43.             t.IsBackground = true;
  44.             t.Start();
  45.         }
  46.         public void StartServer()
  47.         {
  48.             ipAd = IPAddress.Parse(txt_IP.Text);
  49.             port = int.Parse(txt_Port.Text);
  50.             server = new TcpListener(ipAd, port);
  51.             server.Start();
  52.             count = 0;
  53.         }
  54.  
  55.         public int testconnect(Socket socket)
  56.         {
  57.             return 0;
  58.         }
  59.         //Threading
  60.  
  61.         public void connect()
  62.         {
  63.             while (true)
  64.             {
  65.                 //accept a new connection
  66.                 socket[count] = server.AcceptSocket();
  67.  
  68.                 count++;//should use lock here 
  69.                 //break main flow into main + ServeClientConcurrently flow
  70.                 isUsed.Add(1);
  71.                 if (check != true)
  72.                 {
  73.                     int pos = 0;
  74.                     foreach (int i in isUsed)
  75.                     {
  76.                         if (i == 0)
  77.                         {
  78.                             count = pos+1;
  79.                             break;
  80.                         }
  81.                         pos++;
  82.                     }
  83.                     isUsed[pos] = 1;
  84.                     check = true;
  85.                 }
  86.                 Thread t = new Thread(ServeClientConcurrently);
  87.                 t.Start(count-1);
  88.                 txt_QLKN.AppendText("Client " + (count ) + " connect" + "\n");
  89.             }
  90.         }
  91.         public void ServeClientConcurrently(object obj)
  92.         {
  93.             //get correct socket
  94.             int index = (Int32)obj;
  95.             while (true)
  96.             {
  97.                 byte[] b = new byte[100];
  98.                 //string temp = socket[index].RemoteEndPoint.ToString();
  99.                 try
  100.                 {
  101.                     int k = socket[index].Receive(b);
  102.                 }
  103.                 catch
  104.                 {
  105.                     socket[index].Close();
  106.                     isUsed[index] = 0;
  107.                     check = false;
  108.                     txt_QLKN.AppendText("Client " + (index + 1) + " disconnect" + "\n");
  109.                     return;            
  110.                 }
  111.  
  112.                 string abc = Encoding.ASCII.GetString(b);
  113.                 sum[index] = SumFun(abc);
  114.                 Txt_QLTV.AppendText("Yêu cầu từ client " + (index + 1).ToString() + "\n");
  115.                 Txt_QLTV.AppendText("Kết quả: " + sum[index] + "\n");
  116.                 ASCIIEncoding asen = new ASCIIEncoding();
  117.                 socket[index].Send(asen.GetBytes(sum[index]));
  118.             }
  119.         }
  120.        
  121.         public string SumFun(string str)
  122.         {
  123.             string[] strS = str.Split('/');
  124.             int x1 = int.Parse(strS[0]);
  125.             int x2 = int.Parse(strS[1]);
  126.             int sum = x1 + x2;
  127.             return sum.ToString();
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement