3

Take a look at the following two programs:

//Server

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MyServerProgram
{
    class Program
    {
        static void Main(string[] args)
        {            
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            int port = 2000;
            TcpListener listener = new TcpListener(ip, port);
            listener.Start();

            TcpClient client = listener.AcceptTcpClient();

            NetworkStream netStream = client.GetStream();

            BinaryReader br = new BinaryReader(netStream);

            try
            {
                while (client.Client.Connected)
                {
                    string str = br.ReadString();

                    Console.WriteLine(str);
                }
            }
            catch
            {
                br.Close();
                netStream.Close();
                client.Close();
                listener.Stop();
            }
        }
    }
}

//Client

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MyClientProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 2000;
            TcpClient client = new TcpClient("localhost", port);

            NetworkStream netStream = client.GetStream();

            BinaryWriter br = new BinaryWriter(netStream);

            try
            {
                int i=1;
                while (client.Client.Connected)
                {
                    br.Write(i.ToString());
                    br.Flush();
                    i++;

                    int milliseconds = 2000;
                    System.Threading.Thread.Sleep(milliseconds);
                }
            }
            catch
            {
                br.Close();
                netStream.Close();
                client.Close();
            }
        }
    }
}

These programs are working fine.

Suppose, at this point of this program, I need the server to print a message on the screen as soon as a client gets connected to it, and, also when the client is disconnected.

How can I do that?

user366312
  • 16,949
  • 65
  • 235
  • 452
  • in your code client is connected after `TcpClient client = listener.AcceptTcpClient();` and disconnected once you exit while loop or inside catch block; check it too https://stackoverflow.com/questions/1387459/how-to-check-if-tcpclient-connection-is-closed – Greg Apr 27 '19 at 21:57

1 Answers1

2

AcceptTcpClient blocks execution and starts waiting for connection. So right after it you can write message that client connected. Also you could write connected client address. Just for information, but sometimes it could be helpful.

TcpClient client = listener.AcceptTcpClient();
ShowMessage("Connected " + ((IPEndPoint)client.Client.RemoteEndPoint).Address);

For detect client disconnect you could catch exceptions. Change your catch like this:

catch (Exception ex) {
    var inner = ex.InnerException as SocketException;
    if (inner != null && inner.SocketErrorCode == SocketError.ConnectionReset)
        ShowMessage("Disconnected");
    else
        ShowMessage(ex.Message);
...
vik_78
  • 1,107
  • 2
  • 13
  • 20