Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TcpListener listener = new TcpListener("localHost", 10000); //1st is an ip address, 2nd is post number
- listener.Start();
- while (!bStopServer)
- {
- try
- {
- if (listener.Pending())
- {
- using (TcpClient client = listener.AcceptTcpClient())
- {
- using (NetworkStream ns = client.GetStream())
- {
- // Check to see if this NetworkStream is readable.
- if (ns.CanRead)
- {
- byte[] buffer = new byte[1024];
- int allBytesRead = 0;
- // Incoming message may be larger than the buffer size:
- do
- {
- allBytesRead = ns.Read(buffer, 0, buffer.Length);
- if (allBytesRead == 0)
- {
- //client has left!
- }
- }
- while (ns.DataAvailable);
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement