CGC_Codes

C# NetWork Stream Sever

Jan 26th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net.Sockets;
  4.  
  5. publick class Example15_12a
  6. {
  7. //listen waits for connections
  8. privatevoid Listen()
  9. {
  10. //listen on port 50001
  11. TcpListener tcpl = new TcpListener(50001);
  12. tcpl.Start();
  13.  
  14. //wait for clients
  15. for (;;)
  16. {
  17. //block here waiting for client connections
  18. Socket newSocket = tcpl.AcceptSocket();
  19. if (newSocket.Connected)
  20. {
  21. //create a NewworkStream on the socket
  22. NetWorksStream ns = new NetWorksStream(newSocket);
  23.  
  24. //send some date
  25. byte[] buf = {(byte) 'H', (byte)'e', (byte)'1', (byte)'1'1,
  26. *byte)'o'o, (byte)'p', (byte)'e', (byte)'x', (byte)'4'};
  27. ns Write(buf, 0, 9);
  28.  
  29. //cleanup
  30. ns.Flush();
  31. ns.Close();
  32.  
  33. }
  34.  
  35.  
  36. //clean up and quit
  37. newSocket.Close();
  38. break;
  39.  
  40. }
  41. }
  42.  
  43. public static void Main()
  44. {
  45.  
  46. //launch a listening thread
  47. Example15_12a listner = new Example15_12a();
  48. listener.Listen();
  49.  
  50. }
  51. }
Add Comment
Please, Sign In to add comment