关闭

Java网络编程——利用Socket实现简单的网上聊天功能

标签: java网络编程socket聊天
280人阅读 评论(0) 收藏 举报
分类:

一、聊天功能的实现

之前的文章里学习了Java中Socket编程的一些基本内容,现在简单写一个小程序,能够实现服务器端和客户端的聊天的功能。


服务器端:

Server.java

public class Server {
	public static void main(String[] args) throws IOException {
		ServerSocket server = null;
		Socket client = null;
		InputStream in = null;
		InputStreamReader reader = null;
		BufferedReader bufReader = null;
		OutputStream out = null;
		PrintWriter writer = null;
		Scanner scanner = new Scanner(System.in);
		boolean end = false;
		try {
			//1、创建服务器端socket,并绑定端口,绑定端口之后服务器一直监听该端口
			server = new ServerSocket(9999);
			System.out.println("------服务器端启动,开始监听客户端的信息,发送bye结束聊天------");
			
			while(true) {
				//2、接收到客户端请求,建立连接
				client = server.accept();
				//3、获取客户端的请求信息并解析
				in = client.getInputStream();
				reader = new InputStreamReader(in);
				bufReader = new BufferedReader(reader);
				String msg = null;
				while((msg = bufReader.readLine()) != null) {
					System.out.println("客户端说:" + msg);
					if("bye".equals(msg.trim().toLowerCase())) {
						end = true;
						break;
					}
				}
				//4、关闭输入流
				client.shutdownInput();
				if(end) {
					System.out.println("------聊天结束------");
					return;
				}
				
				System.out.print("我说:");
				String myMsg = scanner.nextLine();
				//5、通过输出流向客户端发送信息
				out = client.getOutputStream();
				writer = new PrintWriter(out);
				writer.write(myMsg);
				writer.flush();
				//6、关闭输出流
				client.shutdownOutput();
				if("bye".equals(myMsg.trim().toLowerCase())) {
					System.out.println("------聊天结束------");
					return;
				}
			}		
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//7、程序结束一定要关闭相关资源
			if(in != null)
				in.close();
			if(reader != null) 
				reader.close();
			if(bufReader != null)
				bufReader.close();
			if(out != null)
				out.close();
			if(writer != null)
				writer.close();
			if(scanner != null)
				scanner.close();
			if(client != null)
				client.close();
			if(server != null)
				server.close();
		}
	}
}
客户端:

Client.java

public class Client {
	public static void main(String[] args) throws IOException {
		Socket client = null;
		InputStream in = null;
		InputStreamReader reader = null;
		BufferedReader bufReader = null;
		OutputStream out = null;
		PrintWriter writer = null;
		Scanner scanner = new Scanner(System.in);
		boolean end = false;
		try {
			 System.out.println("------准备向服务器发送消息,发送bye结束聊天------");
			 while(true) {
				 //1、新建客户端socket,指明需要连接的服务器地址和端口号
				 //如果是本机,地址可以是localhost或者127.0.0.1
				 client = new Socket("127.0.0.1", 9999);
				 System.out.print("我说:");
				 //2、建立连接后,通过输出流向服务器端发送信息
				 String myMsg = scanner.nextLine();
				 out = client.getOutputStream();
				 writer = new PrintWriter(out);
				 writer.write(myMsg);
				 writer.flush();
				 //3、关闭输出流
				 client.shutdownOutput();
				 if("bye".equals(myMsg.trim().toLowerCase())) {
					 System.out.println("------聊天结束------");
					 return;
				 }
				 
				 //4、通过输入流获取服务器端的响应
				 in = client.getInputStream();
				 reader = new InputStreamReader(in);
				 bufReader = new BufferedReader(reader);
				 String msg = null;
				 while((msg = bufReader.readLine()) != null) {
					 System.out.println("服务器说:" + msg);
					 if("bye".equals(msg.trim().toLowerCase())) {
						 end = true;
						 break;
					 }
				 }
				 //5、关闭输入流
				 client.shutdownInput();
				 
				 if(end) {
					 System.out.println("------聊天结束------");
					 return;
				 } 
			 }	
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//6、程序结束一定要关闭相关资源
			if(in != null)
				in.close();
			if(reader != null) 
				reader.close();
			if(bufReader != null)
				bufReader.close();
			if(out != null)
				out.close();
			if(writer != null)
				writer.close();
			if(scanner != null)
				scanner.close();
			if(client != null)
				client.close();
		}
	}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:34008次
    • 积分:755
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:1篇
    • 译文:0篇
    • 评论:5条
    最新评论