2018.4.28 基于java的聊天系统(带完善)

##Java聊天系统

###1.Socket类

1Socket(InetAddress address, int port) 2创建一个流套接字并将其连接到指定 IP 地址的指定端口号。 3 4 5Socket(String host, int port) 6创建一个流套接字并将其连接到指定主机上的指定端口号。 7 8 9Socket(InetAddress address, int port, InetAddress localAddr, int localPort) 10创建一个套接字并将其连接到指定远程地址上的指定远程端口。 11 12 13Socket(String host, int port, InetAddress localAddr, int localPort) 14创建一个套接字并将其连接到指定远程主机上的指定远程端口。 15 16 17 18 19close() 20关闭此套接字。 21 22connect(SocketAddress endpoint) 23将此套接字连接到服务器。 24 25connect(SocketAddress endpoint, int timeout) 26将此套接字连接到服务器,并指定一个超时值。 27 28getInetAddress() 29返回套接字连接的地址。 30 31getInputStream() 32返回此套接字的输入流。 33 34getLocalPort() 35返回此套接字绑定到的本地端口。 36 37getOutputStream() 38返回此套接字的输出流。 39 40getPort() 41返回此套接字连接到的远程端口。 42

###2.ServerSocket类

1ServerSocket(int port) 2创建绑定到特定端口的服务器套接字。 3accept() 4侦听并接受到此套接字的连接。 5getInetAddress() 6返回此服务器套接字的本地地址。 7 8Socket编程步骤 9服务器端创建ServerSocket对象,调用accept方法返回Socket对象 10客户端创建Socket对象,通过端口连接到服务器 11客户端、服务器端都使用Socket中的getInputStream方法和getOutputStream方法获得输入流和输出流,进一步进行数据读写操作 12 13(InetAddress用来描述主机地址; 14Socket用来创建两台主机之间的连接; 15ServerSocket用来侦听来自客户端的请求; 16Socket通常称作“套接字”,通常通过“套接字”向网络发出请求或者应答网络请求。) 17

###3.实现的步骤:

