TCP 连接失败

发布于 2024-12-10 15:18:48 字数 3033 浏览 0 评论 0原文

我厌倦了找出我的代码出了什么问题......请帮助我!

我正在尝试在 LAN 中的服务器客户端之间创建 TCP 连接。发生的情况是,当客户端尝试连接时程序崩溃。 请参阅代码:

public void Go()
        {
            if (whatjob == true)
            {
                IPEndPoint server_ipEndPoint = new IPEndPoint(IPAddress.Any, PortNum);
                server_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server_socket.Bind(server_ipEndPoint);
                try
                {

                    server_socket.Listen(1);
                    server_GotClient = server_socket.Accept();
                }
                catch (SocketException e)
                {
                    MessageBox.Show("Unable to listen...");
                    MessageBox.Show(e.ToString());
                }


                reading = new Thread(new ThreadStart(this.get_msg));
                reading.Start();
            }

            if (whatjob == false)
            {
                IPEndPoint client_ipEndPoint = new IPEndPoint(IPAddress.Parse(IpAddress), PortNum);
                client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    client_socket.Blocking = true;
                    client_socket.Connect(client_ipEndPoint); //here it stops execution
                    //client_socket.BeginConnect(client_ipEndPoint, new AsyncCallback(NowConnected), client_socket);
                }
                catch (SocketException e)
                {
                    MessageBox.Show("Unable to connect...");
                    MessageBox.Show(e.ToString());
                    return;
                }
                reading = new Thread(new ThreadStart(this.get_msg));
                reading.Start();
            }

whatjob is bool... true 表示执行服务器工作, false 表示执行客户端工作

此功能位于聊天窗口的 Windows FORM 内。当它到达 client_socket.connect(ipendpoint) 时,它崩溃了,甚至聊天窗口变形了......

线程函数

public void get_msg()
        {
            byte[] byte_message = new byte[1000];
            string string_message = null;
            int x = 0;
            while (true)
            {
                if (server_GotClient != null)
                {
                    x = server_GotClient.Receive(byte_message);
                }
                if (client_socket != null)
                {
                    x = client_socket.Receive(byte_message);
                }

                if (x != 0)
                {
                    string_message = Encoding.ASCII.GetString(byte_message);
                    this.richTextBox_GetMessage.Invoke(new MethodInvoker(delegate
                    {
                        richTextBox_GetMessage.Text = richTextBox_GetMessage.Text + "\nFriend: " + string_message;

                    }));
                }
                x = 0;
            }
            server_socket.Close();
        }

请帮助解决此代码。 进一步补充一下,我对异步技术一无所知,请帮助我一些简单的方法。

I am tired to figuring out what wrong with my code... Please help me out here!

I am trying to create a TCP connection between server client in LAN. What happens is, that the program crashes when client tries to connect.
Please see the code:

public void Go()
        {
            if (whatjob == true)
            {
                IPEndPoint server_ipEndPoint = new IPEndPoint(IPAddress.Any, PortNum);
                server_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server_socket.Bind(server_ipEndPoint);
                try
                {

                    server_socket.Listen(1);
                    server_GotClient = server_socket.Accept();
                }
                catch (SocketException e)
                {
                    MessageBox.Show("Unable to listen...");
                    MessageBox.Show(e.ToString());
                }


                reading = new Thread(new ThreadStart(this.get_msg));
                reading.Start();
            }

            if (whatjob == false)
            {
                IPEndPoint client_ipEndPoint = new IPEndPoint(IPAddress.Parse(IpAddress), PortNum);
                client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    client_socket.Blocking = true;
                    client_socket.Connect(client_ipEndPoint); //here it stops execution
                    //client_socket.BeginConnect(client_ipEndPoint, new AsyncCallback(NowConnected), client_socket);
                }
                catch (SocketException e)
                {
                    MessageBox.Show("Unable to connect...");
                    MessageBox.Show(e.ToString());
                    return;
                }
                reading = new Thread(new ThreadStart(this.get_msg));
                reading.Start();
            }

whatjob is bool... true means do server job and false means do client job

this function is inside windows FORM for chat window. when it reaches until client_socket.connect(ipendpoint), it crashed and even chat window is deformated...

Threaded Function

public void get_msg()
        {
            byte[] byte_message = new byte[1000];
            string string_message = null;
            int x = 0;
            while (true)
            {
                if (server_GotClient != null)
                {
                    x = server_GotClient.Receive(byte_message);
                }
                if (client_socket != null)
                {
                    x = client_socket.Receive(byte_message);
                }

                if (x != 0)
                {
                    string_message = Encoding.ASCII.GetString(byte_message);
                    this.richTextBox_GetMessage.Invoke(new MethodInvoker(delegate
                    {
                        richTextBox_GetMessage.Text = richTextBox_GetMessage.Text + "\nFriend: " + string_message;

                    }));
                }
                x = 0;
            }
            server_socket.Close();
        }

Please help regarding this code.
to add further, I have no knowledge of asynchronous techniques, help me with some easy methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2024-12-17 15:18:48

了解完整的错误消息将有所帮助。

根据您所显示的内容,我的想法是引发的异常不是 SocketException。 Socket.Connect 的 MSDN 文档显示它可以抛出四种异常中的任何一种,包括 SocketException。

我会将 catch 中的异常类型更改为异常,并在左括号上设置断点。下次失败时,您将确切地知道出了什么问题,并且可以从那里捕获并处理它。

另外,我注意到,虽然您的客户端作业确实会在连接错误时从该方法返回,但服务器作业不会;异常被捕获,消息显示,然后继续执行 reading = new Thread(new ThreadStart(this.get_msg));。这可能会导致抛出您没有预料到的其他异常。除非您确定这是客户端作业流程失败的原因,否则我会注意此方法的另一面。

至于多线程的帮助,有很多资源,包括有关 SO 的其他问题,可以帮助您入门。您到底想要多线程什么,以及您需要如何控制它,尚不清楚,因此我尝试给出的任何答案都可能会出现错误,这些错误会让您进一步沮丧,因为这不是正确的方法。

It would help to know the full error message.

From what you've shown, my thoughts are that the exception being thrown is not a SocketException. The MSDN docs for Socket.Connect show that it can throw any of four exceptions, including SocketException.

I would change the type of exception in the catch to just an Exception, and set my breakpoint on the opening bracket. The next time it fails you'll know exactly what is going wrong, and you can catch and handle it from there.

Also, I notice that while your client job does return out of this method on a connection error, the server job does not; the exception is caught, the messages show, and then execution continues with reading = new Thread(new ThreadStart(this.get_msg));. This will likely cause additional exceptions, which you aren't expecting, to be thrown out. Unless you're sure it's the client job flow failing here, I would pay some attention to the other side of this method.

As for help with multithreading, there are many resources, including other questions on SO, that can help you get started. Exactly what you want to multithread, and how you need to be able to control it, is not known, so any answer I try to give would probably have errors that would frustrate you further because it wasn't the right approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文