4

I do not know why I am receiving the following error. Can anyone shed some light on this?

System.Net.Sockets.SocketeException: An invalid argument was supplied Error Code:10022 [closed]

private void btnStart_Click(object sender, EventArgs e)
        {

 try
            {
                epLocal = new IPEndPoint(IPAddress.Parse(txtIP1.Text), Convert.ToInt32(txtPort1.Text));
                sck.Bind(epLocal);
                epRemote = new IPEndPoint(IPAddress.Parse(txtIP2.Text), Convert.ToInt32(txtPort2.Text));
              //Here Error Occures I dont know what is my mistake?
                sck.Bind(epRemote);
                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
                btnStart.Text = "Connected";
                btnStart.Enabled = false;
                btnSend.Enabled = true;
                txtMessage.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
0

2 Answers 2

5

You are binding same socket to mutiple Endpoint objects , that's why you are getting an InvalidArgument exception , You can bind one socket to only one IPEndPoint object at a time.

As you are trying to listen on Public IP address and on Local IP , please try this one

 IPEndPoint ep =  new  IPEndPoint(IPAddress.Any, yourportnumber );
 sck.Bind(ep) ; 

this will create a listener that listen on All the ip addresses of your PC , Otherwise you better use a Seperate socket object

IF I were you , I would not parse local IPAddress from a textbox instead i would use something like IPAddress.Loopback

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

9 Comments

Thanks, here, as primary level im connecting my own ip as txtIP1 and txtIP2: 192.168.0.102, and port is 91 and 92 respectively. port availability is checked by netstat -a command and they are available, I am confused, what to try next.
sck is a Socket or a TCPSocket ?
sck is socket object as Socket sck;
Sorry, yet my problem is not resolve, epRemote and epLocal are two different endpoint epLocal=new IPEndPoint(IPAddress.Parse(txtIP1.Text),90); sock.Bind(epLocal); IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 91); sock.Bind(ep); still the error is same.i am sorry but not getting exact error. Please Help.
You can not bind a Socket to Two Different endpoints, You should use two different sockets resepective, or you must bind your socket to IPAddress.Any , The Argument exception occures because you are binding same Sck object to multiple endpoint objects.
|
1

In my case, this error was caused by my folder containing my .Net code being stored on a shared drive. Once I moved it to a local drive it worked. Hope this may help someone else!

1 Comment

Welcome to Stack Overflow! Thank you for your answer. Please provide more details about your solution. Code snippets, high quality descriptions, or any relevant information would be great. Clear and concise answers are more helpful and easier to understand for everyone. Edit your answer with specifics to raise the quality of your answer. For more information: How To: Write good answers. Happy coding!

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.