java编写网络聊天程序,群聊怎么实现
本文实例为大家分享了爪哇岛网络编程之群聊功能的具体代码,供大家参考,具体内容如下
1、服务端
包装网络编码;导入Java。io。数据输入流;导入Java。io。数据输出流;导入Java。io。io异常;导入Java。网。服务器套接字;导入Java。网。插座;导入Java。util。并发。copyonwritearraylist/**** 1, 指定端口,使用服务器创建服务器* 2, 阻塞式等待连接接受* 3,操作:输入输出流操作* 4, 释放资源** 5,加入容器实现群聊* * * */public class WeiHuShanChatRoomServer { private static CopyOnWriteArrayListChat all=new CopyOnWriteArrayListChat();公共静态void main(String[] args)抛出io异常{系统。出去。println(-server );//1, 指定端口,使用服务器创建服务器服务器套接字server=新服务器套接字(9999);//2, 阻塞式等待连接接受while(true){ Socket client=server。接受();聊天聊天=新聊天(客户端);//交给容器管理all.add(聊天);新线索(聊天)。start();} }静态类闲谈实现Runnable {私有数据输出流dos私有数据输入流阴间私有套接字客户端;私有布尔isrunning私有字符串名称;公共聊天(套接字客户端){ this.client=clientthis。正在运行=真;试试这个。dis=新数据输入流(客户端。getinputstream());这个。dos=新数据输出流(客户端。获取输出流());这个。name=receive();this.send(this.name ,威虎山欢迎你的到来);this.sendOthers(this.name 来到了威虎山,真);} catch (IOException e) { //出错释放资源系统。出去。println(===1==);这个。发布();} }私有字符串receive(){ String data=" ";请尝试{ data=dis。读取utf();} catch(io异常e){ system。出去。println(===2==);//这个。发布();}返回数据;} //群聊private void send(字符串数据){ try { dos。写utf(数据);dos。flush();} catch(io异常e){ system。出去。println(===3==);
this.release(); } } private void sendOthers(String data,boolean isSys) { boolean isPrivate = data.startsWith("@"); if(isPrivate) { int index= data.indexOf(":"); String targetName=data.substring(1,index); String msg=data.substring(index+1); for (Chat chat : all) { if(chat.name.equals(targetName)) { System.out.println("******"+chat.name+targetName); chat.send(this.name+"悄悄对你说:"+msg); } } }else { for (Chat chat : all) { if(chat==this) { continue; }else { if(isSys) { chat.send(data); }else { chat.send(this.name+"对大家说:"+data); } } } } } private void release() { this.isRuning=false; Utils.close(dis,dos,client); all.remove(this); sendOthers(this.name+"离开了威虎山", true); } @Override public void run() { while(isRuning) { String data = receive(); if(!data.equals("")) { sendOthers(data,false); }else { send("不能发送空消息"); } } } } }2、客户端
package networkCoding; import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket;import java.net.UnknownHostException; /** * * 1, 建立连接,使用socket 创建客户端 + 服务端的地址端口号; * 2, 操作:输入输出流操作 * 3, 释放资源 * * **/ public class WeiHuShanChatRoomClient { public static void main(String[] args) throws UnknownHostException, IOException { BufferedReader bf= new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入姓名"); String bfString = bf.readLine(); //1, 建立连接,使用socket 创建客户端 + 服务端的地址端口号; Socket client = new Socket("localhost",9999); // 2, 操作:输入输出流操作 new Thread(new Send(client,bfString)).start(); new Thread(new Receive(client)).start(); } }
(1)发送封装类
package networkCoding; import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket; public class Send implements Runnable{ private BufferedReader bf; private DataOutputStream dos; private Socket client; private boolean isRuning; private String name; public Send(Socket client,String name) { this.client=client; this.isRuning=true; this.name=name; this.bf= new BufferedReader(new InputStreamReader(System.in)); try { this.dos=new DataOutputStream(client.getOutputStream()); this.send(name); } catch (IOException e) { // 出错释放资源 System.out.println("===4=="); this.release(); this.isRuning=false; } } private void release() { this.isRuning=false; Utils.close(dos,client); } private void send(String data) { try { dos.writeUTF(data); dos.flush(); } catch (IOException e) { System.out.println("===5=="); this.release(); } } private String getString() { String dataString =""; try { dataString = this.bf.readLine(); } catch (IOException e) { System.out.println("===6=="); this.release(); } return dataString; } @Override public void run() { // TODO Auto-generated method stub while(isRuning) { String data = getString(); if(!data.equals("")) { send(data); }else { //send("不能发送空消息"); } } } }
(2)接收封装类
package networkCoding; import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket; public class Receive implements Runnable { private DataInputStream dis; private Socket client; private boolean isRuning; public Receive(Socket client) { this.client=client; this.isRuning=true; try { this.dis = new DataInputStream(client.getInputStream()); } catch (IOException e) { // 出错释放资源 System.out.println("===6=="); this.release(); this.isRuning=false; } } private String receive() { String data=""; try { data= dis.readUTF(); } catch (IOException e) { System.out.println("===7=="); this.release(); } return data; } private void release() { this.isRuning=false; Utils.close(dis,client); } @Override public void run() { while(isRuning) { String data = receive(); if(!data.equals("")) { System.out.println(data); }else { } } } }
3、工具类
package networkCoding; import java.io.Closeable;import java.io.IOException; public class Utils { public static void main(String[] args) { } public static void close(Closeable...target) { for (Closeable obj : target) { try { if(null!=obj) { obj.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。