C# SocketException 未处理

C# SocketException was unhandled

我刚刚浏览了关于 tcpip 服务器和客户端的 YouTube 教程,并使用了其中的代码:https://www.youtube.com/watch?v=uXFso7xSSWk 第 1 部分和第 2 部分。我重写了两次代码并确保一切正常相同。每当我 运行 服务器时,我都会在以下方法中的“tcpListener.Start();”行收到错误 "SocketException was unhandled"。

private void TcpServerRun()
        {
            TcpListener tcpListener = new TcpListener(IPAddress.Any, 5004);
            tcpListener.Start();
            updateUI("Listening");
            while (true)
            {
                TcpClient client = tcpListener.AcceptTcpClient();
                updateUI("Connected");
                Thread tcpHandlerThread = new Thread (new ParameterizedThreadStart(tcpHandler));
                tcpHandlerThread.Start(client);
            }
        }

当我 运行 客户端时,我也遇到同样的错误,但它在以下方法中的 'client.Connect(IPAddress.Parse("10.3.29.252"), 5004);' 行:

private void ConnectAsClient()
        {
            TcpClient client = new TcpClient();
            client.Connect(IPAddress.Parse("10.3.29.252"), 5004);
            NetworkStream stream = client.GetStream();
            string s = "Hello from client";
            byte[] message = Encoding.ASCII.GetBytes(s);
            stream.Write(message, 0, message.Length);
            updateUI("Message send");
            stream.Close();
            client.Close();
        }

非常感谢任何和所有的帮助。我对编码很陌生,对 c# 也很陌生,所以对于任何可能不清楚的地方,我深表歉意。

这是服务器的完整代码:

namespace TcpServerTutorial
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void bStartServer_Click(object sender, EventArgs e)
        {
            Thread tcpServerRunThread = new Thread(new ThreadStart(TcpServerRun));
            tcpServerRunThread.Start();
            TcpServerRun();
        }

        private void TcpServerRun()
        {
            TcpListener tcpListener = new TcpListener(IPAddress.Any, 5004);
            tcpListener.Start();
            updateUI("Listening");
            TcpClient client = tcpListener.AcceptTcpClient();
            while (true)
            {
                TcpClient client = tcpListener.AcceptTcpClient();
                updateUI("Connected");
                Thread tcpHandlerThread = new Thread (new ParameterizedThreadStart(tcpHandler));
                tcpHandlerThread.Start(client);
            }


        }

        private void tcpHandler(object client)
        {
            TcpClient mClient = (TcpClient)client;
            NetworkStream stream = mClient.GetStream();
            byte[] message = new byte[1024];
            stream.Read(message, 0, message.Length);
            updateUI("New Message = " + Encoding.ASCII.GetString(message));
            stream.Close();
            mClient.Close();
        }

        private void updateUI(string s)
        {
            Func<int> del = delegate()
            {
                textBox1.AppendText(s + System.Environment.NewLine);
                return 0;
            };
            Invoke(del);
        }
    }
}

Bingo.Look 在这段代码中:

    private void bStartServer_Click(object sender, EventArgs e)
    {
        // Called once when the thread starts
        Thread tcpServerRunThread = new Thread(new ThreadStart(TcpServerRun));
        tcpServerRunThread.Start();
        // Called again here
        TcpServerRun();
    }

TCPServerRun() 方法被调用两次:一次是在您的线程启动时,然后是在您启动线程后立即通过显式调用再次调用(甚至可能以相反的顺序)。这是 SocketException 发生的调用,因为服务器已经启动。删除第二个参考,您的问题应该得到解决;在您的客户身上寻找类似的情况。