Guest User

Untitled

a guest
Dec 4th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. //form1
  2. namespace SockProgram
  3. {
  4. public partial class loginForm : Form
  5. {
  6. public Socket sock = new SockProgram.Socket();
  7.  
  8. public loginForm()
  9. {
  10. InitializeComponent();
  11. }
  12. private void button1_Click(object sender, EventArgs e)
  13. {
  14. if (this.inputusername.Text.Length > 1 && this.inputpassword.Text.Length > 1)
  15. {
  16. sock.connect();
  17. sock.send("hello\r\n");
  18. sock.username = this.inputusername.Text;
  19. sock.password = this.inputpassword.Text;
  20. }
  21.  
  22. }
  23. }
  24. }
  25.  
  26. //Socket.cs
  27. namespace SockProgram
  28. {
  29. public class Socket
  30. {
  31.  
  32. NetworkStream ns;
  33. System.Windows.Forms.Timer sockRead = new Timer();
  34. int connected = 0;
  35.  
  36. string str_username;
  37. string str_password;
  38. int loggedin = 0;
  39.  
  40. public string username
  41. {
  42. get { return str_username; }
  43. set { str_username = value; }
  44. }
  45.  
  46. public string password
  47. {
  48. get { return str_password; }
  49. set { str_password = value; }
  50. }
  51.  
  52. int isConnected
  53. {
  54. get { return connected; }
  55. set { connected = value; }
  56. }
  57.  
  58. public Socket()
  59. {
  60. return;
  61. }
  62. public void connect()
  63. {
  64. TcpClient server;
  65.  
  66. try
  67. {
  68. server = new TcpClient("servon.dk", 1000);
  69. }
  70. catch (SocketException)
  71. {
  72. MessageBox.Show("Kunne ikke connecte til server.");
  73. return;
  74. }
  75. ns = server.GetStream();
  76. connected = 1;
  77. Debug.WriteLine("Connected..");
  78. sockRead.Interval = 20;
  79. sockRead.Enabled = true;
  80. sockRead.Tick += new EventHandler(sockRead_Tick);
  81. }
  82.  
  83. void sockRead_Tick(object sender, EventArgs e)
  84. {
  85. string stringData;
  86. int bytes;
  87. if (ns.DataAvailable)
  88. {
  89. byte[] data = new byte[1024];
  90. bytes = ns.Read(data, 0, data.Length);
  91. stringData = Encoding.ASCII.GetString(data, 0, bytes);
  92. Debug.WriteLine("<- " + stringData);
  93. handleInput(stringData);
  94. }
  95. }
  96.  
  97. public void send(string text)
  98. {
  99. if (isConnected == 1)
  100. {
  101. ns.Write(Encoding.ASCII.GetBytes(text), 0, text.Length);
  102. ns.Flush();
  103. Debug.WriteLine("-> " + text);
  104. }
  105. }
  106.  
  107. public void handleInput(string input)
  108. {
  109. string[] words = input.Split(' ');
  110. int code = System.Convert.ToInt32(words[0]);
  111. Debug.WriteLine("Code: " + code);
  112. switch (code)
  113. {
  114. case 1:
  115. Debug.WriteLine("Handshake ok");
  116. /* Do login */
  117. send("USER " + username + "\n");
  118. send("PASS " + password + "\n");
  119. break;
  120. case 2:
  121. MessageBox.Show("Du bruger en forkert version af programmet, update plz");
  122. break;
  123. case 3:
  124. MessageBox.Show("Brugernavn ikke fundet");
  125. break;
  126. case 4:
  127. MessageBox.Show("Forkert kode");
  128. break;
  129. case 5:
  130. loggedin = 1;
  131. Form mainForm = new mainForm();
  132. break;
  133. }
  134. }
  135.  
  136. }
  137. }
Add Comment
Please, Sign In to add comment