JAVA 聊天室服务器端和客户端完整代码
根据JAVA的网络相关知识和多线程的知识,要简单的能够实现一对一通讯务必能运行!加注释。hey_tear@yeah.net这是邮箱地址谢谢。请附上客户端和服务器的运行效果...
根据JAVA的网络相关知识和多线程的知识,要简单的 能够实现一对一通讯 务必能运行!加注释。hey_tear@yeah.net 这是邮箱地址 谢谢。 请附上客户端和服务器的运行效果图!
展开
1个回答
2013-11-25
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
import java.io.*;
public class Server extends JFrame
{
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("文件");
JMenu m2 = new JMenu("帮助");
JMenuItem mi1 = new JMenuItem("连接");
JMenuItem mi2 = new JMenuItem("退出");
JLabel lab1 = new JLabel("用户在线列表");
JLabel lab2 = new JLabel("消息:");
JTextArea ta = new JTextArea(9,30);
java.awt.List list = new java.awt.List(9);
JTextField tf = new JTextField(24);
JButton b = new JButton("发送");
JScrollPane sp = new JScrollPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
ServerSocket serverSocket;
Socket socket;
ArrayList array = new ArrayList();
int connect;
public Server()
{
super("聊天服务器");
setJMenuBar(mb);
mb.add(m1);
mb.add(m2);
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
sp.getViewport().add(ta);
p1.setLayout(new BorderLayout());
p3.add(lab1);
p1.add(p3,BorderLayout.NORTH);
p1.add(list,BorderLayout.SOUTH);
p2.add(lab2);
p2.add(tf);
p2.add(b);
getContentPane().add(sp,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
pack();
//服务器端界面居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
setSize(400,430);
setVisible(true);
setResizable(false);
mi1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(serverSocket == null)
{
try
{
serverSocket = new ServerSocket(7000);
ta.append("系统提示:"+" 聊天服务器"
+" 正在启动......\n");
}
catch(IOException ex)
{
ta.append("系统提示:"+" 聊天服务器"
+" 启动失败!\n");
}
}
else
{
ta.append("系统消息:"+" 聊天服务器"
+" 已经启动......\n");
}
//启动监听客户端连接线程
Thread t = new Watch();
t.start();
}
});
mi2.addActionListener(new ServerListener());
b.addActionListener(new ServerListener());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
closeAll();
System.exit(0);
}
});
}
class Watch extends Thread
{
public void run()
{
while(true)
{
if(array.size() <= 10)
{
try
{
socket = serverSocket.accept();
}
catch(IOException ex)
{
ta.append("系统消息: "+ex+"\n");
}
//创建客户端线程,一个线程对应一个客户端
Client c = new Client(socket);
array.add(c);
c.start();
}
else
{
ta.append("系统消息:"+" 聊天服务器"
+" 已经超过最大连接数,请待会连接!\n");
}
}
}
}
class Client extends Thread
{
String name;
String ip;
String ipAddress;
Socket socket;
BufferedReader br;
PrintStream ps;
public Client(Socket s)
{
try
{
socket = s;
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
}
catch (IOException ex)
{
ta.append("系统消息: "+ex+"\n");
}
}
public void run()
{
String info;
try
{
while(true)
{
info = br.readLine();
if(info == null)
break;
StringTokenizer st = new StringTokenizer(info, ":");
String keyword = st.nextToken();
//处理客户端传来的信息
if (keyword.equals("CONNECT"))
{
name = st.nextToken();
ip = st.nextToken();
ipAddress = ip.substring(ip.indexOf("/")+1);
ta.append("系统消息: " + name + "/"
+ ipAddress +" 已经连接\n");
list.add(name + " " + ip
+" 已经登录");
notifyChatRoom();
}
else if (keyword.equals("MSG"))
{
String broadcast = st.nextToken();
StringBuffer message = new StringBuffer("MSG:" +broadcast);
message.append(st.nextToken("\0"));
if (broadcast.equals("BROAD"))
{
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
import java.io.*;
public class Server extends JFrame
{
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("文件");
JMenu m2 = new JMenu("帮助");
JMenuItem mi1 = new JMenuItem("连接");
JMenuItem mi2 = new JMenuItem("退出");
JLabel lab1 = new JLabel("用户在线列表");
JLabel lab2 = new JLabel("消息:");
JTextArea ta = new JTextArea(9,30);
java.awt.List list = new java.awt.List(9);
JTextField tf = new JTextField(24);
JButton b = new JButton("发送");
JScrollPane sp = new JScrollPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
ServerSocket serverSocket;
Socket socket;
ArrayList array = new ArrayList();
int connect;
public Server()
{
super("聊天服务器");
setJMenuBar(mb);
mb.add(m1);
mb.add(m2);
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
sp.getViewport().add(ta);
p1.setLayout(new BorderLayout());
p3.add(lab1);
p1.add(p3,BorderLayout.NORTH);
p1.add(list,BorderLayout.SOUTH);
p2.add(lab2);
p2.add(tf);
p2.add(b);
getContentPane().add(sp,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
pack();
//服务器端界面居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
setSize(400,430);
setVisible(true);
setResizable(false);
mi1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(serverSocket == null)
{
try
{
serverSocket = new ServerSocket(7000);
ta.append("系统提示:"+" 聊天服务器"
+" 正在启动......\n");
}
catch(IOException ex)
{
ta.append("系统提示:"+" 聊天服务器"
+" 启动失败!\n");
}
}
else
{
ta.append("系统消息:"+" 聊天服务器"
+" 已经启动......\n");
}
//启动监听客户端连接线程
Thread t = new Watch();
t.start();
}
});
mi2.addActionListener(new ServerListener());
b.addActionListener(new ServerListener());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
closeAll();
System.exit(0);
}
});
}
class Watch extends Thread
{
public void run()
{
while(true)
{
if(array.size() <= 10)
{
try
{
socket = serverSocket.accept();
}
catch(IOException ex)
{
ta.append("系统消息: "+ex+"\n");
}
//创建客户端线程,一个线程对应一个客户端
Client c = new Client(socket);
array.add(c);
c.start();
}
else
{
ta.append("系统消息:"+" 聊天服务器"
+" 已经超过最大连接数,请待会连接!\n");
}
}
}
}
class Client extends Thread
{
String name;
String ip;
String ipAddress;
Socket socket;
BufferedReader br;
PrintStream ps;
public Client(Socket s)
{
try
{
socket = s;
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
}
catch (IOException ex)
{
ta.append("系统消息: "+ex+"\n");
}
}
public void run()
{
String info;
try
{
while(true)
{
info = br.readLine();
if(info == null)
break;
StringTokenizer st = new StringTokenizer(info, ":");
String keyword = st.nextToken();
//处理客户端传来的信息
if (keyword.equals("CONNECT"))
{
name = st.nextToken();
ip = st.nextToken();
ipAddress = ip.substring(ip.indexOf("/")+1);
ta.append("系统消息: " + name + "/"
+ ipAddress +" 已经连接\n");
list.add(name + " " + ip
+" 已经登录");
notifyChatRoom();
}
else if (keyword.equals("MSG"))
{
String broadcast = st.nextToken();
StringBuffer message = new StringBuffer("MSG:" +broadcast);
message.append(st.nextToken("\0"));
if (broadcast.equals("BROAD"))
{
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
合力亿捷
2023-07-25 广告
2023-07-25 广告
要快速搭建在线客服系统,可以考虑以下步骤:1. 确定需求:首先需要确定自己的需求和目标,例如需要什么类型的客服、需要支持多少用户、需要什么样的沟通工具等等。2. 选择合适的平台:选择一个合适的在线客服系统平台,可以选择公有云或私有云部署方式...
点击进入详情页
本回答由合力亿捷提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询