简体   繁体   中英

TCP Connection failed

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.

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.

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