教育路上

全国站>Java>Java基础>java在线聊天室群发和私聊的案例客户端Socket
发布需求
填写需求信息关闭
我们会根据您的需求匹配

姓      名:

意向城市:

留      言:

手机号码:

验  证  码:  换一张

确认提交

java在线聊天室群发和私聊的案例客户端Socket

摘要:java在线聊天室群发和私聊的案例。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。

2021-06-29 09:11网络推荐

发布时间:
2021-06-29 09:11
信息来源:
网络推荐
浏览次数:
1535
java在线聊天室群发和私聊的案例客户端Socket

package chartall;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

import java.sql.*;
import java.util.*;

public class cilent  extends JFrame {
	private JButton send;
	private JPanel up;
	private JPanel center;
	private JPanel down;
	private JTextArea inputTextArea;
	private JTextArea showTextArea;
	private JComboBox userlist;
	private JButton serverjoin;
	private JButton serverjoinsubmit=new JButton("确定");;
	private JLabel serverjoinlable;
	private JTextField username;
	private JTextField ip;
	private JTextField port;
	private Socket client;
	private String userid;
	private InetAddress ips;
	private int ports;
	
	
	cilent(){
		gui();//图形界面
		listion();//按钮监听
		flashuser();//刷新在线人数		
	}
	
	void gui(){
		//窗体
		JFrame f=new JFrame("客户端");
		f.setSize(800,500);
		f.setLocation(300,100);	
		up=new JPanel() ;
		center=new JPanel() ;
		down=new JPanel() ;
		inputTextArea=new JTextArea(5,50);
		showTextArea=new JTextArea(20,50);
		showTextArea.setEditable(false);
		serverjoinlable=new JLabel("未连接服务器") ;
		down.add(serverjoinlable);
		send=new JButton("发送") ;
		serverjoin=new JButton("连接服务器") ;
		down.add(serverjoin);
		userlist=new JComboBox();
		userlist.addItem("所有人");
		up.add(new JScrollPane(showTextArea));
		center.add(inputTextArea);
		down.add(userlist);
		down.add(send);
		f.setLayout(new BorderLayout());
		f.add(up,BorderLayout.NORTH);
		f.add(center,BorderLayout.CENTER);
		f.add(down,BorderLayout.SOUTH);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);				
		f.setVisible(true);
	}
	
	void listion() {
		//连接服务器监听
		serverjoin.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
				JDialog dialog = new JDialog(cilent.this, true );
				dialog.setLayout(new GridLayout(4,2));
				dialog.setTitle("服务器连接");
				dialog.setSize(300, 200);
				dialog.setLocation(50, 50);
				JLabel usernametext=new JLabel("用户名");
				JLabel iptext=new JLabel("IP");
				JLabel porttext=new JLabel("端口");
				username=new JTextField();
				ip=new JTextField();
				port=new JTextField();
				dialog.add(usernametext);
				dialog.add(username);
				dialog.add(iptext);
				dialog.add(ip);
				dialog.add(porttext);
				dialog.add(port);
				dialog.add(serverjoinsubmit);
				dialog.setVisible(true);
			}
		});
		//服务器连接输入ip端口并提交
		serverjoinsubmit.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
				serverjoinlable.setText("已连接服务器");
				try {
				ips = InetAddress.getByName(ip.getText());
				ports=Integer.parseInt(port.getText());
				client=new Socket(ips,ports);
				PrintWriter writer = new PrintWriter(client.getOutputStream());
				userid=username.getText();
				String a=userid+"@@"+"reg";
				writer.println(a);  
		        writer.flush(); 
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		
		//发送聊天内容
		send.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
				try {
				//client=new Socket(ips,ports);
				
				PrintWriter writer = new PrintWriter(client.getOutputStream());
				String a=userid+"@"+inputTextArea.getText()+"@"+userlist.getSelectedItem();
				writer.println(a);  
		        writer.flush(); 
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
				
	}
	

	
	void flashuser(){	
		new Thread() {	   
			public void run() {
				while(true) {	
					System.out.println(client);
					if(client!=null) {
					    try {
							BufferedReader r = new BufferedReader(new InputStreamReader(client.getInputStream())); 
							String line=r.readLine();
							String[] arr=line.split("@");
							//null@aaaa@userlist
							if(arr[arr.length-1].equals("userlist")) {
								for(String user:arr) {
									if(!user.equals("null")&&!user.equals("userlist")) {
									userlist.addItem(user);	
								    }
								}
							}else if(arr[arr.length-1].equals("群发聊天")) {
									showTextArea.append(arr[0]+arr[1]+"\n");
							}else {
								    showTextArea.append(arr[0]+arr[1]+arr[2]+"\n");
							}
					   }catch(Exception e) {
							e.printStackTrace();
					   }
	               }
	           }
			}
	   }.start();
	}
	

	public static void main(String[] args) {
		new cilent();
	}
}

上一篇:
java调用不同方法两个整数相加
下一篇:
java多线程循环等待服务器向客户端发送数据流网络编程
标签:
网友评论
发布评论

访客的评论 2023/02/02 18:30

文中描述的是准确的吗,如何报名!

相关推荐
我也来发表评价关闭
我对该内容的评价:
0
评价500
验证码: 看不清 换一张
提交 (匿名发布,无须担心别人知道您的身份)
纠错补充本文信息关闭
非常感谢您帮助纠错补充本文信息


 换一张

确认提交