我有一个聊天应用程序客户端服务器,它在没有服务器的LAN上运行。
这是服务器类:
import java.net.*;
import java.io.*;
public class Server {
private ServerSocket server;
private Socket connessione;
private BufferedReader dalClient;
private PrintStream alClient;
private String name;
public Server(String name) {
this.name = name;
try {
InetAddress IP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+IP.getHostAddress());
server = new ServerSocket(1000, 5);
System.out.println("Server attivo");
connessione = server.accept();
dalClient = new BufferedReader(new InputStreamReader(connessione.getInputStream()));
alClient = new PrintStream(connessione.getOutputStream());
}
catch(IOException e) {
System.out.println(e);
}
} // Server()
public void conversazione() {
String messaggio = "";
BufferedReader t = new BufferedReader(new InputStreamReader(System.in));
try {
alClient.println("Sei connesso al server! Digita /logout per effetuare la disconnessione.\n");
while(!messaggio.equals("/logout")) {
messaggio = dalClient.readLine();
System.out.println(messaggio);
if(!messaggio.equals("/logout")) {
messaggio = t.readLine();
alClient.println(name+" scrive: "+ messaggio);
}
} // while
}
catch(IOException e) {
System.err.println("Conversazione interrotta");
}
}
}
这是客户端类:
import java.net.*;
import java.io.*;
public class Client {
private Socket connessione;
private BufferedReader dalServer;
private PrintStream alServer;
private String name;
public Client(String name) {
this.name = name;
BufferedReader t = new BufferedReader(new InputStreamReader(System.in));
try {
int porta = getLocalPort();
System.out.println("Porta locale: " + porta);
System.out.println("Inserire indirizzo IP server: ");
String indirizzo = t.readLine();
connessione = new Socket(indirizzo, 1000);
dalServer = new BufferedReader(new InputStreamReader(connessione.getInputStream()));
alServer = new PrintStream(connessione.getOutputStream());
} catch(IOException e) {
System.out.println(e);
}
} // Client()
public void conversazione() {
String messaggio = "";
BufferedReader t = new BufferedReader(new InputStreamReader(System.in));
try {
while(!messaggio.equals("/logout")) {
messaggio = dalServer.readLine();
System.out.println(messaggio);
if(!messaggio.equals("/logout")) {
messaggio = t.readLine();
alServer.println(name+" scrive: "+messaggio);
}
} // while
connessione.close();
}
catch(IOException e) {
System.out.println("Conversazione interrotta");
}
} // conversazione()
}
我还有其他两个类文件用于运行上面的代码,如下所示:
这是我执行以运行服务器端的类:
public class ProvaServer {
public static void main(String[] args) {
Server s = new Server(args[0]);
s.conversazione();
}
}
这是我执行以运行客户端的类:
public class ProvaClient {
public static void main(String[] args) {
Client c = new Client(args[0]);
c.conversazione();
}
}
如果我打开两个提示符(在同一网络中连接的同一台PC上)并且这样做,代码效果很好: - 在一个提示中我运行服务器类 - 在另一个提示符中我运行客户端类 运行客户端类后,我插入我的localhost,它运行良好。
如果我在电脑上打开提示而另一个提示在另一台电脑上(但在同一网络中连接),代码也可以正常工作 - 在一个提示(第一台)我运行服务器类 - 在另一个提示符(第2个)中,我运行客户端类 运行客户端类后,我插入另一台机器的IP地址(IPV4),它运行良好。
在这个解释之后,我问你,如何在不使用物理服务器的情况下修改代码以使代码在连接到不同网络的两台PC之间工作?