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.Text;
- using System.Net.Sockets;
- using System.Diagnostics;
- using System.Threading;
- namespace Simpele_Server
- {
- class Program
- {
- const int PORT = 2000;
- const string IP = "10.145.4.43";
- string doorStuur = "";
- static void Main(string[] args)
- {
- Listener listener = new Listener(IP, PORT);
- listener.start();
- Console.ReadKey();
- listener.stop();
- }
- class Listener
- {
- private TcpListener listener;
- private System.Net.IPAddress ip;
- private bool on;
- public Listener(string ipAddress, int port)
- {
- this.ip = System.Net.IPAddress.Parse(ipAddress);
- this.listener = new TcpListener(System.Net.IPAddress.Parse(ipAddress), port);
- }
- public void start()
- {
- on = true;
- listener.Start();
- Thread thread = new Thread(() => listen());
- thread.Start();
- }
- public void stop()
- {
- on = false;
- listener.Stop();
- }
- public bool isOn()
- {
- return on;
- }
- private void listen()
- {
- string doorStuur = "";
- Console.WriteLine("started listening");
- while (on)
- {
- if (listener.Pending())
- {
- Console.WriteLine("found a connection");
- TcpClient eenClient = listener.AcceptTcpClient();
- //Gebruik een 'NetworkStream object' om gegevens te verzenden en te ontvangen
- NetworkStream ns = eenClient.GetStream();
- byte[] data = new byte[eenClient.ReceiveBufferSize];
- //Lees de binnenkomende 'stream' --> Read() is een 'blocking call'
- int numBytesRead = ns.Read(data, 0, System.Convert.ToInt32(eenClient.ReceiveBufferSize));
- string antwoord = Encoding.ASCII.GetString(data, 0, numBytesRead);
- Console.WriteLine(string.Format("{0} scoorde {1} punten", antwoord.Substring(0, 7), controleer(antwoord)));
- doorStuur += antwoord.Substring(0, 7) + controleer(antwoord) + ", ";
- Console.WriteLine(doorStuur.Length);
- if (doorStuur.Length == 30)
- {
- TcpClient mijnClient = new TcpClient();
- mijnClient.Connect("10.145.6.147", 2000);
- NetworkStream mijnNS = mijnClient.GetStream();
- byte[] aDataTeVersturen = Encoding.ASCII.GetBytes(doorStuur);
- mijnNS.Write(aDataTeVersturen, 0, aDataTeVersturen.Length);
- Console.Write("\n De punten werden verzonden...");
- }
- }
- }
- Console.WriteLine("stopped");
- }
- public int controleer(string antwoord)
- {
- int punten = 0;
- string ant = "";
- if (antwoord.Contains("Client1:"))
- {
- ant = antwoord.Replace("Client1:", "");
- }
- if (antwoord.Contains("Client3:"))
- {
- ant = antwoord.Replace("Client3:", "");
- }
- if (antwoord.Contains("Client2:"))
- {
- ant = antwoord.Replace("Client2:", "");
- }
- string ant1 = ant.Substring(0, 1);
- string ant2 = ant.Substring(1, 1);
- string ant3 = ant.Substring(2, 1);
- string ant4 = ant.Substring(3, 1);
- if (ant1.Equals("1"))
- punten++;
- if (ant2.Equals("2"))
- punten++;
- if (ant3.Equals("3"))
- punten++;
- if (ant4.Equals("4"))
- punten++;
- return punten;
- }
- //private string stuurDoor(string )
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement