社区
Java
帖子详情
如何更改socket去实现多人聊天
LOCO1993
2015-10-25 10:32:53
2楼上代码,新人刚刚学习socket,下面的代码只能实现单个客户端与服务器聊天,但是把服务器的监听设置了随时之后发现不能看了,看了两天还没完成,希望各位大神能讲解下思路,最后能在原代码上加以更改的讲解,谢谢
...全文
61
2
打赏
收藏
如何更改socket去实现多人聊天
2楼上代码,新人刚刚学习socket,下面的代码只能实现单个客户端与服务器聊天,但是把服务器的监听设置了随时之后发现不能看了,看了两天还没完成,希望各位大神能讲解下思路,最后能在原代码上加以更改的讲解,谢谢
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
LOCO1993
2015-10-25
打赏
举报
回复
client: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; import java.io.*; public class MyClient { public static void main(String[] args){ JFrame f=new JFrame("AA"); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JTextArea ta=new JTextArea(15,30); ta.setEditable(false); //文本域只读 JScrollPane sp=new JScrollPane(ta); //滚动窗格 JTextField tf=new JTextField(20); JButton b=new JButton("发送"); p1.add(sp); p2.add(tf); p2.add(b); f.add(p1,"Center"); f.add(p2,"South"); f.setBounds(300,300,360,300); f.setVisible(true); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Socket socket=null; BufferedInputStream bis=null; BufferedOutputStream bos=null; try{ socket=new Socket("127.0.0.1",5000); bis=new BufferedInputStream(socket.getInputStream()); bos=new BufferedOutputStream(socket.getOutputStream()); MyThread01 mt=new MyThread01(bis,ta); mt.start(); }catch(Exception e){ e.printStackTrace(); } b.addActionListener(new ButtonActionListener01(tf,ta,bos)); } } class ButtonActionListener01 implements ActionListener{ JTextField tf; JTextArea ta; BufferedOutputStream bos; public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){ this.tf=tf; this.ta=ta; this.bos=bos; } public void actionPerformed(ActionEvent e){ String message=tf.getText(); if(!message.equals("")){ tf.setText(""); //清空文本框 ta.append("AA:"+message+"\n"); //添加到文本域并换行 try{ bos.write(message.getBytes()); bos.flush(); }catch(Exception ex){ System.out.println("发送失败"); } } } } class MyThread01 extends Thread{ BufferedInputStream bis; JTextArea ta; public MyThread01(BufferedInputStream bis,JTextArea ta){ this.bis=bis; this.ta=ta; } public void run(){ try{ while(true){ byte[] b=new byte[100]; int length=bis.read(b); String message=new String(b,0,length); ta.append("BB:"+message+"\n"); } }catch(Exception e){ e.printStackTrace(); } } }
LOCO1993
2015-10-25
打赏
举报
回复
server: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; import java.io.*; public class MyServer { public static void main(String[] args) { JFrame f = new JFrame("BB"); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JTextArea ta = new JTextArea(12, 30); // 文本域,第一个参数为行数,第二个参数为列数 ta.setEditable(false); // 文本域只读 JScrollPane sp = new JScrollPane(ta); // 滚动窗格 JTextField tf = new JTextField(20); JButton b = new JButton("发送"); p1.add(sp); p2.add(tf); p2.add(b); f.add(p1, "Center"); f.add(p2, "South"); f.setBounds(300, 300, 360, 300); f.setVisible(true); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ServerSocket server = null; Socket socket = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { server = new ServerSocket(5000); // ta.append("等待AA连接...\n"); socket = server.accept(); // ta.append("AA已连接\n"); bis = new BufferedInputStream(socket.getInputStream()); bos = new BufferedOutputStream(socket.getOutputStream()); MyThread1 mt = new MyThread1(bis, ta); mt.start(); } catch (Exception e) { e.printStackTrace(); } b.addActionListener(new ButtonActionListener1(tf, ta, bos)); } } class ButtonActionListener1 implements ActionListener { JTextField tf; JTextArea ta; BufferedOutputStream bos; public ButtonActionListener1(JTextField tf, JTextArea ta, BufferedOutputStream bos) { this.tf = tf; this.ta = ta; this.bos = bos; } public void actionPerformed(ActionEvent e) { String message = tf.getText(); // 获取文本框中的内容 if (!message.equals("")) { tf.setText(""); // 清空文本框 ta.append("BB:" + message + "\n"); // 添加到文本域并换行 try { bos.write(message.getBytes()); bos.flush(); } catch (Exception ex) { System.out.println("发送失败!"); } } } } class MyThread1 extends Thread { BufferedInputStream bis; JTextArea ta; public MyThread1(BufferedInputStream bis, JTextArea ta) { this.bis = bis; this.ta = ta; } public void run() { try { while (true) { byte[] b = new byte[100]; int length = bis.read(b); String message = new String(b, 0, length); ta.append("AA:" + message + "\n"); } } catch (Exception e) { e.printStackTrace(); } } }
linux
多人
聊天
室
基于linux的
socket
多线程通信方式。采用sqlite3数据库存储用户信息. 能够
实现
注册、登陆、退出 私聊、群聊、修改密码、查看在线用户........
java 精美
多人
聊天
系统
4、网络通信采用MINA2.0,为整个项目省去
socket
和多线程的麻烦,好的框架才使得整个项目增添了如此之多的功能; 5、本人与朋友已经为消灭Bug做出了很多努力,但仍然不乏一些尚未
实现
或存在bug的功能,有不合理的...
Qt5(C++)高4:仿QQ
多人
聊天
室(TCP+SQL)实战
1.带领您深入学习QT5/C++:Qt开发
多人
聊天
室、Qt使用Windows的API记性通信、开发
聊天
界面,逐步提升Qt编程水平。 2.所有章节均有理论知识介绍、接口讲解、实例代码讲解,讲解过程中不断穿插老师在开发过程中遇到的...
使用C++
实现
Socket
2人
聊天
1.首先修改
聊天
对方的IP地址 2.请首先运行服务端(server)程序,再运行客户端(client)程序: 如配置正确服务端会收到相关连接信息。 3.连接成功后,需要由服务器端首先发起会话(输入消息并确认发送), ...
基于
Socket
的网络
聊天
系统
随着网络技术的发展,人们越来越离不开网络,
聊天
工具已经...本
聊天
系统可以
实现
多人
对
多人
的
聊天
,用户注册登录,基本信息修改,对好友进行添加、删除,发送消息、接收消息等功能。系统界面简洁,使用方便,功能完善。
Java
50,344
社区成员
85,704
社区内容
发帖
与我相关
我的任务
Java
Java相关技术讨论
复制链接
扫一扫
分享
社区描述
Java相关技术讨论
java
spring boot
spring cloud
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章