IpEndPoint problem
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 get the ip-address of the remote host, which is displayed in the list, and accordingly, to start receiving messages, I need to create IPEndPoint
( clientIP
).
newSock
is a variable of type socket, initialized as:
newSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
The problem is on the line where I bind the newSock socket to the IPIndPoint clientIP, where I get the error that it is an invalid address. However, to cross-check, I tried to display the ip address in the message box, which it did correctly. So what exactly is going wrong?
a source to share
You cannot bind a socket to a remote host address. It is used to indicate which incoming IP address you should listen to. You must specify one of your own IPs (if you only want to listen on one IP), or specify IPAddress.Any
(0.0.0.0) to listen on all IPs you have.
By the way, if you want to connect to a remote address, you shouldn't use it Bind
at all. You just use the methodConnect
a source to share