1、 服务器端处理流程
接触一种技术,首先都要熟悉它的生命周期,这样才能从大局上看每个流程的细节。才有居高临下,势如劈竹的架势。加深我们对这种技术的理解。现在来看看socket服务器的生命周期。
socket程序,一般情况下,都是服务器与客户端成双成对出现。
服务器端socket的生命周期为:监听服务器端口,接受客户端连接,获取客户端输入、输出流,处理业务逻辑、发送响应信息,关闭客户端连接,关闭服务区监听。详情如下所示:
<<<------------- <_< 向左看
2、 客户端处理流程
socket程序的客户端的处理流程与服务器的相似,生命周期为:创建客户端socket,获取服务器输入、输出流,处理业务逻辑、发送相应信息、关闭客户端连接。详情如下所示:
3、 实例代码
首先是写一个Server类,这个类用来监听9999端口,并从这个端口接收消息然后输出,当收到“bye”时退出。
public class MyServer { public static void main(String[] args) throws IOException { int port = 9999; // 监听服务器端口 ServerSocket server = new ServerSocket(port); System.out.println("listening on " + port); while (true) { // 接受一个客户端连接 Socket socket = server.accept();
System.out.println("---------------------------"); System.out.println("the client " + socket.getInetAddress().getHostAddress() + " has connected!"); // 获取客户端的输入流 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 获取客户端的输出流
PrintWriter out = new PrintWriter(socket.getOutputStream()); String msg = in.readLine(); System.out.println("Server received " + msg); if (msg.equals("bye")) { System.out.println("Server stoped!"); break; } else { // 想客户端发送信息 System.out.println("Server send
" + msg); out.print("Server received " + msg); out.flush(); } socket.close(); } server.close(); } }
然后是一个Client类,这个类连接上面启动的Server类,然后接收任何用户输入,当遇到回车时发送字符串到Server上,当输入“bye”是退出。
public class MyClient { public static void main(String[] args) throws UnknownHostException, IOException { Scanner reader = new Scanner(System.in); while (true) { // 创建客户端socket Socket client = new Socket("localhost", 9999); System.out.println("---------------------------");
// 获取客户端的输入流(获取服务器端的输出流) BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); // PrintWriter out = new PrintWriter(client.getOutputStream()); System.out.println("Input Something "); String msg = reader.nextLine(); System.out.println("Client
sended " + msg); out.println(msg); out.flush(); if (msg.equals("bye")) { System.out.println("Client stop!"); break; } System.out.println("Client received " + in.readLine()); client.close(); } } }
4、 结果
首先运行MyServer类,然后MyClient类,然后在MyClient的控制台输入任意字符。
服务器端
listening on 9999 --------------------------- the client 127.0.0.1 has connected! Server received i am here Server send i am here --------------------------- the client 127.0.0.1 has connected! Server received bye Server stoped!
客户端
--------------------------- Input Something i am here Client sended i am here Client received Server received i am here --------------------------- Input Something bye Client sended bye Client stop!
Java Socket之单线程通信,布布扣,bubuko.com