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

左心房为你撑大大i 提交于 2021-02-07 21:48:23

问题


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);
            }
        }

回答1:


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



来源:https://stackoverflow.com/questions/19874725/system-net-sockets-socketeexception-an-invalid-argument-was-supplied-error-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!