0

In simulation of a lan-messenger in c# I am using the loopback address just for starters to check the functionality of the code.However after sending a few messages, I am getting a socket exception while using the end-connect method and then a socket exception while sending the data. Is there any reason behind this exception.

    private void button1_Click(object sender, EventArgs e)
    {

        HideCaret(this.textBox1.Handle);
        StringBuilder sb = new StringBuilder(this.textBox1.Text);
        str = this.textBox2.Text;
        Socket newsock = new Socket(AddressFamily.InterNetwork,socketType.Stream,   ProtocolType.Tcp);
        IPEndPoint ip = new IPEndPoint(localaddress, 5555);
        newsock.BeginConnect(ip, new AsyncCallback(connected), newsock);
        if (str != "")
        {
            sb.AppendLine(this.textBox2.Text);
            this.textBox1.Text = sb.ToString();
            this.textBox2.Text = "\0";
            send = Encoding.ASCII.GetBytes(str);
            try
            {
                newsock.BeginSend(send, 0, send.Length, SocketFlags.None, new AsyncCallback(sent), newsock);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("arguments incorrect in begin-send call", "Error", MessageBoxButtons.OK);
            }
            catch (SocketException)
            {
                MessageBox.Show("error in accessing socket while sending", "Error", MessageBoxButtons.OK);
            }
            catch (ObjectDisposedException)
            {
                MessageBox.Show("socket closed while sending", "Error", MessageBoxButtons.OK);
            }
            catch (Exception)
            {
                MessageBox.Show("error while sending", "Error", MessageBoxButtons.OK);
            }

        }                                 

    }

Please help

1
  • General tip: in your exception handlers, incorporate the Exception.Message string into what you display, as it contains more specific information. Commented Mar 30, 2009 at 6:43

3 Answers 3

4

It's possibly because you're not waiting for the socket to be connected before sending data. You have two options:

  1. Send the data asynchronously from your connected callback method.
  2. Send the data synchronously by blocking this thread until connected.

You accomplish #1 by combining the data to send and the socket into a new class, then passing that as state in your call to BeginConnect. Then move your send code to the callback method.

You accomplish #2 by setting up a System.Threading.ManualResetEvent and calling WaitOne after BeginConnect. Then call EndConnect in your connected callback method, followed by a Set on your ManualResetEvent. Then proceed to send as you have above.


You'll also probably learn more if you find out what kind of SocketException you're getting.

From the MSDN Documentation:

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

Sign up to request clarification or add additional context in comments.

Comments

0

Do you have a separate thread reading from the socket? If not, your socket buffers are probably filling up.

1 Comment

I have made an asynchronous call for receiving messages.
0

You can also use .Net Socket.Connect method it will block your code until connection. So, you don't need to use ManualResetEvent and calling WaitOne. And you can further use other socket asynchronous methods too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.