java编写的聊天室.docx

上传人:小飞机 文档编号:3159803 上传时间:2023-03-11 格式:DOCX 页数:7 大小:37.48KB
返回 下载 相关 举报
java编写的聊天室.docx_第1页
第1页 / 共7页
java编写的聊天室.docx_第2页
第2页 / 共7页
java编写的聊天室.docx_第3页
第3页 / 共7页
java编写的聊天室.docx_第4页
第4页 / 共7页
java编写的聊天室.docx_第5页
第5页 / 共7页
亲,该文档总共7页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《java编写的聊天室.docx》由会员分享,可在线阅读,更多相关《java编写的聊天室.docx(7页珍藏版)》请在三一办公上搜索。

1、java编写的聊天室/服务器端package com.day92;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.io.PrintWriter;import .ServerSocket;import .Socket;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class

2、 ChatServer extends Thread static ServerSocket server;PrintWriter out;Socket client;BufferedReader in;static List<Socket> allclient = new ArrayList<Socketpublic ChatServer(Socket client) this.client = client;public static void main(String args) throws IOException server = new ServerSocket(9999

3、);System.out.println(服务端启动成功!);Socket socket;boolean boo = true;while (boo) socket = server.accept;System.out.println(得到客户端请求。);PrintWriter outP = new PrintWriter(socket.getOutputStream,true);outP.println(欢迎光临本聊天室!);outP.println(欢迎光临);allclient.add(socket);/服务器端每过来一个请求,就开一个线程,使得服务器能同时向每个客户端发送消息/随时读取

4、本客户端发送过来的数据new ChatServer(socket).start;Override/同时运行的写到线程里面runpublic void run / TODO Auto-generated method stubtry boolean boo=true;while (boo) in = new BufferedReader(new InputStreamReader(client.getInputStream);/ 读取客户端写入的数据String str = in.readLine;if (!str.equals() | str != null) /遍历整个集合,向每一个客户端写

5、入Iterator<Socket> socketPer = allclient.iterator;while (socketPer.hasNext) Socket socketCurrent = (Socket) socketPer.next;out = new PrintWriter(socketCurrent.getOutputStream,true);out.println(str); catch (Exception e) /客户端package com.day92;import java.io.BufferedReader;import java.io.IOException

6、;import java.io.InputStreamReader;import java.io.PrintStream;import java.io.PrintWriter;import .InetAddress;import .Socket;import .UnknownHostException;/启动一个客户端public class ChatClient implements RunnableBufferedReader in;/需要读取服务器发送过来的消息PrintWriter out;/向服务器端写static Socket socket;public static void m

7、ain(String args) throws UnknownHostException, IOException / TODO Auto-generated method stub/开一个客户端socket = new Socket(localhost, 9999);/BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream);/开一个客户端线程ChatClient chatClient=new ChatClient;new Thread(chatClient).start;chatCli

8、ent.writerToServer;/向服务器发送信息的方法/把本客户端向服务器写入数据的功能单写到一个方法里面,/不能写到线程里面,因为向服务器写入数据的过程不能和其他的客户端同步public void writerToServer throws UnknownHostException, IOExceptiontry BufferedReader keyIn=new BufferedReader(new InputStreamReader(System.in);boolean boo=true;while(boo)String string=keyIn.readLine;out=new

9、PrintWriter(socket.getOutputStream,true);out.println(InetAddress.getLocalHost.getHostName+说:+string+n);/string=; catch (UnknownHostException e1) / TODO Auto-generated catch blocke1.printStackTrace; catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace;Override/开线程是为了,每个客户端都能同时得到服务器端发过来的数据/每个客户端需要同步接收到服务器发过来的信息public void run try boolean boo=true;in=new BufferedReader(new InputStreamReader(socket.getInputStream);while(boo)System.out.println(in.readLine); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号