我创建了两个Java程序,一个服务器和一个可以相互通信的客户端,如果它们在同一台PC上执行的话。
服务器:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(30);
ServerSocket server;
try {
server = new ServerSocket(5555);
System.out.println("Server gestartet!");
while(true){
try {
Socket client = server.accept();
//Thread t = new Thread(new Handler(client));
//t.start();
executor.execute(new Handler(client));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
处理程序(每次新客户端加入时,服务器都会创建一个实例):
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class Handler implements Runnable {
private Socket client;
public Handler(Socket client) {
this.client = client;
}
@Override
public void run() {
try{
//Streams
OutputStream out = client.getOutputStream();
PrintWriter writer = new PrintWriter(out);
InputStream in = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
// ------------------------------
String s = null;
while((s = reader.readLine()) != null){
writer.write(s + "\n");
writer.flush();
System.out.println("Empfangen vom Client: " + s);
}
writer.close();
reader.close();
client.close();
}catch(Exception e){}
}
}
客户:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
Socket client = new Socket("localhost", 5555);
System.out.println("Client gestartet!");
//Streams
OutputStream out = client.getOutputStream();
PrintWriter writer = new PrintWriter(out);
InputStream in = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
// ------------------------------
writer.write("Hallo Server!\n");
writer.flush();
String s = null;
while((s = reader.readLine()) != null){
System.out.println("Empfangen vom Server: " + s);
}
reader.close();
writer.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我现在的问题是: 如何在我的计算机上制作可以通过Internet访问的服务器?我的第一个想法是在客户端套接字的构造函数中改变一些东西
Socket client = new Socket("localhost", 5555);
"本地主机"可能意味着服务器与客户端在同一台PC上运行。
我的目标是在智能手机上安装一个应用程序,通过互联网连接到我的PC上运行的服务器。它应该发回我输入的信息。
答案 0 :(得分:0)
将客户端代码更改为Socket client = new Socket("<replace_with_your_public_ip>", 5555);
确保允许客户端打开到端口5555的传出连接(如果在Wifi中连接则通常是这种情况,但如果您使用3G / 4G则不是这样)
配置服务器所连接的路由器,以便将来自Internet 5555上的传入连接重定向到LAN上服务器的IP(仍在端口5555上)。这种设置通常可以在路由器管理控制台中的一个名为&#34; NAT&#34;的部分中找到(通常可以通过http://192.168.0.1或http://192.168.1.1访问,具体取决于路由器型号)。