Client.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Text;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. namespace Server
  6. {
  7. public class Client
  8. {
  9. TcpClient client;
  10. NetworkStream networkStream;
  11. private readonly AppGlobal _global;
  12. private int iduser;
  13. public Client(TcpClient socket, AppGlobal global)
  14. {
  15. client = socket;
  16. networkStream = client.GetStream();
  17. _global = global;
  18. iduser = -1;
  19. Console.WriteLine("New client connected");
  20. Thread t = new Thread(receive);
  21. t.Start();
  22. }
  23. public void receive()
  24. {
  25. while (true)
  26. {
  27. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  28. networkStream.Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
  29. String response = Encoding.ASCII.GetString(bytesFrom);
  30. String[] response_parts = response.Split('|');
  31. if (response_parts.Length > 0)
  32. {
  33. switch (response_parts[0])
  34. {
  35. case "0": //login
  36. if (response_parts.Length == 4) {
  37. int admin, id;
  38. _global.CheckLogin(response_parts[1], response_parts[2], out admin, out id);
  39. if(id > -1)
  40. {
  41. this.iduser = id;
  42. sendString("0|" + id + "|" + admin + "|" );
  43. }
  44. else
  45. {
  46. sendString("0|0|0|");
  47. }
  48. }
  49. break;
  50. case "1": //meetsessies ophalen
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. public void sendString(string s)
  57. {
  58. byte[] b = Encoding.ASCII.GetBytes(s);
  59. networkStream.Write(b, 0, b.Length);
  60. networkStream.Flush();
  61. }
  62. }
  63. }