我有一些问题,我无法在家里检查它是否工作。这是代码
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();
}
}
}
我也有一个简单的程序来连接到它,然后发送一个带有数据的字符串。它在本地主机上运行良好,但是当我尝试连接不同的计算机时出现问题。
我什至关闭了我的 PC 和路由器上的防火墙,就像我在朋友的笔记本电脑上所做的那样。每次我尝试连接时,他的电脑都拒绝连接。也许我做错了什么?
当然,ipAddress
现在是一个本地地址,因为它目前只使用它。有什么建议该怎么做吗?