无法从远程计算机连接 [英] 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();
}
}
}
我有一个简单的程序作为以及连接到这一点,然后发送数据的字符串。它工作在本地主机上正常,但有当我试图用不同的coputer连接问题。
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.
我甚至关掉防火墙我的电脑和路由器上,如我做了我朋友的笔记本电脑。每次我都试图连接的时候,他的电脑拒绝连接。也许我做错了什么?
I even turned off the firewall on my PC and router, as I did on my friend's laptop. Every time I have tried to connect, his computer refused connection. Maybe I'm doing something wrong?
当然, ip地址
现在是一个本地地址,因为它只能工作那的时刻。任何建议怎么办?
Of course, ipAddress
is a local address now since it's only working with that at the moment. Any suggestions what to do?
推荐答案
您需要将其设置为接受来自任何IP连接,还有一个ip地址超载功能这样的:
You need to set it to accept connections from any IP, there is an IPAddress overload function for this:
System.Net.IPAddress.Any
用它代替127.0.0.1,它会解决您的问题。
use it instead of 127.0.0.1 and it will fix your problem.
这篇关于无法从远程计算机连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!