我有一個C#WPF應用程序,它使用TCPListener在我的電腦中啓動一個服務器,以及一個作爲客戶端的Android應用程序。當我啓動服務器並在我的計算機上模擬應用程序時,它完美地工作,但大多數情況下,當我使用智能手機連接到我的計算機時,它不起作用,它只在重新啓動路由器DHCP後才起作用,我的智能手機套接字連接將無法在智能手機和電腦之間工作
如果需要,這裏的連接代碼
服務器:
private static IPAddress ipAd;
private static TcpListener server;
private static TcpClient client;
public static void start() {
//Already tried with both
ipAd = IPAddress.Parse(TCPServer.GetLocalIPAddress());
//ipAd = IPAddress.Parse("127.0.0.1");
server = new TcpListener(ipAd, 1209);
client = default(TcpClient);
try {
server.Start();
Console.WriteLine("Server started");
} catch {
Console.WriteLine("Failed to start server");
}
客戶:
Socket socket = null;
String response = new String();
try{
//connect This ipAddress is the same in my desktop
InetAddress ipAd = InetAddress.getByName(ipAddress);
socket = new Socket(ipAd, 1209);
//send
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.write(message.getBytes());
}
...
感謝您的時間,我應該有專門的更合我的計算機網絡類...
我應該重定向一些特定的端口嗎?在我的路由器/防火牆中有一些特定的配置?我想我錯過了一些東西
我有點懷疑它與你的路由器有關。當它失敗時會得到什麼錯誤? – zapl