当前位置: 首页 > news >正文

大连网站制作中企动力珍岛网站建设

大连网站制作中企动力,珍岛网站建设,苏中建设集团官方网站,做网站公司cnfg网络聊天室 服务器: 1.启动服务器,在服务器端循环监听客户端的连接 try {ServerSocket serverSocketnew ServerSocket(6622);System.out.println("服务器已启动");while(true){//把客户端实例添加到sockets里Socket socketserverSocket.acc…

网络聊天室

服务器:

1.启动服务器,在服务器端循环监听客户端的连接

try {ServerSocket serverSocket=new ServerSocket(6622);System.out.println("服务器已启动");while(true){//把客户端实例添加到sockets里Socket socket=serverSocket.accept();sockets.add(socket);System.out.println("当前连接到客户端数量:"+sockets.size());//为每一个链接过来的客户端开一个线程new SocketThread(socket).start();}} catch (IOException e) {e.printStackTrace();System.out.println("服务器创建失败");}

2.把循环接收到的多个客户端Socket对象存储起来(集合)

ArrayList<Socket> sockets=new ArrayList<>();

3.在服务器端每个Socket都要监听各自客户端发来的消息

while(true){try {String msg=dataInputStream.readUTF();jTextAreamsg.append(msg+"\n");//在服务器端显示msg}} catch (IOException e) {e.printStackTrace();System.out.println("客户端下线了");}}

4.一旦某个客户端发送了消息,那么在服务器端,就将此消息转发给其他的客户

//向不同客户端转发消息//遍历socketsfor (Socket socket:sockets){//客户端1 客户端2 客户端3DataOutputStream dataOutputStream=new DataOutputStream(socket.getOutputStream());dataOutputStream.writeUTF(msg);}
客户端:

1.用户登录,创建Socket

 Socket socket = new Socket("xxxxxxxxx", 6622);

2.如果Socket创建成功,打开聊天窗口

new ChatWindow(jTextaccount.getText(), socket);//打开聊天窗口

3.输入内容,点击发送按钮发送消息

4.在客户端监听服务器发送回来的消息,并把消息显示出来

class ClientThread extends Thread{DataInputStream dataInputStream;public ClientThread(Socket socket) throws IOException {dataInputStream=new DataInputStream(socket.getInputStream());}@Overridepublic void run() {while(true){try {String msg=dataInputStream.readUTF();jTextArea.append(msg+"\n");//可以显示聊天内容并且保持原先内容} catch (IOException e) {e.printStackTrace();break;}}}}

完整代码:

ChatWindow:

public class ChatWindow extends JFrame {JTextArea jTextArea;public ChatWindow(String name, Socket socket) throws IOException {DataOutputStream dos=new DataOutputStream(socket.getOutputStream());this.setTitle("欢迎"+name+"登录");this.setSize(500,500);this.setLocationRelativeTo(null);//居中this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//关闭窗口时退出程序this.setResizable(false);//禁止窗口拖拽//创建面板JPanel jPanel=new JPanel();jTextArea=new JTextArea(22,43);//显示文本域jTextArea.setEditable(false);//不可修改的JTextArea jTextArea2=new JTextArea(5,33);//发送文本域jTextArea2.setLineWrap(true);//强制换行JScrollPane j=new JScrollPane(jTextArea2);//滚动条JButton jButtonsend=new JButton("发送");jPanel.add(jTextArea);jPanel.add(j);jPanel.add(jButtonsend);this.add(jPanel);this.setVisible(true);//来到聊天窗口jButtonsend.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String message=jTextArea2.getText();if(message.length()==0){//输入为空JOptionPane.showMessageDialog(null,"发送内容不能为空");return;}else{//不为空,向服务器发送消息String msg=name+"  "+new SimpleDateFormat("HH:mm:ss").format(new Date());msg=msg+"\n"+message;try {dos.writeUTF(msg);//发送成功清空发送内容jTextArea2.setText("");} catch (IOException ioException) {ioException.printStackTrace();JOptionPane.showMessageDialog(null,"内容发送失败,请检查网络连接");}}}});new ClientThread(socket).start();this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {int res=JOptionPane.showConfirmDialog(null,"你确定要退出聊天吗?","警告",JOptionPane.OK_CANCEL_OPTION);if(res==0){//点击确定new LoginWindow();dispose();}}});}//监听服务器发的消息(即客户端123的消息)class ClientThread extends Thread{DataInputStream dataInputStream;public ClientThread(Socket socket) throws IOException {dataInputStream=new DataInputStream(socket.getInputStream());}@Overridepublic void run() {while(true){try {String msg=dataInputStream.readUTF();jTextArea.append(msg+"\n");//可以显示聊天内容并且保持原先内容} catch (IOException e) {e.printStackTrace();break;}}}}}

LoginWindow:

public class LoginWindow extends JFrame {public LoginWindow(){this.setTitle("欢迎登录");this.setSize(500,350);this.setLocationRelativeTo(null);//居中this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序this.setResizable(false);//禁止窗口拖拽JPanel jPanel=new JPanel();JLabel jLabel=new JLabel("欢迎登录");jLabel.setFont(new Font("横体", Font.BOLD,30));jPanel.add(jLabel);//欢迎登录//中间第一个空标签JLabel jLabel1=new JLabel();//欢迎登录下面空白部分jLabel1.setPreferredSize(new Dimension(500,50));//账号JLabel jLabelAccount=new JLabel("账号");//账号标签jLabelAccount.setPreferredSize(new Dimension(30,30));JTextField jTextaccount=new JTextField(15);//账号文本域//中间第二个空标签JLabel jLabel2=new JLabel();//欢迎登录下面空白部分jLabel2.setPreferredSize(new Dimension(500,20));//密码JLabel jLabelPassword=new JLabel("密码 ");//账号标签jLabelPassword.setPreferredSize(new Dimension(30,30));JPasswordField jPasswordField=new JPasswordField(15);//中间第二个空标签JLabel jLabel3=new JLabel();//欢迎登录下面空白部分jLabel3.setPreferredSize(new Dimension(500,20));//按钮JButton ButtonLogin=new JButton("登录");JButton ButtonSign=new JButton("注册");jPanel.add(jLabel1);jPanel.add(jLabelAccount);jPanel.add(jTextaccount);jPanel.add(jLabel2);jPanel.add(jLabelPassword);jPanel.add(jPasswordField);jPanel.add(jLabel3);jPanel.add(ButtonLogin);jPanel.add(ButtonSign);this.add(jPanel);this.setVisible(true);//按钮事件ButtonLogin.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(jTextaccount.getText().equals("")||jPasswordField.getText().equals("")){//当账号或密码为空时,弹出警告JOptionPane.showMessageDialog(null,"账号和密码不能为空","警告",JOptionPane.ERROR_MESSAGE);return;}else if(!((jTextaccount.getText().matches("\\w{1,100}"))&&(jPasswordField.getText().matches("\\w{1,100}")))){//当1-100范围内不是字母和数字时弹出警告JOptionPane.showMessageDialog(null,"账号密码只能由数字,字母组成","警告",JOptionPane.ERROR_MESSAGE);return;}//创建Socketelse {try {Socket socket = new Socket("10.13.0.67", 6622);new ChatWindow(jTextaccount.getText(), socket);//打开聊天窗口dispose();//释放窗口} catch (IOException ioException) {ioException.printStackTrace();JOptionPane.showMessageDialog(null, "服务器连接失败,请稍后再试");}}}});}
}

Run:

public class Run {public static void main(String[] args) {new LoginWindow();}
}

Server:

public class Server extends JFrame {JTextArea jTextAreamsg;//放到成员变量便于在内部类中也可以使用//客户端连接的数量ArrayList<Socket> sockets=new ArrayList<>();public void ServerStart(){this.setTitle("我是服务器");this.setSize(500,500);this.setLocationRelativeTo(null);//居中this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//关闭窗口时退出程序this.setResizable(false);//禁止窗口拖拽JPanel jPanel=new JPanel();jTextAreamsg=new JTextArea(28,43);jTextAreamsg.setLineWrap(true);//强制换行JScrollPane jScrollPane=new JScrollPane();jTextAreamsg.setEditable(false);//不可修改的jPanel.add(jTextAreamsg);jPanel.add(jScrollPane);this.add(jPanel);this.setVisible(true);//关闭窗口this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {int res=JOptionPane.showConfirmDialog(null,"你确定要关闭服务器吗?","警告",JOptionPane.OK_CANCEL_OPTION);if(res==0){//点击确定dispose();}}});try {ServerSocket serverSocket=new ServerSocket(6622);System.out.println("服务器已启动");while(true){//把客户端实例添加到sockets里Socket socket=serverSocket.accept();sockets.add(socket);System.out.println("当前连接到客户端数量:"+sockets.size());//为每一个链接过来的客户端开一个线程new SocketThread(socket).start();}} catch (IOException e) {e.printStackTrace();System.out.println("服务器创建失败");}}//内部类,用来监听自己客户端有没有发消息class SocketThread extends Thread{Socket socket;//用来引入到run方法DataInputStream dataInputStream;public SocketThread(Socket socket) throws IOException {this.socket=socket;//把成员变量socket赋值为传入的socket参数,以便于客户端下线后可以从sockets里移除客户端dataInputStream=new DataInputStream(socket.getInputStream());}@Overridepublic void run() {//循环监听客户端自己发送消息while(true){try {String msg=dataInputStream.readUTF();jTextAreamsg.append(msg+"\n");//在服务器端显示msg//向不同客户端转发消息//遍历socketsfor (Socket socket:sockets){//客户端1 客户端2 客户端3DataOutputStream dataOutputStream=new DataOutputStream(socket.getOutputStream());dataOutputStream.writeUTF(msg);}} catch (IOException e) {e.printStackTrace();System.out.println("客户端下线了");sockets.remove(socket);break;}}}}
}

ServerRun:

public class ServerRun {public static void main(String[] args) {new Server().ServerStart();}
}

在这个过程中监听服务器消息和监听客户端消息都用到了内部类,这样的好处是可以之间在内部类中使用大类里面的jTextArea和sockets

http://www.yayakq.cn/news/404659/

相关文章:

  • 聊城做网站的公司教程提供提供手机网站建设
  • 定西营销型网站建设兰州网站建设优化制作公司
  • 淄博制作网站的公司怎么制作网页快捷方式
  • 网站建设生意怎么样淘宝网卖家中心入口
  • 闵行网站制作哪里有俄罗斯跨境电商平台ozon
  • 长沙网页制作网站化德网站建设
  • 学网站建设的工资高吗qq业务网站平台
  • 什么网站可以直接做word文档做视频网站服务器怎么选择
  • 网站后台系统功能最好的做法
  • 黄页网站怎么查有哪些网站是做采购招标的
  • 怎样创造自己的网站哪个网站原创文章
  • seo最好的网站源码带后台的html网站源码
  • 搭建网站挣钱图片外链生成工具
  • 中国网站建设公司有哪些方面qq排名优化网站
  • 自己做网站卖什么名字执业医师变更注册网站
  • 建设厅投诉网站杭州网站备案
  • 泉州专业建站品牌微商城手机网站制作公司
  • 怎样建设公司网站小程序网站浏览成交指标
  • 怎么评价网站的好坏可以做c oj的网站
  • 什么学习网站建设有哪些网站上可以做试卷
  • 做网站映射tcp竞价运营是做什么的
  • 贵阳网站建设优化属于c2c的网站是
  • 网上建站赚钱一个网站可以有几个域名
  • 该网站在工信部的icp ip地址柳州市住房建设保障网
  • 宽城网站制作网络货运平台有哪些
  • 深圳外贸网站制作敬请期待的句子
  • 录播教育系统网站建设费用o2o系统网站建设
  • 做一个配送网站淘宝美工与网站开发
  • 如何把自己网站推广出去西安高端网站设计公司
  • 嘉兴网站建设全包网站开发合作协议书