I’m not looking for a complete written solution, I only want to know what is going wrong, as it’s part of a school assignment. Both classes are written by the teacher, so I assume something is going wrong on my computer, but I have no idea where to look. I searched for some other solutions and can’t find any which is different from this one, except low-level solutions but I also got a low-level solution from my teacher and that also doesn’t work.
The server:
var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener server = new TcpListener(ipAddress, clientPort);
server.Start();
TcpClient client = server.AcceptTcpClient(); // The server gets here
StreamReader clientIn = new StreamReader(client.GetStream());
StreamWriter clientOut = new StreamWriter(client.GetStream());
clientOut.AutoFlush = true;
while (true)
{
string msg = clientIn.ReadLine();
Console.WriteLine(msg);
clientOut.WriteLine(msg);
}
The client:
TcpClient client = new TcpClient("localhost", serverPort); //The client gets here
StreamReader clientIn = new StreamReader(client.GetStream());
StreamWriter clientOut = new StreamWriter(client.GetStream());
clientOut.AutoFlush = true;
while (true)
{
clientOut.WriteLine(Console.ReadLine());
Console.WriteLine(clientIn.ReadLine());
}
As the client is in a try-catch block, which is in a loop until he has the connection, he tries to connect to the server many times. The server isn’t catched, as AcceptTcpClient just waits for a connection, but while they are on the same ip and on the port of the other process, they never reach any connection.
The connections are started in separate threads, but the main thread seems to wait until one is finished, which is not what I expected. I tried to let them both sleep on various methods (with Thread.Sleep(1000), Thread.Sleep(0) (documentation said that another thread would be scheduled if you’d do that) and while(stopwatch<1000ms) {for(i<100000)}), none of them helped. The main thread only has some progress at the moment that the sleep of the connection-thread is gone and the connection-thread again creates the client.
The problem also arises on another W7 64 bits computer.
Does anybody know what the problem is?
The problem almost certainly resides in the fact that you’re using
IPAddress.Anywhen building the server. The reason that’s an issue is because that will not necessarily resolve tolocalhost, though you may get lucky, it’s not consistent. So, I would recommend starting up the server with an IP address like this:Next, though I’m confident you’re doing this, ensure that the ports are the same for both
clientPortandserverPort.Next, the
while (true)loop is very suspect to me so in my example below I’m going to change that. Unless impossible always avoidwhile (true), you are literally begging for a problem.Finally, surrounding how you will do threading here, you’re going to need to build two separate threads somehow and I’m going to recommend the
BackgroundWorkerclass (others may recommendasync-awaitbut I don’t know enough about it yet to recommend that and you would need to be using .NET 4.5 and I don’t know if you are).So, you could build a
BackgroundWorkerlike this for the server (and you can build another one similar for the client):Finally, somewhere you’ll need to startup these workers by calling
RunWorkerAsynclike this:Update
Alright, below is a fully working console application connecting on 2104. One thing that you need to note is that when using
var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];we are getting an IP address that looks like this::1and that is the problem. However, if we listen on127.0.0.1:2104the client is able to connect because that what it’s trying to connect to when issuingvar result = client.BeginConnect("localhost", 2104, null, null);, which would be the same as issuingnew TcpClient("localhost", 2104);.