Home > Back-end >  Client Dissconnect or down
Client Dissconnect or down

Time:10-09

I have 2 Application in a visual studio Windows Forms App(.Net Framework 4) The names of my two programs are:

IpServer

IpClient

My problem is that I do not know what to do that when the IpClient Application closes or stops or the IpServer Application shows a message in messagebox.show("Client Is dissconnect")

IpServer Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace IpServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        TcpListener tcplist;
        Socket s;
        private void init()
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            tcplist = new TcpListener(ip,5050);
            tcplist.Start();
            while (true)
            {
                s = tcplist.AcceptSocket();
                Thread t = new Thread(new ThreadStart(replay));
                t.IsBackground = true;
                t.Start();
            }
        }
        private void replay()
        {
            Socket sc = s;
            NetworkStream ns = new NetworkStream(sc);
            StreamReader reader = new StreamReader(ns);
            StreamWriter writer = new StreamWriter(ns);
            string str = "";
            string response = "";
            try { str = reader.ReadLine(); }
            catch { str = "error"; }
            if (str == "register")
            {
                MessageBox.Show("ok");
            }
            response = "registeredSucss,";
            writer.WriteLine(response);
            writer.Flush();
            ns.Close();
            sc.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(init));
            t.IsBackground = true;
            t.Start();
            MessageBox.Show("Server run!!");
            button1.Enabled = false;
        }

and IpClient Code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace IpClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        TcpClient tcp;
        NetworkStream ns;
        StreamReader reader;
        StreamWriter writer;
        string str = "";

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                tcp = new TcpClient("127.0.0.1",5050);
                tcp.ReceiveBufferSize = 25000;
                tcp.NoDelay = true;
                ns = tcp.GetStream();
                reader = new StreamReader(ns);
                writer = new StreamWriter(ns);
                writer.WriteLine("register");
                writer.Flush();
                str = reader.ReadLine();
                string[] strsplit = null;
                strsplit = str.Split(',');
                if (strsplit[0] != "registeredSucss")
                {
                    MessageBox.Show("Not connected");
                }
                else
                {
                    MessageBox.Show("Your connected");
                }
                
            }
            catch
            {
                MessageBox.Show("error");
            }
        }

CodePudding user response:

I wrote a complete example for you.

You can modify it according to your needs.

Simply connecting the client to the server without sending or receiving messages will not disconnect.

In the server and the client, there is a thread that continuously calls the receive function, and sends a message when the connection is disconnected.

 byte[] buffer = new byte[1024 * 1024 * 2];
                    int length = socket.Receive(buffer);
                    if (length == 0) {
                    ///Write the desired operation
                    }

"Disconnect when closing the window" uses the formclosing event:

 private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
        }

This is the control diagram used by the two windows:

IpClient:

enter image description here

IpServe:

enter image description here

This is the code for the two windows:

