前段时间在做一个聊天软件,功能没有你要求的那么强大,时间比较紧,所以只实现基本的一些功能。
其中服务器端的一些功能还没有实现,客户端的成员列表还没有弄好。仅供参考!
服务器端代码如下:(可运行代码)服务器端可以字定义端口号,可向客户端发送系统消息。
程序代码:
import *;
import *;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Server extends JFrame implements ActionListener
{
Container con;
JLabel label1,label2,label3;
JTextField field1,field2;
static JTextArea area1=new JTextArea(7,30);
JTextArea area2;
JButton button1,button2,button3,button4;
JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7,panel8,panel9,panelq,panelw;
int k,i;
String a,b;
static String g;
static Vector allsockets1;
public static void main(String[] args)
{
new Server();
}
public Server(String area)
{
this.g=area;
area1.append(g+"\n");
}
public Server(Vector v)
{
this.allsockets1=v;
}
public Server()
{
super("服务器端");
label1=new JLabel("服务器IP:",JLabel.CENTER);
label2=new JLabel("端口",JLabel.CENTER);
label3=new JLabel(new ImageIcon("Love Message.jpg"));
field1=new JTextField(13);
field2=new JTextField("8000",13);
button1=new JButton("开启");
button2=new JButton("断开");
button3=new JButton("发送");
button4=new JButton("退出");
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
panel4=new JPanel();
panel5=new JPanel();
panel6=new JPanel();
panel7=new JPanel();
panel8=new JPanel();
panel9=new JPanel();
panelq=new JPanel();
panelw=new JPanel();
//area1=new JTextArea(7,30);
//area1.append(g);
area2=new JTextArea(3,30);
area1.setEditable(false);
area1.setLineWrap(true);
area2.setLineWrap(true);
con=this.getContentPane();
con.setLayout(new BorderLayout());
con.add(panel1,BorderLayout.WEST);//西
con.add(panel2,BorderLayout.CENTER);
con.add(panel3,BorderLayout.EAST);
panel2.setLayout(new BorderLayout());
panel2.add(panel4,BorderLayout.NORTH);//北
panel2.add(panel5,BorderLayout.CENTER);//中
panel2.add(panel6,BorderLayout.EAST);//东
panel2.add(panel7,BorderLayout.SOUTH);//南
//panel5.setLayout(new BorderLayout());
panel8.setLayout(new GridLayout(4,1,0,10));
panel5.add(panel8);
panel5.add(panel9);
panel8.add(label1);
panel8.add(field1);
field1.setEditable(false);
panel8.add(label2);
panel8.add(field2);
panel9.add(button1);
panel9.add(button2);
panel6.add(label3);
panel7.setLayout(new BorderLayout());
panel7.add(panelq,BorderLayout.NORTH);
panel7.add(panelw,BorderLayout.CENTER);
panelq.setLayout(new BorderLayout(0,5));
panelq.add(new JScrollPane(area1),BorderLayout.NORTH);
panelq.add(new JScrollPane(area2),BorderLayout.CENTER);
panelw.add(button3);
panelw.add(button4);
button1.addActionListener(this);
//button2.addActionListener(this);
button3.addActionListener(this);
try{
InetAddress localHost=InetAddress.getLocalHost();
System.out.println (localHost);
String s=localHost.toString();
field1.setText(s);
}catch(Exception s){
s.printStackTrace();
}
//this.setResizable(false);
this.setLocation(200,160);
this.setSize(365,500);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
a=this.field2.getText();
i=Integer.parseInt(a);
b=this.area2.getText();
if(e.getSource()==button1)
{
new Sockets(i).start();
}
if(e.getSource()==button3)
{
//Vector allsockets1;
if(b.equals(""))
{
JOptionPane.showMessageDialog(this,"管理员也不能发送空消息!");
area2.requestFocus(true);
}
else
{
try{
for(int i=0;i<allsockets1.size();i++)
{
Object obj=allsockets1.get(i);//返回向量中指定位置的元素
Socket socket=(Socket)obj;
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
pw.println("管理员:"+b);
}
area1.append("管理员: "+b+"\n");
area2.setText("");
area2.requestFocus(true);
}catch(IOException o)
{
System.out.println (o);
}
}
}
}
}
class Sockets extends Thread
{
int i;
public Sockets(int i)
{
this.i=i;
}
public void run()
{
try{
System.out.println (i);
System.out.println ("我是服务器");
ServerSocket ss=new ServerSocket(i);//建立一个Socket,断口为8000
Vector v=new Vector();
while(true)
{
Socket s=ss.accept();//等待客户端连接(阻塞)
InputStream in=s.getInputStream();//得到输入流
v.add(s);
new ServerReceiveThread(in,v).start();
new Server(v);
}
}catch(IOException i){
i.printStackTrace();
}
}
}
class ServerReceiveThread extends Thread
{
InputStream in;
Vector allsockets;
Vector r=new Vector();
int k;
String h;
public ServerReceiveThread(InputStream in,Vector v)
{
this.in=in;
this.allsockets=v;
}
public void run()
{
BufferedReader br=new BufferedReader(new InputStreamReader(in));
try{
while(true)
{
String str=br.readLine();
System.out.println (str);
if(str.equals(""))
{
k++;
h="游客"+k+"号连接";
new Server(h);
//r.add(h);
}
else
{
h=str;
new Server(h);
//r.add(h1);
}
for(int i=0;i<allsockets.size();i++)
{
Object obj=allsockets.get(i);//返回向量中指定位置的元素
Socket socket=(Socket)obj;
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
pw.println(h);
/*if(str.equals(""))
{
k++;
pw.println("游客"+k+"号连接");
//h="游客"+k+"号连接";
//new Server(h);
//r.add(h);
}
else
{
pw.println(str);
//h1=str;
//new Server(h);
//r.add(h1);
}*/
//r.add(h);
//pw.println(r);
}
System.out.println ("该消息已发送");
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
/*class Shuchu extends Thread
{
OutputStream out;
public Shuchu(OutputStream out)
{
this.out=out;
}
public void run()
{
PrintWriter pw=new PrintWriter(out,true);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
try{
pw.println(br.readLine());
}catch(Exception e){
e.printStackTrace();
}
}
}
}*/
客户端代码如下:(可运行代码)客户端昵称可填可不填,如果在本机上测试IP为127.0.0.1
端口必须和服务器设置的一样。
程序代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import *;
import *;
import java.awt.event.*;
import java.util.Date;
public class Client extends JFrame implements ActionListener
{
JButton button1,button2,button3;
JLabel label1,label2,label3,label4,label5;
JTextArea area1,area2,area3;
JTextField field1,field2,field3;
JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7;
Container con;
String a,b,g,q;
int c;
Socket socket;
public Client(String s)
{
String x=s;
JOptionPane.showMessageDialog(this,x);
}
public Client()
{
super("客户端");
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
panel4=new JPanel();
panel5=new JPanel();
panel6=new JPanel();
panel7=new JPanel();
con=this.getContentPane();
con.setLayout(new BorderLayout());
con.add(panel1,BorderLayout.WEST);
con.add(panel2,BorderLayout.CENTER);
con.add(panel3,BorderLayout.EAST);
panel2.setLayout(new BorderLayout(5,0));
panel2.add(panel4,BorderLayout.NORTH);
panel2.add(panel5,BorderLayout.WEST);
panel2.add(panel6,BorderLayout.CENTER);
panel2.add(panel7,BorderLayout.SOUTH);
panel5.setLayout(new BorderLayout(0,10));
area1=new JTextArea(10,30);
area2=new JTextArea(4,30);
area1.setLineWrap(true);
area2.setLineWrap(true);
panel5.add(new JScrollPane(area1),BorderLayout.CENTER);
area1.setEditable(false);
panel5.add(new JScrollPane(area2),BorderLayout.SOUTH);
button1=new JButton("发送");
button2=new JButton("取消");
button3=new JButton("连接");
label1=new JLabel("成员列表:");
label2=new JLabel("我的昵称:");
label3=new JLabel("IP地址:");
label4=new JLabel("端口:");
label5=new JLabel();
field1=new JTextField(10);
field2=new JTextField(10);
field3=new JTextField(10);
area3=new JTextArea(8,10);
//panel6.setLayout(new GridLayout(9,1,5,5));
panel6.add(label1);
panel6.add(new JScrollPane(area3));
panel6.add(label2);
panel6.add(field1);
panel6.add(label3);
panel6.add(field2);
panel6.add(label4);
panel6.add(field3);
panel6.add(button3);
panel6.add(label5);
panel7.add(button1);
panel7.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
new Shijian(label5).start();
try{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
this.repaint();
}
catch(Exception ext){
ext.printStackTrace();
}
//this.setIconImage(Toolkit.getDefaultToolkit().createImage("1.jpg"));
this.setSize(500,460);
this.setLocation(260,160);
this.setResizable(false);
this.setVisible(true);
field2.requestFocus(true);
}
/*public void join(String s)
{
c=Integer.parseInt(b);
try{
System.out.println ("我是客户端");
//连接断口为8000的服务器,localhost表示本机,可改为服务器的IP地址
Socket sk=new Socket(s,c);
InputStream in=sk.getInputStream();
OutputStream out=sk.getOutputStream();//得到输出流
new Shuru(in).start();
new Shuchu(out).start();
//PrintStream ps=new PrintStream(os,true);//输出字符流
//ps.println("你好服务器");
//InputStream is=sk.getInputStream();//得到输入流
//InputStreamReader isr=new InputStreamReader(is);//输入字节流
//BufferedReader br=new BufferedReader(isr);//输入字符流
//System.out.println ("服务器说:"+br.readLine());//读行
}catch(IOException e){
e.printStackTrace();
}
}*/
public void actionPerformed(ActionEvent e)
{
a=this.field2.getText();
b=this.field3.getText();
q=this.field1.getText();
g=this.area2.getText();
if(e.getSource()==button1)
{
if(g.equals(""))
{
JOptionPane.showMessageDialog(this,"不能发送空消息!");
area2.requestFocus(true);
}
else
{
try{
OutputStream out=socket.getOutputStream();
PrintWriter pw=new PrintWriter(out,true);
pw.println(q+" "+new Date().toLocaleString()+"\n"+" "+g);
area2.setText("");
area2.requestFocus(true);
}catch(Exception x){
x.printStackTrace();
}
}
}
if(e.getSource()==button3)
{
if(a.equals("")&&b.equals(""))
{
JOptionPane.showMessageDialog(this,"IP地址或端口为空!");
field2.requestFocus(true);
}
else if(a.equals(""))
{
JOptionPane.showMessageDialog(this,"请填写IP地址");
field2.requestFocus(true);
}
else if(b.equals(""))
{
JOptionPane.showMessageDialog(this,"请填写端口");
field3.requestFocus(true);
}
else
{
//this.join(a);
try{
socket=new Socket(a,Integer.parseInt(b));
field1.setEditable(false);
field2.setEditable(false);
field3.setEditable(false);
button3.setEnabled(false);
button3.setText("连接中...");
JOptionPane.showMessageDialog(this,"连接成功!");
area2.requestFocus(true);
new ClientReceiveThread(socket.getInputStream(),area1,area3).start();
}catch(Exception x){
JOptionPane.showMessageDialog(this,"连接服务器失败!");
x.printStackTrace();
}
if(q.equals(""))
{
try{
OutputStream out=socket.getOutputStream();
PrintWriter pw=new PrintWriter(out,true);
pw.println("");
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str=br.readLine();
area1.append(str+"\n");
field1.setText(str.replace("连接",""));//String类的方法(替换)
area3.append(str.replace("连接","")+"\n");
}catch(Exception x){
x.printStackTrace();
}
}
else
{
try{
OutputStream out=socket.getOutputStream();
PrintWriter pw=new PrintWriter(out,true);
pw.println(q+"连接");
}catch(Exception x){
x.printStackTrace();
}
}
}
}
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new Client();
}
}
class ClientReceiveThread extends Thread
{
InputStream in;
JTextArea area4,area5;
public ClientReceiveThread(InputStream in,JTextArea area,JTextArea area1)
{
this.in=in;
this.area4=area;
this.area5=area1;
}
public void run()
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(in));
while(true)
{
String str=br.readLine();
area4.append(str+"\n");
if(str.endsWith("连接"))//测试此字符串是否以指定的后缀结束。
{
area5.append(str.replace("连接","")+"\n");
}
}
}catch(Exception e)
{
String s="服务器已断开连接!";
new Client(s);
e.printStackTrace();
}
}
}
class Shijian extends Thread
{
JLabel label;
public Shijian(JLabel label)
{
this.label=label;
}
public void run()
{
while(true)
{
try{
label.setText(new Date().toLocaleString());
}catch(Exception e){
e.printStackTrace();
}
}
}
}
/*class Shuchu extends Thread
{
OutputStream out;
public Shuchu(OutputStream out)
{
this.out=out;
}
public void run()
{
PrintWriter pw=new PrintWriter(out,true);
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
try{
pw.println(new Client().g);//br.readLine());
}catch(Exception e){
e.printStackTrace();
}
}
}
}*/