Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Diagnostics;
  7. using System.Threading;
  8.  
  9. namespace Simpele_Server
  10. {
  11.     class Program
  12.     {
  13.         const int PORT = 2000;
  14.         const string IP = "10.145.4.43";
  15.         string doorStuur = "";
  16.         static void Main(string[] args)
  17.         {
  18.             Listener listener = new Listener(IP, PORT);
  19.             listener.start();
  20.             Console.ReadKey();
  21.             listener.stop();
  22.         }
  23.  
  24.         class Listener
  25.         {
  26.             private TcpListener listener;
  27.             private System.Net.IPAddress ip;
  28.             private bool on;
  29.             public Listener(string ipAddress, int port)
  30.             {
  31.                 this.ip = System.Net.IPAddress.Parse(ipAddress);
  32.                 this.listener = new TcpListener(System.Net.IPAddress.Parse(ipAddress), port);
  33.             }
  34.             public void start()
  35.             {
  36.                 on = true;
  37.                 listener.Start();
  38.                 Thread thread = new Thread(() => listen());
  39.                 thread.Start();
  40.             }
  41.             public void stop()
  42.             {
  43.                 on = false;
  44.                 listener.Stop();
  45.             }
  46.             public bool isOn()
  47.             {
  48.                 return on;
  49.             }
  50.  
  51.             private void listen()
  52.             {
  53.                 string doorStuur = "";
  54.              
  55.                 Console.WriteLine("started listening");
  56.                 while (on)
  57.                 {
  58.                     if (listener.Pending())
  59.                     {
  60.                        
  61.                    
  62.                         Console.WriteLine("found a connection");
  63.                         TcpClient eenClient = listener.AcceptTcpClient();
  64.  
  65.                         //Gebruik een 'NetworkStream object' om gegevens te verzenden en te ontvangen
  66.                         NetworkStream ns = eenClient.GetStream();
  67.                         byte[] data = new byte[eenClient.ReceiveBufferSize];
  68.  
  69.                         //Lees de binnenkomende 'stream' --> Read() is een 'blocking call'
  70.                         int numBytesRead = ns.Read(data, 0, System.Convert.ToInt32(eenClient.ReceiveBufferSize));
  71.                         string antwoord = Encoding.ASCII.GetString(data, 0, numBytesRead);
  72.                        
  73.  
  74.                         Console.WriteLine(string.Format("{0} scoorde {1} punten", antwoord.Substring(0, 7), controleer(antwoord)));
  75.                        
  76.                         doorStuur += antwoord.Substring(0, 7) + controleer(antwoord) + ", ";
  77.                         Console.WriteLine(doorStuur.Length);
  78.                         if (doorStuur.Length == 30)
  79.                         {
  80.                             TcpClient mijnClient = new TcpClient();
  81.                             mijnClient.Connect("10.145.6.147", 2000);
  82.                             NetworkStream mijnNS = mijnClient.GetStream();
  83.                            
  84.                             byte[] aDataTeVersturen = Encoding.ASCII.GetBytes(doorStuur);
  85.                             mijnNS.Write(aDataTeVersturen, 0, aDataTeVersturen.Length);
  86.                             Console.Write("\n De punten werden verzonden...");
  87.  
  88.                         }
  89.                      
  90.  
  91.                        
  92.  
  93.                        
  94.                     }
  95.                 }
  96.                 Console.WriteLine("stopped");
  97.             }
  98.             public int controleer(string antwoord)
  99.             {
  100.                                
  101.                 int punten = 0;
  102.                 string ant = "";
  103.                 if (antwoord.Contains("Client1:"))
  104.                 {
  105.                      ant = antwoord.Replace("Client1:", "");
  106.                 }
  107.  
  108.                 if (antwoord.Contains("Client3:"))
  109.                 {
  110.                     ant = antwoord.Replace("Client3:", "");
  111.                 }
  112.                 if (antwoord.Contains("Client2:"))
  113.                 {
  114.                     ant = antwoord.Replace("Client2:", "");
  115.                 }
  116.                
  117.                 string ant1 = ant.Substring(0, 1);
  118.                 string ant2 = ant.Substring(1, 1);
  119.                 string ant3 = ant.Substring(2, 1);
  120.                 string ant4 = ant.Substring(3, 1);
  121.                 if (ant1.Equals("1"))
  122.                     punten++;
  123.                 if (ant2.Equals("2"))
  124.                     punten++;
  125.                 if (ant3.Equals("3"))
  126.                     punten++;
  127.                 if (ant4.Equals("4"))
  128.                     punten++;
  129.                 return punten;
  130.             }
  131.             //private string stuurDoor(string )
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement