您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > java实现聊天室功能(包含全部代码-有界面)
服务器端代码:importjava.net.*;importjava.io.*;importjava.util.*;importjava.util.concurrent.*;importjavax.swing.*;publicclassServer{privatestaticfinalintPORT=6666;G_Menugm=newG_Menu();privateServerSocketserver;publicArrayListPrintWriterlist;publicstaticStringuser;publicstaticArrayListUserlist1=newArrayListUser();//定义用户集合publicUseruu;publicServer(Stringuser){this.user=user;}publicvoidgetServer(){list=newArrayListPrintWriter();try{server=newServerSocket(PORT);System.out.println(服务器启动,开始监听......);while(true){Socketclient=server.accept();//接收客户端线程PrintWriterwriter=newPrintWriter(client.getOutputStream());list.add(writer);Threadt=newThread(newChat(client));t.start();}}catch(Exceptionex){ex.printStackTrace();}}publicstaticvoidmain(String[]args){newServer(user).getServer();}classChatimplementsRunnable{Socketsocket;privateBufferedReaderbr;privateStringmsg;privateStringmssg=;publicChat(Socketsocket){try{this.socket=socket;}catch(Exceptionex){ex.printStackTrace();}}publicvoidrun(){try{br=newBufferedReader(newInputStreamReader(socket.getInputStream()));while((msg=br.readLine())!=null){if(msg.equals(1008611))//匹配字符串显示好友列表{msg=br.readLine();String[]st=msg.split(:);//将用户信息跟消息分隔开uu=newUser(st[0],st[1],socket);//将用户信息添加到User对象中list1.add(uu);//将对象添加到用户集合IteratorUserit=Server.list1.iterator();//遍历用户集合while(it.hasNext()){Useruse=it.next();msg=use.getName()+(+use.getSex()+):;mssg+=msg;//将所有的用户信息连接成一个字符串}sendMessage(1008611);//显示好友列表匹配标识sendMessage(mssg);//群发消息}elseif(msg.equals(10010))//显示说话消息{msg=br.readLine();System.out.println(msg);sendMessage(10010);//显示说话信息匹配标识sendMessage(msg);}elseif(msg.equals(10086))//显示进入聊天室{msg=br.readLine();System.out.println(msg);sendMessage(10086);//进入聊天室匹配标识sendMessage(msg);}elseif(msg.equals(841163574))//私聊{msg=br.readLine();String[]rt=msg.split(1072416535);//把传进来的用户信息跟说话内容分开System.out.println(rt[1]);//在服务器端显示说话内容String[]tg=rt[0].split(:);//因为是私聊,传过来两个用户的用户信息,这句作用是再把两个用户信息分开IteratorUseriu=Server.list1.iterator();//遍历用户集合while(iu.hasNext()){Userse=iu.next();if(tg[1].equals(se.getName()+(+se.getSex()+)))//如果传进来的用户信息跟集合中的用户信息吻合{try{PrintWriterpwriter=newPrintWriter(se.getSock().getOutputStream());//建立用户自己的流pwriter.println(841163574);//匹配标识pwriter.println(rt[1]);//向单独用户发送消息pwriter.flush();System.out.println(rt[1]);}catch(Exceptionex){ex.printStackTrace();}}elseif(tg[0].equals(se.getName()))//如果传进来的用户信息跟集合中的用户信息吻合{try{PrintWriterpwr=newPrintWriter(se.getSock().getOutputStream());//建立用户自己的流pwr.println(841163574);//匹配标识pwr.println(rt[1]);//向单独用户发送消息pwr.flush();System.out.println(rt[1]);}catch(Exceptionex){ex.printStackTrace();}}}}elseif(msg.equals(456987))//下线{msg=br.readLine();System.out.println(msg);//在服务端显示信息sendMessage(456987);//匹配字符串sendMessage(msg);//匹配完毕后群发消息String[]si=msg.split(:);//将传过来的用户名跟信息分隔开IteratorUserat=Server.list1.iterator();//遍历用户集合while(at.hasNext()){Usersr=at.next();if(sr.getName().equals(si[0]))//如果传过来的用户名跟用户集合里的用户吻合{list1.remove(sr);//将吻合的用户移除sr.getSock().close();//关闭此用户的socket}}break;}elseif(msg.equals(123654))//刷新{Stringmssge=;IteratorUseriter=Server.list1.iterator();//遍历用户集合while(iter.hasNext()){Useruus=iter.next();msg=uus.getName()+(+uus.getSex()+):;mssge+=msg;//将所有的用户信息连接成一个字符串}sendMessage(123654);//发送刷新匹配标识sendMessage(mssge);//群发消息}}}catch(IOExceptionex){ex.printStackTrace();}}}publicvoidsendMessage(Stringmessage)//群发消息方法{try{for(PrintWriterpw:list)//输出流集合{pw.println(message);pw.flush();}}catch(Exceptionex){ex.printStackTrace();}}}客户端代码:importjava.net.*;importjava.io.*;importjava.util.*;importjava.util.concurrent.*;publicclassSocket_one//客户端{privatestaticfinalintPORT=6666;//端口publicstaticStringuser;publicstaticSocketsocket;publicSocket_one(Stringuser){this.user=user;try{socket=newSocket(127.0.0.1,PORT);//建立socket连接System.out.println(【+user+】欢迎来到聊天室!);Threadtt=newThread(newRecove(socket,user));//建立客户端线程tt.start();//启动线程}catch(Exceptionex){ex.printStackTrace();}}publicstaticvoidmain(String[]args)throwsException{newSocket_one(user);}}classRecoveimplementsRunnable{publicStringuser;privateSocketsocket;privateBufferedReaderkeybord;publicBufferedReaderbr;privatePrintWriterpw;privateStringmsg;G_Menugm=newG_Menu();publicRecove(Socketsocket,Stringuser)throwsIOException{try{this.socket=socket;this.user=user;}catch(Exceptionex){ex.printStackTrace();}}publicvoidrun(){try{br=newBufferedReader(newInputStreamReader(socket.getInputStream()));while((msg=br.readLine())!=null){Stringmessage=msg;if(message.equals(1008611))//匹配字符串显示好友列表{gm.listModel1.clear();//接收前清空好友列表gm.jcomb.removeAllItems();//清空JComboxgm.jcomb.addItem(所有人);message=br.readLine();String[]str=message.split(:);//将接收到的所有用户信息分隔开for(Stringss:str){gm.listModel1.addElement(ss);//将所有用户信息添加到好友列表gm.jcomb.addItem(ss);//将所有用户信息添加到JCombox}}elseif(message.equals(841163574))//私聊{message=br.readLine();System.out.println(收到:+message);//在服务器端显示私聊消息gm.jta2.append(message+\n);//在我的频道显示私聊信息}elseif(message.equals(10010))//显示说话消息{message=br.readLine();System.out.println(收到:+message);//在服务器端显示说话信息gm.jta1.append(message+\n);//在公共频道显示说话信息gm.jt
本文标题:java实现聊天室功能(包含全部代码-有界面)
链接地址:https://www.777doc.com/doc-2236533 .html