tags:

views:

194

answers:

1

The prog in c#:

  private void listBox1_Click(object sender, EventArgs e)
  {
        String data = (String)this.listBox1.SelectedItem;
        data = data.TrimEnd(new char[] { '\r', '\n' });

        try
        {
            ip = Dns.GetHostAddresses(data);
        }
        catch (SocketException ex)
        {
            MessageBox.Show(ex.ErrorCode.ToString());
        }

        clientIP = new IPEndPoint(ip[0], 6000);

        newSock.Bind(clientIP);
        newSock.Listen(100);

        resetEvent.Set();
    }

In the above code, I obtain the ip-address of the remote host who is shown in the listbox and correspondingly in order to start accepting messages need to create an IPEndPoint(clientIP).

newSock is a variable of type socket initialized as:

newSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

The problem is in the line where I bind the socket newSock to the IPEndPoint clientIP where I am getting an error saying it is an invalid address. However to cross check I tried to display the ip-address on a message box which it did correctly. So what exactly is going wrong??

A: 

You shouldn't bind a socket to remote host address. It's used to indicate on which inbound IP address you should listen. You should either specify one of your own IP addresses (if you want to listen only on a single IP) or specify IPAddress.Any (0.0.0.0) to listen on all IP addresses you have.

By the way, if you want to connect to a remote address, you shouldn't use Bind at all. You'd just use the Connect method

Mehrdad Afshari