Guest User

Untitled

a guest
Mar 19th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. class Program {
  11.  
  12. private static Random random = new Random();
  13.  
  14. static void Main(string[] args) {
  15. IPAddress ip = IPAddress.Parse("127.0.0.1");
  16. int port = 5000;
  17. TcpClient client = new TcpClient();
  18. client.Connect(ip, port);
  19. Console.WriteLine("client connected!!");
  20.  
  21. Thread thread = new Thread(o => ReceiveData((TcpClient)o));
  22.  
  23. thread.Start(client);
  24.  
  25. Console.ReadKey();
  26. client.Client.Shutdown(SocketShutdown.Send);
  27. thread.Join();
  28. //ns.Close();
  29. client.Close();
  30. Console.WriteLine("disconnect from server!!");
  31. Console.ReadKey();
  32. }
  33.  
  34.  
  35. static void ReceiveData(TcpClient client) {
  36. NetworkStream ns = client.GetStream();
  37. byte[] receivedBytes = new byte[1024];
  38. int byte_count;
  39.  
  40.  
  41. while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0) {
  42. String receivedDt = Encoding.ASCII.GetString(receivedBytes, 0, byte_count);
  43. Console.Write("in:" + receivedDt);
  44.  
  45. int val = random.Next(3, 8) * 1000;
  46. Thread.Sleep(val);
  47.  
  48. byte[] buffer = Encoding.ASCII.GetBytes(receivedDt);
  49. ns.Write(buffer, 0, buffer.Length);
  50. Console.Write("ot:" + val);
  51. }
  52.  
  53.  
  54. }
  55. }
Add Comment
Please, Sign In to add comment