Reputation: 1039
I am trying to establish a connection to any IP on that port. Originally, I had it said to 10.0.0.7, which was the IP of another computer on my network, so I could test the client/server. However, I want it to work with any computer without having to change the IP Address to 10.0.0.7 I tried changing it to IPAddress.Any, as the name made it seem like it would accept any IP. Evidently, it didn't because now I'm getting an error. I'm confused. Below is my entire main method, which is the only method so far.
TcpClient client = new TcpClient(IPAddress.Any.ToString() , 1200);
NetworkStream stream = client.GetStream();
string messageToSend;
byte[] messageBytes;
while (true)
{
try
{
Console.WriteLine("Type a message to send");
messageToSend = Console.ReadLine();
messageBytes = Encoding.Unicode.GetBytes(messageToSend);
stream.Write(messageBytes, 0, messageBytes.Length);
}
catch
{
}
Upvotes: 3
Views: 10808
Reputation: 4913
Your example can't work with IpAddress.Any
.
You have to provide the ip of the server.
A client has to connect to a server with a given IP address.
But a server can listen for any IpAddress.
Reference :
https://msdn.microsoft.com/fr-fr/library/system.net.ipaddress.any(v=vs.110).aspx
Upvotes: 2