服务器端代码:
Server kelas publik {
public static void main(String[] args) {
server ServerSocket;
mencoba{
server = ServerSocket baru(1111);
Soket soket = server.accept();
BufferedReader br = BufferedReader baru(InputStreamReader baru(socket.getInputStream(),"UTF-8"));
Sistem.keluar.println(br.readLine());
br.close();
soket.close();
server.close();
}menangkap (Pengecualian e) {
Sistem.keluar.println(e);
}
}
}
客户端代码:
Klien kelas publik {
public static void main(String[] args) {
soket soket;
Aliran Cetak ps;
mencoba {
soket = Soket baru("127.0.0.1",1111);
System.out.println("koneksi berhasil...");
System.out.println("Silakan masukan beberapa KATA ke server:");
BufferedReader br = BufferedReader baru(InputStreamReader(Sistem.in) baru);
Konten string = null;
mencoba {
konten = br.readLine();
} tangkapan (Pengecualian e) {
}
ps = new PrintStream(socket.getOutputStream(),true,"UTF-8");
ps.print(konten);
soket.close();
} tangkapan (Pengecualian e) {
Sistem.keluar.println(e);
}
}
}