简体   繁体   English

如果我已经连接到服务器。 那我如何显示异常已经连接了

[英]if i have already connected to the server. then how i show exception saying it is already connected

how to add in exception showing the input has to be enter, if the user click on connect without entering any input? 如果用户在未输入任何输入的情况下单击“连接”,该如何添加异常以显示必须输入的输入? i would like the have a message box show if the user click on the connect button without entering the name, ip and port. 如果用户在不输入名称,ip和端口的情况下单击连接按钮,我希望显示一个消息框。 [SOLVED] [解决了]

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.Sockets;
using System.Threading;

namespace SocketClient
{

    public partial class SocketClient : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
        string readData = null;


        public SocketClient()
        {
            InitializeComponent();
        }


        private void getMessage()
        {
            while (true)
            {
                serverStream = clientSocket.GetStream();
                int buffSize = 0;
                byte[] inStream = new byte[10025];
                buffSize = clientSocket.ReceiveBufferSize;
                serverStream.Read(inStream, 0, buffSize);
                string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                readData = "" + returndata;
                msg();
            }
        }


        private void msg()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(msg));
            else
                textDisplay.Text = textDisplay.Text + Environment.NewLine + " >> " + readData;
        }


        private void buttonConnect_Click(object sender, EventArgs e)
        {

            readData = "Conected to NYP Server ...";
            msg();
            //clientSocket.Connect("127.0.0.1", 8888);
            clientSocket.Connect(textIP.Text, Convert.ToInt32(textPort.Text));
            serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textName.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            Thread ctThread = new Thread(getMessage);
            ctThread.Start();
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            // Show msg box if no server is connected
            if (serverStream == null)
            {
                MessageBox.Show("Please connect to a server first!");
                return;
            }

            // Send text
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textSend.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            // Clear text
            textSend.Text = "";

        }

        private void textDisplay_TextChanged(object sender, EventArgs e)
        {
            textDisplay.SelectionStart = textDisplay.Text.Length;
            textDisplay.ScrollToCaret();
            textDisplay.Refresh();
        }

        private void textSend_TextChanged(object sender, EventArgs e)
        {
            buttonSend.Enabled = !string.IsNullOrEmpty(textSend.Text);
        }
    }
}

if(string.IsNullOrEmpty(textIP.Text) || string.IsNullOrEmpty(textPort.Text)  || string.IsNullOrEmpty(textName.Text))
{
   MessageBox.Show("Please enter IP address, Port #, and Name");
}
else
{
  //they entered stuff...so, try to connect..
}

You can use TcpClient.Connected property to check whether a connection is already established or not. 您可以使用TcpClient.Connected属性来检查是否已建立连接。

if(clientSocket.Connected){
  .... show error message here
}
else{
  ... go ahead  
}

Your form holds references to both a TcpClient and a NetworkStream instance. 您的表单包含对TcpClientNetworkStream实例的引用。 Both of these implement IDisposable , so that they must be disposed of explicitly. 这两个都实现了IDisposable ,因此必须明确地将它们丢弃。 You must override the Dispose method of the form and dispose of these instances if they are not null. 您必须重写表单的Dispose方法,并处理这些实例(如果它们不为null)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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