1 第一步: ChatUtil工具类:把一些常用的常量放进来 2 3 第二步:Server开启服务 4 第三步:ClientSocket连接服务器的socket 5 第四步:CHatFrame(添加两个属性(name,sex))2.添加了getSocket方法 6 第五步:LoginFrame设置了默认值 处理性别获得socket对象 7 8 9package com.lanqiao.demo2; 10/** 11 * 工具类 12 * @author qichunlin 13 * 14 */ 15public final class ChatUtil { 16 //地址 17 public static final String ADDRESS = "localhost"; 18 //端口 19 public static final int PORT = 9999; 20} 21 22 23 24package com.lanqiao.demo2; 25 26import java.io.IOException; 27import java.net.ServerSocket; 28import java.net.Socket; 29 30/** 31 * 服务端类 32 * @author qichunlin 33 */ 34public class Server { 35 public static void main(String[] args) { 36 try { 37 ServerSocket ss = new ServerSocket(ChatUtil.PORT); 38 int count = 0; 39 while (true) { 40 System.out.println("等待客户端连接......."); 41 Socket socket = ss.accept(); 42 count++; 43 System.out.println("目前有"+count+"个客户端进入了聊天室"); 44 } 45 46 } catch (IOException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51} 52 53 54package com.lanqiao.demo2; 55 56import java.io.IOException; 57import java.net.Socket; 58import java.net.UnknownHostException; 59 60/** 61 * 客户端的socket 62 * @author qichunlin 63 * 64 */ 65public class ClientSocket { 66 public static Socket socket; 67 public ClientSocket() { 68 try { 69 socket = new Socket(ChatUtil.ADDRESS, ChatUtil.PORT); 70 } catch (UnknownHostException e) { 71 // TODO Auto-generated catch block 72 e.printStackTrace(); 73 } catch (IOException e) { 74 // TODO Auto-generated catch block 75 e.printStackTrace(); 76 } 77 } 78} 79 80 81 82package com.lanqiao.demo2; 83 84import java.awt.Container; 85import java.awt.FlowLayout; 86import java.awt.GridLayout; 87import java.awt.event.ActionEvent; 88import java.awt.event.ActionListener; 89import java.io.IOException; 90import java.net.Socket; 91import java.net.UnknownHostException; 92 93import javax.swing.ButtonGroup; 94import javax.swing.JButton; 95import javax.swing.JFrame; 96import javax.swing.JLabel; 97import javax.swing.JPanel; 98import javax.swing.JRadioButton; 99import javax.swing.JTextField; 100 101/** 102 * 聊天系统的登录界面 103 * @authorqichunlin 104 * 105 */ 106public class LoginFrame extends JFrame implements ActionListener{ 107 //定义组件 108 JLabel userLab,addrLab,portLab;//标签 109 JTextField userText,addrText,portText;//文本框 110 JRadioButton radioMan,radioWoman,radioser;//单选按钮 111 ButtonGroup group ;//组 112 JButton connectBut,closeBut;//按钮 113 //容器 114 JPanel p1,p2,p3; 115 116 public LoginFrame(){ 117 //实例化组件 118 p1 = new JPanel(); 119 p1.setLayout(new FlowLayout(FlowLayout.LEFT)); 120 121 userLab = new JLabel("姓名:"); 122 userText = new JTextField(10); 123 radioMan = new JRadioButton("男"); 124 radioMan.setSelected(true); 125 radioWoman = new JRadioButton("女"); 126 radioser = new JRadioButton("保密"); 127 //把单选按钮,添加到组中 128 group = new ButtonGroup(); 129 group.add(radioMan); 130 group.add(radioWoman); 131 group.add(radioser); 132 133 //往p1中,添加组件了(注意:组不需要添加到容器中) 134 p1.add(userLab); 135 p1.add(userText); 136 p1.add(radioMan); 137 p1.add(radioWoman); 138 p1.add(radioser); 139 140 141 p2 = new JPanel(); 142 p2.setLayout(new FlowLayout(FlowLayout.LEFT)); 143 addrLab = new JLabel("地址:"); 144 145 addrText = new JTextField(10); 146 addrText.setText(ChatUtil.ADDRESS); 147 148 portLab = new JLabel("端口:"); 149 portText = new JTextField(10); 150 portText.setText(ChatUtil.PORT+""); 151 152 //把组件添加到p2中 153 p2.add(addrLab); 154 p2.add(addrText); 155 p2.add(portLab); 156 p2.add(portText); 157 158 159 p3 = new JPanel(); 160 p3.setLayout(new FlowLayout(FlowLayout.CENTER)); 161 162 connectBut =new JButton("连接"); 163 //绑定事件【点击事件】 164 connectBut.addActionListener(this); 165 166 closeBut = new JButton("断开"); 167 //把组件添加到p3中 168 p3.add(connectBut); 169 p3.add(closeBut); 170 171 172 173 174 175 //设置面板的布局模式(流式布局) 176 this.getContentPane().setLayout(new GridLayout(3,1));//网格布局 177 178 //把组件添加到面板了 179 //1、获取面板 180 Container c = this.getContentPane(); 181 //把p1容器添加到面板 182 c.add(p1); 183 //把p2容器添加到面板 184 c.add(p2); 185 //把p3容器添加到面板 186 c.add(p3); 187 188 init(); 189 } 190 /** 191 * 初始化窗体的基本信息 192 */ 193 public void init(){ 194 //1、标题 195 this.setTitle("登录界面"); 196 //2、大小 197 this.setSize(350,200); 198 //3、关闭放大功能 199 this.setResizable(false); 200 //4、位置 201 this.setLocationRelativeTo(null); 202 //5、是否显示 203 this.setVisible(true); 204 //6、关闭 205 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 206 } 207 208 public static void main(String[] args) { 209 new LoginFrame(); 210 } 211 //点击事件的处理过程 212 @Override 213 public void actionPerformed(ActionEvent e) { 214 //处理选择中的性别 215 String sex = ""; 216 if(radioWoman.isSelected()) { 217 sex = "女"; 218 }else if(radioMan.isSelected()) { 219 sex = "男"; 220 }else { 221 sex = "保密"; 222 } 223 System.out.println("============"); 224 //1、隐藏当前的界面【登录界面】 225 this.setVisible(false); 226 //2、显示聊天的界面 227 ChatFrame c = new ChatFrame(userText.getText(),sex);//登录名传递过来 228 c.getSocket(); 229 } 230} 231 232 233 234 235 236package com.lanqiao.demo2; 237 238import java.io.IOException; 239import java.net.ServerSocket; 240import java.net.Socket; 241 242/** 243 * 服务端类 244 * @author qichunlin 245 */ 246public class Server { 247 public static void main(String[] args) { 248 try { 249 ServerSocket ss = new ServerSocket(ChatUtil.PORT); 250 int count = 0; 251 while (true) { 252 System.out.println("等待客户端连接......."); 253 Socket socket = ss.accept(); 254 count++; 255 System.out.println("目前有"+count+"个客户端进入了聊天室"); 256 } 257 258 } catch (IOException e) { 259 // TODO Auto-generated catch block 260 e.printStackTrace(); 261 } 262 } 263}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一文带你了解Python Socket 编程

大家好,我是皮皮。前言Socket又称为套接字,它是所有网络通信的基础。网络通信其实就是进程间的通信,Socket主要是使用IP地址,协议,端口号来标识一个进程。端口号的范围为065535(用户端口号一般大于1024),协议有很多种,一般我们经常用到的就是TCP,IP,UDP。下面我们来详细了解下Socket吧。一、导入Socket模块因为要操作套接字,

tcp,udp协议的基础聊天,基于udp协议的时间同步机制,在pycharm上实现着色

今日内容:  1,tcp协议的聊天    服务器(server):服务器:<brimportsocketsksocket.socket()创建套接字sk.bind(('127.0.0.1',9090))绑定端口sk.listen()监听f