[英]Unable to connect from remote machine
I have some kind of problem and I can't check this at home if its working or not. 我有一些问题,如果不工作,我无法在家检查。 Here is the code 这是代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net.Security;
class Program
{
private static IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
private static int port = 6000;
private static string data = null;
static void Main(string[] args)
{
Thread thread = new Thread(new ThreadStart(receiveThread));
thread.Start();
Console.ReadKey();
}
public static void receiveThread()
{
while (true)
{
TcpListener tcpListener = new TcpListener(ipAddress, port);
tcpListener.Start();
Console.WriteLine("Waiting for connection...");
TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Connected with {0}", tcpClient.Client.RemoteEndPoint);
while (!(tcpClient.Client.Poll(20, SelectMode.SelectRead)))
{
NetworkStream networkStream = tcpClient.GetStream();
StreamReader streamReader = new StreamReader(networkStream);
data = streamReader.ReadLine();
if(data != null)
Console.WriteLine("Received message: {0}", data);
}
Console.WriteLine("Dissconnected...\n");
tcpListener.Stop();
}
}
}
I have a simple program as well to connect to this and then send a string with data. 我有一个简单的程序连接到这个,然后发送一个包含数据的字符串。 It works fine on localhost but there is a problem when I'm trying to connect with a different coputer. 它在localhost上工作正常,但是当我尝试连接另一台计算机时出现问题。
I even turned off the firewall on my PC and router, as I did on my friend's laptop. 我甚至关闭了我的PC和路由器上的防火墙,就像我在朋友的笔记本电脑上做的那样。 Every time I have tried to connect, his computer refused connection. 每次我尝试连接时,他的计算机都拒绝连接。 Maybe I'm doing something wrong? 也许我做错了什么?
Of course, ipAddress
is a local address now since it's only working with that at the moment. 当然, ipAddress
现在是一个本地地址,因为它目前只与它有关。 Any suggestions what to do? 有什么建议吗?
You need to set it to accept connections from any IP, there is an IPAddress overload function for this: 你需要将它设置为接受来自任何IP的连接,有一个IPAddress重载函数:
System.Net.IPAddress.Any
use it instead of 127.0.0.1 and it will fix your problem. 使用它而不是127.0.0.1,它将解决您的问题。
You're listening on 127.0.0.1 which is the loopback address which is a special address that means 'this computer'. 您正在侦听127.0.0.1这是一个环回地址,这是一个特殊的地址,意思是“这台计算机”。 This means that you will only accept connections that are made on the same machine as the server is running on. 这意味着您只接受在运行服务器的同一台计算机上建立的连接。
You need to listen on one (or more) of the server's real ip addresses. 您需要侦听服务器的一个(或多个)真实IP地址。
Your problem is that the setting an IP address explicitly when you initialize the TcpListener will only allow it to accept connections from that address . 您的问题是,在初始化TcpListener时显式设置IP地址只允许它接受来自该地址的连接。 Therefore, putting in the local address of 127.0.0.1 will only accept connections originating from your PC. 因此,输入127.0.0.1的本地地址只接受来自PC的连接。
The implementation you want to use is as follows: 您要使用的实现如下:
TcpListener tcpListener = new TcpListener(IPAddress.Any, port);
This will allows connections from any IP address to connect to your program on the specified port. 这将允许来自任何IP地址的连接连接到指定端口上的程序。
I think it is your computer (the server) that refuses to connect because 127.0.0.1 is local(-only). 我认为是你的计算机(服务器)拒绝连接,因为127.0.0.1是本地的(-only)。
Try this simple overload: 试试这个简单的重载:
TcpListener tcpListener = new TcpListener(port);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.