3

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);
            }
        }
Evorlor
  • 7,263
  • 17
  • 70
  • 141
User23289
  • 33
  • 1
  • 1
  • 5

1 Answers1

4

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

Kas
  • 3,747
  • 5
  • 29
  • 56
  • 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. – User23289 Nov 10 '13 at 07:07
  • sck is a Socket or a TCPSocket ? – Kas Nov 10 '13 at 07:09
  • sck is socket object as Socket sck; – User23289 Nov 10 '13 at 09:01
  • 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. – User23289 Nov 10 '13 at 09:16
  • 1
    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. – Kas Nov 10 '13 at 09:18
  • As I know actully your socket act as a Listner for incoming connections, In that case you should specify one single EndPoint object. – Kas Nov 10 '13 at 09:22
  • i jst want to connect two sockets and start chatting between them, if there is any other way for chat application will implement that, Thank You. – User23289 Nov 10 '13 at 09:24
  • You only need one listener for a chat application , Why do you create two listners ? – Kas Nov 10 '13 at 09:26
  • i am very new in socket programming, i have downloaded video from youtube and trying this first time will u guide me some more Thanks U a lot. – User23289 Nov 10 '13 at 09:41