Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 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.Sockets;
  11. using System.Net;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Threading;
  15.  
  16.  
  17.  
  18.  
  19. namespace servidor
  20. {
  21. public partial class Servidor : Form
  22. {
  23.  
  24. TcpListener ntpcListener;
  25. TcpClient ntcpClient = null;
  26. private Socket socket;
  27.  
  28.  
  29.  
  30.  
  31.  
  32. public Servidor()
  33. {
  34.  
  35. InitializeComponent();
  36. txtip.Text = GetLocalIPAddress();
  37. txtporto.Text = "23000";
  38.  
  39. }
  40.  
  41. public string gettime()
  42. {
  43. return string.Format("{0:HH:mm:ss tt}", DateTime.Now);
  44. }
  45.  
  46.  
  47. private void btnlogout_Click(object sender, EventArgs e)
  48. {
  49. this.Hide();
  50. Login este = new Login();
  51. este.Show();
  52. }
  53.  
  54. private void btnlisten_Click(object sender, EventArgs e)
  55. {
  56. IPAddress ip;
  57. int porto;
  58.  
  59.  
  60. if (!IPAddress.TryParse(txtip.Text, out ip))
  61. {
  62. txtmsg.Text += gettime() + ": Ip Invalido\n";
  63. return;
  64. }
  65. if (!int.TryParse(txtporto.Text, out porto))
  66. {
  67. txtmsg.Text += gettime() + ": Porto Inválido\n";
  68. return;
  69. }
  70. ntpcListener = new TcpListener(ip, porto);
  71. ntpcListener.Start();
  72. txtmsg.Text += gettime() + ": Servidor à escuta\n";
  73. ntpcListener.BeginAcceptSocket(OnCompleteAcceptCliente, ntpcListener);
  74.  
  75.  
  76.  
  77.  
  78.  
  79. }
  80.  
  81. public void OnCompleteAcceptCliente(IAsyncResult iar)
  82. {
  83.  
  84. TcpListener tcpl = (TcpListener)iar.AsyncState;
  85. ntcpClient = tcpl.EndAcceptTcpClient(iar);
  86. Thread th = new Thread(new ThreadStart(this.connected));
  87. th.Start();
  88.  
  89.  
  90.  
  91.  
  92. }
  93.  
  94. public void connected()
  95. {
  96. Thread.Sleep(2000);
  97. if (this.InvokeRequired)
  98.  
  99. {
  100.  
  101. this.Invoke(new MethodInvoker(connected));
  102. StreamWriter writer = new StreamWriter(ntcpClient.GetStream());
  103. writer.AutoFlush = true;
  104. writer.WriteLine(gettime() + ":Cliente aceite...");
  105. return;
  106.  
  107.  
  108. }
  109. txtmsg.Text += gettime() + ":Cliente aceite...\n";
  110. }
  111.  
  112.  
  113.  
  114.  
  115. public static string GetLocalIPAddress()
  116. {
  117. var host = Dns.GetHostEntry(Dns.GetHostName());
  118. foreach (var ip in host.AddressList)
  119. {
  120. if (ip.AddressFamily == AddressFamily.InterNetwork)
  121. {
  122. return ip.ToString();
  123. }
  124. }
  125. throw new Exception("Local IP Address Not Found!");
  126. }
  127.  
  128. private void btnsend_Click(object sender, EventArgs e)
  129. {
  130. enviar();
  131. }
  132.  
  133.  
  134. public void enviar()
  135. {
  136.  
  137. StreamWriter writer = new StreamWriter(ntcpClient.GetStream());
  138. writer.AutoFlush = true;
  139. writer.WriteLine(gettime() + ":" + txtmsgsend.Text );
  140. this.txtmsg.Text += gettime() +":" + txtmsgsend.Text + "\n";
  141. txtmsgsend.Text = "";
  142. if (txtmsgsend.Text == "/clear")
  143. {
  144. txtmsg.Text = "";
  145. }
  146. receber();
  147. }
  148.  
  149. public void receber()
  150. {
  151. StreamReader reader = new StreamReader(ntcpClient.GetStream());
  152. this.txtmsg.Text += gettime() + ":" + reader.ReadLine() + "\n";
  153. return;
  154.  
  155.  
  156. }
  157.  
  158. private void txtmsgsend_KeyPress(object sender, KeyPressEventArgs e)
  159. {
  160.  
  161. if (e.KeyChar == (char)13)
  162. {
  163. enviar();
  164. }
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement