简体   繁体   中英

For c# language how to make disconnect the code?

I want to add disconnection button to my form also but I don't know how to make disconnect the code? How to make disconnect it? Please see the code here in the below. This is only for local database

This is for Visual Studio 2017. I'm using bare SQL and localDB in Visual Studio

    Socket sck;
    EndPoint epLocal, epRemote;
    byte[] buffer;

   //

    private void Form8_Load(object sender, EventArgs e)
    {
        sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

        textLocalIp.Text = GetLocalIP();
        textRemoteIP.Text = GetLocalIP();
    }

    private void buttonConnect_Click(object sender, EventArgs e)
    {
        epLocal = new IPEndPoint(IPAddress.Parse(textLocalIp.Text), Convert.ToInt32(textLocalPort.Text));
        sck.Bind(epLocal);
        epRemote = new IPEndPoint(IPAddress.Parse(textRemoteIP.Text), Convert.ToInt32(textRemotePort.Text));
        sck.Connect(epRemote);
        buffer = new byte[1500];
        sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
    }
    private void MessageCallBack(IAsyncResult aResult)
    {
        try
        {
            byte[] receivedData = new byte[1500];
            receivedData = (byte[])aResult.AsyncState;
            ASCIIEncoding aEncoding = new ASCIIEncoding();
            string receivedMessage = aEncoding.GetString(receivedData);
            listMessage.Items.Add("Client: " + receivedMessage);
            buffer = new byte[1500];
            sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
        } catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        }
          private void buttonSend_Click(object sender, EventArgs e)
    {
       ASCIIEncoding aEncoding = new ASCIIEncoding();
        byte[] sendingMessage = new byte[1500];
        sendingMessage = aEncoding.GetBytes(textMessage.Text);
        sck.Send(sendingMessage);
        listMessage.Items.Add("Admin: " + textMessage.Text);
        textMessage.Text = "";
    }
          private string GetLocalIP()
    {
        IPHostEntry host;
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach(IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
                return ip.ToString();
        }
        return "127.0.0.1";
    }

Create a button similar to the connect button for disconnection and use Socket.Disconnect

private void buttonDisconnect_Click(object sender, EventArgs e)
{
    if (sck.Connected)
    {
        sck.Shutdown(SocketShutdown.Both);
        sck.Disconnect(true);
    }
}

You can remove the Disconnect method to do it. Like the following:

 private void Button3_Click(object sender, EventArgs e)
        {
            if (sck.Connected)
            {
                sck.Shutdown(SocketShutdown.Both);
                MessageBox.Show(sck.Connected.ToString()); // this is to see if socket is connected.
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM