Advertisement
benjojo

dd

May 6th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. TcpListener listener = new TcpListener("localHost", 10000); //1st is an ip address, 2nd is post number
  2. listener.Start();
  3.  
  4. while (!bStopServer)
  5. {
  6. try
  7. {
  8. if (listener.Pending())
  9. {
  10. using (TcpClient client = listener.AcceptTcpClient())
  11. {
  12. using (NetworkStream ns = client.GetStream())
  13. {
  14. // Check to see if this NetworkStream is readable.
  15. if (ns.CanRead)
  16. {
  17. byte[] buffer = new byte[1024];
  18. int allBytesRead = 0;
  19.  
  20. // Incoming message may be larger than the buffer size:
  21. do
  22. {
  23. allBytesRead = ns.Read(buffer, 0, buffer.Length);
  24. if (allBytesRead == 0)
  25. {
  26. //client has left!
  27. }
  28. }
  29. while (ns.DataAvailable);
  30. }
  31. }
  32. }
  33. }
  34. }
  35. catch (Exception ex)
  36. {
  37. MessageBox.Show(ex.Message);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement