我有一些問題,我不能檢查這在家裏,如果它的工作與否。 下面是代碼無法從遠程機器連接
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
現在是一個本地地址,因爲它現在只適用於此。有什麼建議做什麼?
據說這種方法已被棄用。我使用本地主機ip,因爲我在我的電腦上監聽傳入連接。我認爲它應該像現在這樣工作,還是我錯了? – Allek
127001只是您PC的PC自身。您需要聽取外部可見的IP,然後查看IpDdress.Any的答案。 –