无法从远程计算机连接

编程入门 行业动态 更新时间:2024-10-08 20:39:21
本文介绍了无法从远程计算机连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些这样的问题,我不能在家里检查这一点,如果它的工作与否。 以下是代码

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.

更多推荐

无法从远程计算机连接

本文发布于:2023-10-16 14:07:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497790.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计算机

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!