问题
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?
回答1:
The problem almost certainly resides in the fact that you're using IPAddress.Any when building the server. The reason that's an issue is because that will not necessarily resolve to localhost, though you may get lucky, it's not consistent. So, I would recommend starting up the server with an IP address like this:
var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener server = new TcpListener(ipAddress, clientPort);
Next, though I'm confident you're doing this, ensure that the ports are the same for both clientPort and serverPort.
Next, the while (true) loop is very suspect to me so in my example below I'm going to change that. Unless impossible always avoid while (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 BackgroundWorker class (others may recommend async-await but 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 BackgroundWorker like this for the server (and you can build another one similar for the client):
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.ProgressChanged += (s, args) =>
{
Console.WriteLine(args.UserState);
}
worker.DoWork += (s, args) =>
{
// startup the server on localhost
var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener server = new TcpListener(ipAddress, clientPort);
server.Start();
while (!worker.CancellationPending)
{
// as long as we're not pending a cancellation, let's keep accepting requests
TcpClient client = server.AcceptTcpClient();
StreamReader clientIn = new StreamReader(client.GetStream());
StreamWriter clientOut = new StreamWriter(client.GetStream());
clientOut.AutoFlush = true;
while ((string msg = clientIn.ReadLine()) != null)
{
worker.ReportProgress(1, msg); // this will fire the ProgressChanged event
clientOut.WriteLine(msg);
}
}
}
Finally, somewhere you'll need to startup these workers by calling RunWorkerAsync like this:
worker.RunWorkerAsync();
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 ::1 and that is the problem. However, if we listen on 127.0.0.1:2104 the client is able to connect because that what it's trying to connect to when issuing var result = client.BeginConnect("localhost", 2104, null, null);, which would be the same as issuing new TcpClient("localhost", 2104);.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.ComponentModel;
using System.Threading;
using System.Net;
namespace ConsoleApplication13
{
class Program
{
static void Main()
{
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.ProgressChanged += (s, args) =>
{
Console.WriteLine(args.UserState);
};
worker.DoWork += (s, args) =>
{
// startup the server on localhost
var ipAddress = IPAddress.Parse("127.0.0.1");
TcpListener server = new TcpListener(ipAddress, 2104);
server.Start();
while (!worker.CancellationPending)
{
Console.WriteLine("The server is waiting on {0}:2104...", ipAddress.ToString());
// as long as we're not pending a cancellation, let's keep accepting requests
TcpClient attachedClient = server.AcceptTcpClient();
StreamReader clientIn = new StreamReader(attachedClient.GetStream());
StreamWriter clientOut = new StreamWriter(attachedClient.GetStream());
clientOut.AutoFlush = true;
string msg;
while ((msg = clientIn.ReadLine()) != null)
{
Console.WriteLine("The server received: {0}", msg);
clientOut.WriteLine(string.Format("The server replied with: {0}", msg));
}
}
};
worker.RunWorkerAsync();
Console.WriteLine("Attempting to establish a connection to the server...");
TcpClient client = new TcpClient();
for (int i = 0; i < 3; i++)
{
var result = client.BeginConnect("localhost", 2104, null, null);
// give the client 5 seconds to connect
result.AsyncWaitHandle.WaitOne(5000);
if (!client.Connected)
{
try { client.EndConnect(result); }
catch (SocketException) { }
string message = "There was an error connecting to the server ... {0}";
if (i == 2) { Console.WriteLine(message, "aborting"); }
else { Console.WriteLine(message, "retrying"); }
continue;
}
break;
}
if (client.Connected)
{
Console.WriteLine("The client is connected to the server...");
StreamReader clientIn = new StreamReader(client.GetStream());
StreamWriter clientOut = new StreamWriter(client.GetStream());
clientOut.AutoFlush = true;
string key;
while ((key = Console.ReadLine()) != string.Empty)
{
clientOut.WriteLine(key);
Console.WriteLine(clientIn.ReadLine());
}
}
else { Console.ReadKey(); }
}
}
}
来源:https://stackoverflow.com/questions/12952679/socket-programming-in-c-sharp-the-client-never-connects-to-the-server