Java网络编程 - 服务器端和客户端的连接和信息传递实现
1、Client(客户端代码)
package cn.qi.Client; import java.io.*; import java.net.Socket; public class Client { Socket clientSocket; Client() {} public void write() throws IOException { // 建立连接到服务器端的socket clientSocket = new Socket("127.0.0.1",5000); // 客户端向服务器端发送消息,通过socket建立管道 OutputStream os = clientSocket.getOutputStream(); // 通过socket获取IO管道,纯IO操作 PrintWriter pw = new PrintWriter(os); pw.write("hello"); pw.flush(); // 流的关闭 pw.close(); // 关闭socket连接 clientSocket.close(); } public void read() throws IOException { // 建立连接到服务器端的socket clientSocket = new Socket("127.0.0.1",5000); // socket实现纯IO InputStream is = clientSocket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String st = br.readLine(); br.close(); System.out.println(st); // 关闭socket连接 clientSocket.close(); } public void writeAndRead() throws IOException { // 建立连接到服务器端的socket clientSocket = new Socket("127.0.0.1",5000); // 客户端向服务器端发送消息,通过socket建立管道 OutputStream os = clientSocket.getOutputStream(); // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); // bw.write("hello"); // bw.write("\n"); // bw.flush(); PrintWriter pw = new PrintWriter(os); pw.write("hello"); // 当服务器端用readline读取消息时,客户端发送消息时要附带换行符,否则服务器端就无法结束读取 pw.write("\n"); pw.flush(); // 客户端与服务器端双向通信时不能关闭IO流,否则默认认为想要关闭socket InputStream is = clientSocket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String st = br.readLine(); System.out.println(st); clientSocket.close(); } }
2、Server(服务器端代码)
package cn.qi.Server; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class Server { ServerSocket serverSocket; Server() {} public void sent() throws IOException { // 建立severSocket,监听对应端口 serverSocket = new ServerSocket(5000); while(true) { // server监听端口,监听成功,生成socket Socket socket= serverSocket.accept(); // 服务器端向客户端发送消息,通过socket建立管道 OutputStream os = socket.getOutputStream(); // 通过socket获取IO管道,纯IO操作 PrintWriter pw = new PrintWriter(os); pw.write("hello"); pw.flush(); // 流的关闭 pw.close(); // 本次请求的socket的关闭 socket.close(); } } public void getInfo() throws IOException { // 建立severSocket,监听对应端口 serverSocket = new ServerSocket(5000); while(true) { // server监听端口,监听成功,生成socket Socket socket= serverSocket.accept(); // socket实现纯IO InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String st = br.readLine(); br.close(); System.out.println(st); // 本次请求的socket的关闭 socket.close(); } } public void readAndWrite() throws IOException { // 建立severSocket,监听对应端口 serverSocket = new ServerSocket(5000); while(true) { // server监听端口,监听成功,生成socket Socket socket = serverSocket.accept(); // socket实现纯IO InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String st = br.readLine(); System.out.println(st); // 服务器端向客户端发送消息,通过socket建立管道 OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write("yes hello"); pw.flush(); pw.close(); // 本次请求的socket的关闭 socket.close(); } } }