服务原语Primitive
实现原理
对应Java命令
ServerSocket---用于建立通信连接的对象
Socket---用于通信的对象
服务原语 | Server端 | Client端 | SOCKET创建一端的socket,也是一个通信的线程,允许有多个 | ServerSocket server = new ServerSocket(PORT); 默认绑定本地IP,端口号PORT需要在初始化中定义 | Socket socket = new Socket(); 默认绑定本地IP | BIND将socket与本地地址绑定 | LISTEN监听连接的请求 | Socket?socket =?server.accept(); 此时线程阻塞,监听来自Client端的连接,当Client端发起connect命令 | | ACCEPT被动的接收连接请求 | CONNECT主动发起请求 | | socket.connect(IP:PORT); | SEND | socket.getOutputStream(); | RECEIVE | socket.getInputStream(); | CLOSE主动断开请求 | socket.close(); |
Server端代码
public class Server extends Port{
public static void main(String[] args) {
try {
//初始化ServerSocket
ServerSocket server = new ServerSocket(2000);
//阻塞等待客户端连接
//一旦建立连接,初始化Socket
Socket client=server.accept();
try {
new SendThread(client).start();
new ReplyThread(client).start();
} catch (Exception e) {
System.out.println("异常关闭");
} finally {
//SendThread和ReplyThread之一的线程释放了锁,才能关闭Socket
synchronized (lock) {
lock.wait();
}
client.close();
server.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Client端代码
public class Client extends Port{
public static void main(String[] args) {
try {
//初始化Socket
Socket client = new Socket();
client.setSoTimeout(300000);
//初始化连接服务器ServerSocket
client.connect(new InetSocketAddress(Inet4Address.getLocalHost(), 2000));
try {
new SendThread(client).start();
new ReplyThread(client).start();
} catch (Exception e) {
System.out.println("异常关闭");
} finally {
//SendThread和ReplyThread之一的线程释放了锁,才能关闭Socket
synchronized (lock) {
lock.wait();
}
client.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Server和Client继承的父类
public class Port {
public static final Object lock = new Object();
}
发送线程代码
@AllArgsConstructor
public class SendThread extends Thread {
private Socket socket;
@SneakyThrows
@Override
public void run() {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
PrintStream send=new PrintStream(socket.getOutputStream());
do {
//键盘(当前)--->程序(当前)
String sendMsg = input.readLine();
//程序(当前)--->程序(对面)
send.println(sendMsg);
if("Esc".equals(sendMsg)) break;
} while (true);
synchronized (Port.lock) {
Port.lock.notify();
}
}
}
接收线程代码
@AllArgsConstructor
public class ReplyThread extends Thread {
private Socket socket;
@SneakyThrows
@Override
public void run() {
BufferedReader reply=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream output = System.out;
do {
//程序(对面)--->程序(当前)
String replyMsg = reply.readLine();
//程序(当前)--->键盘(当前)
output.println(replyMsg);
if("Esc".equals(replyMsg)) break;
} while(true);
synchronized (Port.lock) {
Port.lock.notify();
}
}
}
|