JAVA局域网聊天系统源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

这是我自己做的简单聊天系统客户端
package LiaoTianSys;
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import .Socket;
import .UnknownHostException;
public class ConversationFrame extends JFrame {
JScrollPane jsp;
JTextField jtf;
static JTextArea jta;
//JTextArea jat1,jta2;
JButton enter=new JButton("发送");
JButton jb=new JButton("聊天室好友");
JButton jb2=new JButton("进入聊天室");
JButton jb3=new JButton("刷新在线人员列表");
JPanel jp,jp1,jp3,jp4;
DefaultListModel listmodel = new DefaultListModel();
//static String[] NAME=new String[10];
String n[]={"f"};
JList list=new JList(listmodel);
JLabel time=new JLabel("当前系统时间:");
JLabel showtime=new JLabel("显示时间");
JLabel jl=new JLabel("输聊天信息");
JLabel nicheng=new JLabel("昵称");
JTextField NCshuru=new JTextField(10);
static DataOutputStream dos;
static DataInputStream dis;
//final LoginFrame lf;
Socket socket;
public ConversationFrame()
{
Container con=getContentPane();
con.setLayout(new BorderLayout());
jp=new JPanel();
setSize(700,600);
setLocation(100,100);
jta=new JTextArea();
jta.setEditable(false);
jsp=new JScrollPane(jta);
con.add(jsp,BorderLayout.CENTER);
jtf=new JTextField(20);
jp.add(jl);
jp.add(jtf);
jp.add(enter);
con.add(jp,BorderLayout.SOUTH);
jp1=new JPanel(new BorderLayout());
JScrollPane jsp1=new JScrollPane(list);
jp1.add(jsp1,BorderLayout.CENTER);
jp1.add(jb,BorderLayout.NORTH);
con.add(jp1,BorderLayout.EAST);
//pack();
jp3=new JPanel();
jp3.add(nicheng);
jp3.add(NCshuru);
jp3.add(jb2);
con.add(jp3,BorderLayout.NORTH);
jp4=new JPanel(new GridLayout(10,1));
jp4.add(jb3);
jp4.add(time);
jp4.add(showtime);
new getTime(this).start();
con.add(jp4,BorderLayout.WEST);
setVisible(true);
// 发送信息给所有人
enter.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String info=jtf.getText();
try {
dos.writeUTF(NCshuru.getText()+" 对所有人说:"+info);
dos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//进入聊天室时将自己的昵称发给服务器,首先去验证是否会与已有的人同名,本聊天室是不支持同昵称聊天
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String msg=NCshuru.getText().toString();//得到昵称
if(msg.length()==0)
{
JOptionPane.showMessageDialog(null, "昵称不应该为空,", "温馨提示", RMA TION_MESSAGE);
}
{
try{
dos.writeUTF("name"+msg);
dos.flush();
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
});
//向服务器请求更新在线人员列表
jb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
listmodel.removeAllElements();//请求之前删除JList对象中的所有内容
dos.writeUTF("请求更新在线人员列表");
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
System.out.println(e1.getMessage());
}
}
});
//当有在线人员时,双击JList列表某项弹出私聊对话框事件,匿名内部类实现的
list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)
{
int index=list.locationToIndex(e.getPoint());// 获得list表中的索引值
String recevier=(String)listmodel.getElementAt(index);//得到索引对应的值
String sender=NCshuru.getText();
new Danliao(sender,recevier,socket);
}
}
});
}
public static void main(String[] args) {
ConversationFrame Con=new ConversationFrame();
Con.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Con.kaishi();
}
public void kaishi()
{
try {
socket=new Socket("172.16.14.60",8888);
dos=new DataOutputStream(socket.getOutputStream());
dis=new DataInputStream(socket.getInputStream());
pc pc1=new pc(socket,this);//传送套接字,本类对象给pc线程
pc1.start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// pc线程类,负责接收服务器发送来的信息
class pc extends Thread
{ ConversationFrame conver;
Socket socket;
public pc(Socket socket,ConversationFrame conver)
{
this.conver=conver;
this.socket=socket;
}
public void run()
{
try {
int i=0;
while(true)
{
String line;
//从线路读取信息
line=ConversationFrame.dis.readUTF();
// 将所有的在线人员昵称发送给JList,并添加到在线人列表中
if(line.startsWith("N"))// 服务器发送过来的信息的格式是N+
{
String na=line.substring(1);
try{
conver.listmodel.addElement(na);
System.out.println(na);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
//接受服务器发来的广播聊天信息
else
{
ConversationFrame.jta.append(line+"\n");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
服务器端
package LiaoTianSys;
import java.io.*;
import .*;
import java.util.*;
public class Server implements Runnable{
public static final int port=8888;
protected ServerSocket ss;
static Vector connections;
Thread connect;
public Server()
{
try {
ss=new ServerSocket(port);
connections=new Vector(1000);
connect=new Thread(this);
connect.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
public void run()
{
try{
while(true)
{
Socket client=ss.accept();
System.out.println("客户端连接上");
firstthread f=new firstthread(this,client);
f.setPriority(Thread.MIN_PRIORITY);
f.start();
connections.addElement(f);
}
}catch(IOException e)
{
System.out.println(e.getMessage());
}
}
//向所有人发送聊天信息
public void SenAll(String msg)
{
int i;
firstthread ft;
for(i=0;i<connections.size();i++)
{
ft=(firstthread)connections.elementAt(i);
try{
ft.out.writeUTF(msg);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
//发送单聊信息的方法
public void SenOne(String msg)
{
int i;
firstthread ft;
/*String s1,s2,s3;
s1=new String("people");
s2=new String(msg.substring(2));//私信为两个字
s3=s1.conString msg="我悄悄的对小花说你好啊";
cat(s2);*/
int begin=msg.indexOf("对");
int end=msg.indexOf("说");
begin=begin+1;
String strNew=msg.substring(begin,end);
for(i=0;i<connections.size();i++)//遍历线程,找到要发送的对象{
ft=(firstthread)connections.elementAt(i);//遍历线程
if(strNew.startsWith())
{
System.out.println();
try{
String MSG=msg.substring(7);
ft.out.writeUTF(MSG);
}catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
}
class firstthread extends Thread
{
protected Socket client;
String line,name=null;
DataOutputStream firstout,out;
DataInputStream in;
Server server;
public firstthread(Server server,Socket socket)
{
this.server=server;
this.client=socket;
try
{
in=new DataInputStream(client.getInputStream());
out=new DataOutputStream(client.getOutputStream());
firstout=new DataOutputStream(client.getOutputStream());
}catch(IOException e)
{
server.connections.removeElement(this);
}
}
//客户线程运行的方法,不断监听客户线路发送的来的信息,然后决定转发方式public void run()
{
try
{
while(true)
{
line=in.readUTF();
if(line.startsWith("name"))
{
//获取当前线程
firstthread
d=(firstthread)(server.connections.elementAt(server.connections.indexOf(this)));
line=line.substring(4);
if(==null)
{
=line;
}
}
//将服务器的所有在线人员的昵称发送给所有的客户端
else if(line.startsWith("请求更新在线人员列表"))
{
try
{
for(int i=0;i<server.connections.size();i++)
{
firstthread c=(firstthread)(server.connections.elementAt(i));
if(!=null)
{
firstout.writeUTF("N"+);//发送昵称
}
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
else if(line.startsWith("private"))
{
server.SenOne(line);
}
//发送聊天信息给所有的pc客户端
else
{
server.SenAll(line);
}
}
}catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
显示时间
package LiaoTianSys;
import java.text.SimpleDateFormat;
import java.util.Date;
public class getTime extends Thread{
String time;
ConversationFrame conver;
public getTime(ConversationFrame conver)
{
this.conver=conver;
System.out.println("获得系统时间");
}
public void run()
{
while(true)
{
SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss");
time= df.format(new Date());
//System.out.println(time);
conver.showtime.setText(time);
try{
this.sleep(1000);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
public static void main(String []args)
{
}
}
单聊
package LiaoTianSys;
import java.io.DataOutputStream;
import java.io.IOException;
import .*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Danliao extends JFrame implements ActionListener{ JButton jb=new JButton("发送");
JPanel jp1=new JPanel();
JLabel jl=new JLabel("输入聊天信息");
JTextField jtf=new JTextField(20);
JTextArea jta=new JTextArea();
JScrollPane jsp=new JScrollPane(jta);
String recevier;
String sender;
Socket socket;
public Danliao(String sender,String recevier,Socket socket) { this.recevier=recevier;
this.sender=sender;
this.socket=socket;
setBackground(Color.red);
setTitle("与"+recevier+"单聊");
setSize(400,400);
setLocation(200,200);
Container con=getContentPane();
con.setLayout(new BorderLayout());
con.add(jsp,BorderLayout.CENTER);
jp1.add(jl);
jp1.add(jtf);
jp1.add(jb);
/*addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
//System.exit(0);
//this.dispose();
//this.hide();
this.setVisible(false);
}
});*/
addWindowListener(new Close(this));
con.add(jp1,BorderLayout.SOUTH);
jb.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ String msg=jtf.getText();
jta.append(msg+"\n"+"\n");
jtf.setText(null);
try{
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.writeUTF("private"+sender+"悄悄的对"+recevier+"说: "+msg);
jtf.setText(null);
}catch(Exception e1)
{
e1.printStackTrace();
}
}
public static void main(String[] args)
{
}
}
//单击关闭窗口事件,隐藏单聊窗口
class Close extends WindowAdapter
{
Danliao danliao;
public Close(Danliao danliao)
{
this.danliao=danliao;
}
public void windowClosing(WindowEvent e) {
danliao.setVisible(false);
}
}。

相关文档
最新文档