这是一个使用Java Socket 进行客户端和服务器进行通信的简单例子。
有些考试里头会让写这方面的程序,自己动手写写,掌握这个实现过程。理解之后,很快就能写出来了。
而且这个题目值 10 分。
服务器端代码:Server.java
[CODE]
import java.net.*;
import java.io.*;
class Server {
private ServerSocket server;
private Socket client;
private BufferedReader in;
private PrintWriter out;
Server(){
try{
server = new ServerSocket(1234);
while(true){
//获得客户端连接
client = server.accept();
//获得客户端的IP和端口
String remoteIP = client.getInetAddress().getHostAddress();
int remotePort = client.getLocalPort();
System.out.println("A client connected. IP:" + remoteIP + ", Port: " + remotePort);
System.out.println();
//获得 client 端的输入输出流,为进行交互做准备
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
//获得 client 端发送的数据
String content = in.readLine();
System.out.println("Client message is: " + content);
// 向 client 端发送响应数据
out.println("Your message has been received successfully.");
//关闭各个流
out.close();
in.close();
server.close();
}
}catch(Exception e){
// 发送响应,接收失败
out.println("Receive Error!");
}
}
public static void main(String[] args){
new Server();
}
}
[CODE]
客户端代码:Client.java
[CODE]
import java.io.*;
import java.net.*;
class Client {
private Socket client;
String serverAddress = "127.0.0.1";
int port = 1234;
BufferedReader in;
PrintWriter out;
Client(){
try{
//连接 server 端
System.out.println("Try connect to server ...");
client = new Socket(serverAddress,port);
//显示已连接
System.out.println("Conntected to Server.");
System.out.println();
//为发送数据做准备
System.out.println("Enter some characters:");
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(client.getOutputStream(),true);
// 向 server 发送数据
out.println(in.readLine() + "/n");
System.out.println();
//接收来自 server 的响应数据
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("Message from server: " + in.readLine());
// 关闭各个流
in.close();
out.close();
client.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new Client();
}
}
[CODE]
打开两个终端:
终端一:
javac Server.java
java Server
终端二:
javac Client.java
java Client
即可看到运行结果。
附加一个更加简单的例子, client 端只接收来自 server 端的消息并显示。
服务器端代码:Server.java
[CODE]
import java.net.*;
import java.io.*;
class Server {
private ServerSocket server;
private Socket client;
private int port = 5050;
Server() {
try {
server = new ServerSocket(port);
client = server.accept();
client.getOutputStream().write("dtmobile".getBytes());
client.close();
server.close();
} catch(Exception e) {
}
}
public static void main(String[] args) {
new Server();
}
}
[CODE]
客户端代码:Client.java
[CODE]
import java.net.*;
import java.io.*;
class Client {
private Socket client;
private String host = "127.0.0.1";
private int port = 5050;
Client() {
try {
client = new Socket(host, port);
InputStream in = client.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String content = br.readLine();
System.out.println(content);
} catch(Exception e) {
}
}
public static void main(String[] args) {
new Client();
}
}