C/C++教程

17.Socket双向通讯(ECHO程序)

本文主要是介绍17.Socket双向通讯(ECHO程序),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

服务端

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();
	}

}

这篇关于17.Socket双向通讯(ECHO程序)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!