欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

17.Socket双向通讯

时间:2023-07-01

服务端

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.ServerSocket;import java.net.Socket;public class EchoServer {private ServerSocket server;public EchoServer() throws IOException {//创建服务端套接字对象this.server = new ServerSocket(8888);System.out.println("服务器启动:"+8888+"端口监听连接请求。");}//服务器主要的处理逻辑public void service() {//s是否关闭服务端连接boolean flag = true;while(flag) {Socket socket = null;try {//获取连接的客户端套接字对象socket = server.accept();//获取socket相关的输入流和输出流BufferedReader reader = getReader(socket);BufferedWriter writer = getWriter(socket);//保存客户端发送的数据String data = null;//读取客户端发送的数据while( (data=reader.readLine()) != null ) {//当获取的信息是'bye'是,关闭流if( "byt".equals(data) ) {flag = false;if(reader != null) {reader.close();}if(writer != null) {writer.close();}break;}else {System.out.println("来自客户端的数据:"+data);//回写给客户端的数据writer.write("echo:"+data);//插入一个行分隔符,readLine()方法用来判定字符串有没有结束writer.newline();//刷新缓冲区writer.flush();}}} catch (Exception e) {//出错则结束循环flag = false;}finally {try {if(server != null && (!server.isClosed())) {server.close();System.out.println("服务端关闭");}} catch (IOException e) {e.printStackTrace();}}}}//获取socket相关的输入流对象private BufferedReader getReader(Socket socket) throws IOException {InputStream in = socket.getInputStream();return new BufferedReader(new InputStreamReader(in));}//获取socket相关的输出流对象private BufferedWriter getWriter(Socket socket) throws IOException {OutputStream out = socket.getOutputStream();return new BufferedWriter(new OutputStreamWriter(out));}public static void main(String[] args) {try {new EchoServer().service();} catch (IOException e) {e.printStackTrace();}}}


客户端

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.Socket;public class EchoClient {private Socket socket;public EchoClient() {try {//创建客户端套接字对象this.socket = new Socket("127.0.0.1",8888);} catch (IOException e) {e.printStackTrace();}}public void talkToServer() {System.out.println("请输入要发送给服务端的数据:");BufferedReader localReader = null;String data = null;//获取客户端套接字相关的输入流和输出流BufferedReader reader = getReader(socket);BufferedWriter writer = getWriter(socket);try {//创建读取用户输入的读取流localReader = new BufferedReader(new InputStreamReader(System.in));while((data=localReader.readLine()) != null){//如果客户端输入的是‘bye’则关闭IO流if("bye".equals(data)) {System.out.println("客户端关闭");if(reader!=null) {reader.close();}if(writer!=null) {writer.close();}break;}else {//readLine()方法必须读取到行分隔符才返回读取//所以传递给输入流的字符串必须包含行分隔符System.out.println("客户端输出的数据---->t"+data);writer.write(data);//插入一个行分隔符作为内容结束的标识writer.newline();//清空缓冲区writer.flush();//读取服务端返回的数据System.out.println("服务器相应的数据---->t"+reader.readLine());}}} catch (Exception e) {e.printStackTrace();}finally {//关闭客户端套接字连接try {if(socket!=null) {socket.close();}} catch (IOException e) {e.printStackTrace();}}}private BufferedReader getReader(Socket socket) {try {InputStream in = null;BufferedReader reader = null;in = socket.getInputStream();reader = new BufferedReader(new InputStreamReader(in));return reader;} catch (IOException e) {e.printStackTrace();}return null;}private BufferedWriter getWriter(Socket socket) {try {OutputStream out = null;BufferedWriter writer = null;out = socket.getOutputStream();writer = new BufferedWriter(new OutputStreamWriter(out));return writer;} catch (IOException e) {e.printStackTrace();}return null;}public static void main(String[] args) {new EchoClient().talkToServer();}}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。