将 VM Client 连接到我计算机上的服务器 C#
Connecting VM Client To Server On My Computer C#
我在使用我找到的这个网络聊天应用程序代码时遇到了问题。当两个应用程序 运行 在同一台计算机上时,它工作正常,但是当尝试将 VM 客户端连接到我的主机应用程序时 运行 在我的实际安装中,我遇到超时。这是代码。
static readonly object _lock = new object();
static readonly Dictionary<int, TcpClient> list_clients = new Dictionary<int, TcpClient>();
static void Main(string[] args) {
int count = 1;
TcpListener ServerSocket = new TcpListener(IPAddress.Parse("192.168.1.59"), 5000);
ServerSocket.Start();
while(true) {
TcpClient client = ServerSocket.AcceptTcpClient();
lock(_lock) list_clients.Add(count, client);
Console.WriteLine("Someone connected!!");
Thread t = new Thread(handle_clients);
t.Start(count);
count++;
}
}
}
class Client {
static void Main(string[] args) {
IPAddress ip = IPAddress.Parse("192.168.1.59");
int port = 5000;
TcpClient client = new TcpClient();
client.Connect(ip, port);
Console.WriteLine("client connected!!");
NetworkStream ns = client.GetStream();
Thread thread = new Thread(o => ReceiveData((TcpClient)o));
thread.Start(client);
string s;
while(!string.IsNullOrEmpty((s = Console.ReadLine()))) {
byte[] buffer = Encoding.ASCII.GetBytes(s);
ns.Write(buffer, 0, buffer.Length);
}
client.Client.Shutdown(SocketShutdown.Send);
thread.Join();
ns.Close();
client.Close();
Console.WriteLine("disconnect from server!!");
Console.ReadKey();
}
}```
您的 VM 可能 运行 在不同的网络接口中。
试试看:
TcpListener ServerSocket = new TcpListener(IPAddress.Any, 5000);
这样你就可以告诉服务器监听任何网络接口。
补充:此外,VM 客户端应连接到您的计算机(使用 TCPListener 程序)在 VM 网络范围内的 IP 地址。不是 192.168.1.59,而是:
static void Main(string[] args) {
IPAddress ip = IPAddress.Parse("172.17.xx.xx");
...
我在使用我找到的这个网络聊天应用程序代码时遇到了问题。当两个应用程序 运行 在同一台计算机上时,它工作正常,但是当尝试将 VM 客户端连接到我的主机应用程序时 运行 在我的实际安装中,我遇到超时。这是代码。
static readonly object _lock = new object();
static readonly Dictionary<int, TcpClient> list_clients = new Dictionary<int, TcpClient>();
static void Main(string[] args) {
int count = 1;
TcpListener ServerSocket = new TcpListener(IPAddress.Parse("192.168.1.59"), 5000);
ServerSocket.Start();
while(true) {
TcpClient client = ServerSocket.AcceptTcpClient();
lock(_lock) list_clients.Add(count, client);
Console.WriteLine("Someone connected!!");
Thread t = new Thread(handle_clients);
t.Start(count);
count++;
}
}
}
class Client {
static void Main(string[] args) {
IPAddress ip = IPAddress.Parse("192.168.1.59");
int port = 5000;
TcpClient client = new TcpClient();
client.Connect(ip, port);
Console.WriteLine("client connected!!");
NetworkStream ns = client.GetStream();
Thread thread = new Thread(o => ReceiveData((TcpClient)o));
thread.Start(client);
string s;
while(!string.IsNullOrEmpty((s = Console.ReadLine()))) {
byte[] buffer = Encoding.ASCII.GetBytes(s);
ns.Write(buffer, 0, buffer.Length);
}
client.Client.Shutdown(SocketShutdown.Send);
thread.Join();
ns.Close();
client.Close();
Console.WriteLine("disconnect from server!!");
Console.ReadKey();
}
}```
您的 VM 可能 运行 在不同的网络接口中。
试试看:
TcpListener ServerSocket = new TcpListener(IPAddress.Any, 5000);
这样你就可以告诉服务器监听任何网络接口。
补充:此外,VM 客户端应连接到您的计算机(使用 TCPListener 程序)在 VM 网络范围内的 IP 地址。不是 192.168.1.59,而是:
static void Main(string[] args) {
IPAddress ip = IPAddress.Parse("172.17.xx.xx");
...