无法从远程机器连接

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...
");
            tcpListener.Stop();
        }
    }
}

我也有一个简单的程序来连接它,然后发送一个带有数据的字符串.它在 localhost 上运行良好,但是当我尝试连接不同的计算机时出现问题.

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.

我什至关闭了 PC 和路由器上的防火墙,就像我在朋友的笔记本电脑上所做的那样.每次我尝试连接时,他的电脑都拒绝连接.也许我做错了什么?

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?

当然,ipAddress 现在是一个本地地址,因为它目前只使用该地址.有什么建议吗?

Of course, ipAddress is a local address now since it's only working with that at the moment. Any suggestions what to do?

推荐答案

你需要设置它接受来自任何IP的连接,这里有一个IPAddress重载函数:

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.

这篇关于无法从远程机器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法从远程机器连接

基础教程推荐