IpClient:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace IpClient {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        Socket socket;//The socket responsible for the connection
        private void ConnectB_Click(object sender, EventArgs e) {
            //Determine whether to request a connection repeatedly
            try {
                Log("Connected "   socket.LocalEndPoint.ToString()   "\nPlease do not request the connection repeatedly");
            } catch {
                //Determine whether the input is wrong
                try {
                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    //Create IP address and port number;
                    IPAddress ip = IPAddress.Parse(Ipbox.Text);
                    int port = Convert.ToInt32(PortBox.Text);
                    IPEndPoint iPEndPoint = new IPEndPoint(ip, port);
                    //Determine whether you can connect to the server
                    try {
                        socket.Connect(iPEndPoint);
                        Log("Connected "   socket.LocalEndPoint.ToString());
                        //Start receiving data thread
                        Thread th = new Thread(receive);
                        th.IsBackground = true;
                        th.Start();
                    } catch {
                        Log("Port is not open");
                    }                  
                } catch  {
                    socket = null;
                    Log("Input error");
                }
            }
        }

        private void Log(string str) {
            ClientLog.AppendText(str   "\r\n");
        }
        private void receive() {
            while (true) {
                //Determine whether the data can be received
                try {
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    int length = socket.Receive(buffer);
                    if (length == 0) {
                        Log("Port is not open");
                        CloseSocket(socket);
                        socket = null;
                        break;
                    }
                    string txt = Encoding.UTF8.GetString(buffer, 0, length);
                    Log(socket.RemoteEndPoint   ":\r\t"   txt);
                } catch {
                    break;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e) {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void CloseB_Click(object sender, EventArgs e) {
            if (socket != null) {
                CloseSocket(socket);
                socket = null;
                Log("Connection closed");
                
            } else {
                Log("Not connected");
            }
        }

        private void SendB_Click(object sender, EventArgs e) {
            try {
                string txt = SendText.Text;
                byte[] buffer = Encoding.ASCII.GetBytes(txt);//ascii encoding
                socket.Send(buffer);
            } catch {
                Log("Not connected");
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
            if (socket != null) {
                CloseSocket(socket);
                socket = null;
            }
        }

        //Disconnect socket
        private void CloseSocket(Socket o) {
            try {
                o.Shutdown(SocketShutdown.Both);
                o.Disconnect(false);
                o.Close();
            } catch {
                Log("error");
            }
        }
    }
}

IpServer:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace IpServer {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        Socket socketSend;//Socket responsible for communication
        Socket socketListener;//Socket responsible for monitoring
        Dictionary<string, Socket> dictionary = new Dictionary<string, Socket>();//Store the connected Socket
        private void listnerB_Click(object sender, EventArgs e) {
            //Create a listening socket
            //SocketType.Stream streaming corresponds to the tcp protocol
            //Dgram, datagram corresponds to UDP protocol
            socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try {
                //Create IP address and port number;
                IPAddress ip = IPAddress.Parse(Ipbox.Text);
                int port = Convert.ToInt32(PortBox.Text);
                IPEndPoint iPEndPoint = new IPEndPoint(ip, port);
                //Let the listening socket bind the ip and port number
                socketListener.Bind(iPEndPoint);
                Log("Listen successfully"   ip   "\t"   port);
                //Set up the listening queue
                socketListener.Listen(10);//Maximum number of connections at a time
                Thread thread = new Thread(Listen);
                thread.IsBackground = true;
                thread.Start(socketListener);
        } catch  {
                Log("Failed to listen");
    }
}

        //Use threads to receive data
        private void Listen(object o) {
            Socket socket = o as Socket;
            while (true) {
                //The socket responsible for monitoring is used to receive client connections
                try {
                    //Create a socket responsible for communication
                    socketSend = socket.Accept();
                    dictionary.Add(socketSend.RemoteEndPoint.ToString(), socketSend);
                    clientCombo.Items.Add(socketSend.RemoteEndPoint.ToString());
                    clientCombo.SelectedIndex = clientCombo.Items.IndexOf(socketSend.RemoteEndPoint.ToString());
                    Log("Connected "   socketSend.RemoteEndPoint.ToString());
                    //Start a new thread to receive information from the client
                    Thread th = new Thread(receive);
                    th.IsBackground = true;
                    th.Start(socketSend);
                } catch  {
                    continue;
                }        
            }           
        }

        //The server receives the message from the client
        private void receive(object o) {
            Socket socketSend = o as Socket;
            while (true) {
                //Try to connect to the client
                try {
                    //After the client connects successfully, the server receives the message from the client
                    byte[] buffer = new byte[1024 * 1024 * 2];//2M大小
                     //Number of valid bytes received
                    int length = socketSend.Receive(buffer);
                    if (length == 0) {
                        string tmpIp = socketSend.RemoteEndPoint.ToString();
                        Log(tmpIp   " Offline");
                        clientCombo.Items.Remove(tmpIp);
                        //Try to delete the connection information
                        try {
                            clientCombo.SelectedIndex = 0;
                        } catch  {
                            clientCombo.Text = null;
                        }
                        CloseSocket(dictionary[tmpIp]);
                        dictionary.Remove(tmpIp);                        
                        break;
                    }
                    string str = Encoding.ASCII.GetString(buffer, 0, length);
                    Log(socketSend.RemoteEndPoint.ToString()   "\n\t"   str);
                } catch {           
                    break;
                }
            }
        }

        private void Log(string str) {
            ServerLog.AppendText(str   "\r\n");
        }

        private void Form1_Load(object sender, EventArgs e) {
            //Cancel errors caused by cross-thread calls
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        //Send a message
        private void SendB_Click(object sender, EventArgs e) {
            string txt = SendText.Text;
            byte[] buffer = Encoding.UTF8.GetBytes(txt);
            try {
                string ip = clientCombo.SelectedItem.ToString();//Get the selected ip address
                Socket socketsend = dictionary[ip];
                socketsend.Send(buffer);
            } catch{
                Log("Transmission failed");
            }
        }

        private void Close_Click(object sender, EventArgs e) {
            try {
                try {
                    string tmpip = socketSend.RemoteEndPoint.ToString();                    
                    CloseSocket(dictionary[tmpip]);
                    dictionary.Remove(tmpip);
                    clientCombo.Items.Remove(tmpip);
                    try {
                        clientCombo.SelectedIndex = 0;
                    } catch {
                        clientCombo.Text = null;
                    }
                    socketSend.Close();
                    Log(socketSend.RemoteEndPoint.ToString()   "Offline");
                    socketListener.Close();
                    Log("Listener is closed");
                } catch{
                    socketListener.Close();
                    Log("Listener is closed");
                }                            
            } catch {
                Log("close error");
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
            try {
                try {
                    string tmpip = socketSend.RemoteEndPoint.ToString();
                    CloseSocket(dictionary[tmpip]);
                    dictionary.Remove(tmpip);
                    clientCombo.Items.Remove(tmpip);
                    try {
                        clientCombo.SelectedIndex = 0;
                    } catch {
                        clientCombo.Text = null;
                    }
                    socketSend.Close();
                    socketListener.Close();
                } catch {
                    socketListener.Close();
                }
            } catch {
            }
        }

        private void CloseSocket(Socket o) {
            try {
                o.Shutdown(SocketShutdown.Both);
                o.Disconnect(false);
                o.Close();
            } catch  {
                Log(o.ToString()   "error");
            }           
        }
    }
}

OutPut:

Closing the window causes disconnection:

enter image description here

enter image description here

Disconnect manually:

enter image description here

Transfer data:

enter image description here

If you have any question about my code, please add a comment below.

  • Related