我创建了一个Java项目,使用TCP套接字在服务器和客户端之间建立通信。客户端具有服务器的IP地址和公共端口。
服务器:
ServerSocket welcomeSocket = new ServerSocket(80);
int id=1;
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
WorkerThread wt = new WorkerThread(connectionSocket, id);
Thread t = new Thread(wt);
t.start();
workerThreadCount++;
System.out.println("Client [" + id + "] is now connected. No. of worker threads = " + workerThreadCount);
id++;
}
客户端:
Socket skt = new Socket("103.94.135.201", 80);
outToServer = new PrintWriter(skt.getOutputStream(), true);
inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));
当两端都在同一个WiFi中时,所有方法都可以使用。但是当客户端使用蜂窝数据时,它会令人沮丧地失败。
有解决方案吗?