做成一个局域网聊天室,在一台主机上同时打开服务器段和客户端,服务器可以识别任何一个客户端,但是在一台主机上运行服务器端 ,另一台主机上运行客户端,则服务器端不能识别客户端,聊天功能则不能用
客户端的代码如下
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.util.*;public class Client extends JFrame implements Runnable,ActionListener
{
static
{
int ff=4;
JList list=new JList();
}
JDialog progressDialog=new JDialog(this,"正在登录服务器",false);
//progressDialog.add(new JLabel("dededde"),BorderLayout.CENTER);
public static final int PORT=8765;
public static final int ff=3;
//声明并创建各个组建
JPanel northPanel=new JPanel();//用于将以下各个组建装入
JLabel userLabel=new JLabel("用户名");
JTextField userField=new JTextField(10);//输入用户名
JButton enterButton=new JButton("登录");
JButton exitButton=new JButton("注销");
/******定义静态变量list,但是在ClientThread类中却无法访问***********/
public static JList list=new JList();
JPanel ceterPanel=new JPanel();//中间容器
JTextArea textArea=new JTextArea("");//显示各个客户说的话
JPanel eastPanel=new JPanel();//东边容器
JLabel serverLabel=new JLabel("服务器地址");
JTextField serverField=new JTextField("");
JLabel portLabel=new JLabel("端口");
JTextField portField=new JTextField("8765");
JPanel southPanel=new JPanel();
JTextArea megField=new JTextArea();//显示用户要发送的信息
JButton sendButton=new JButton("发送");
Container con=this.getContentPane();
Socket socket;//声明套接字对象
Thread thread;//声明线程对象
String ip=null;
String user; //用户名
String sendMeg,serverMeg;//分别是要发言的信息,从服务器接收到的信息
DataInputStream in;
DataOutputStream out;
boolean isChatting=false; //标记客户端的状态
//构造函数Client
public Client()
{
this.getContentPane().setForeground(Color.CYAN);
this.textArea.setBackground(Color.magenta);
this.textArea.setEditable(false);
//为按钮添加事件
enterButton.addActionListener(this);
exitButton.addActionListener(this);
sendButton.addActionListener(this);
this.enterButton.setBackground(Color.BLUE);
this.exitButton.setBackground(Color.BLUE);
//窗口布局
northPanel.add(userLabel);
northPanel.add(userField);
northPanel.add(enterButton);
northPanel.add(exitButton);
eastPanel.setLayout(new GridLayout(6,1));//使用网格布局
eastPanel.add(this.serverLabel);
eastPanel.add(this.serverField);
eastPanel.add(this.portLabel);
eastPanel.add(this.portField);
eastPanel.add(new JLabel("所有在线好友"));
eastPanel.add(this.list);
//*********************
this.ceterPanel.setLayout(new BorderLayout(5,5));
JScrollPane scrollPane1=new JScrollPane();
scrollPane1.getViewport().add(this.textArea);
this.ceterPanel.add(scrollPane1,BorderLayout.CENTER);
this.ceterPanel.add(this.eastPanel,BorderLayout.EAST);
JScrollPane scrollPane2=new JScrollPane();
scrollPane2.getViewport().add(this.megField);
this.southPanel.setLayout(new BorderLayout());
this.southPanel.add(scrollPane2,BorderLayout.CENTER);
this.southPanel.add(this.sendButton,BorderLayout.EAST);
con.setLayout(new BorderLayout(5,5));
con.add(this.ceterPanel,BorderLayout.CENTER);
con.add(this.northPanel,BorderLayout.NORTH);
con.add(this.southPanel,BorderLayout.SOUTH);
this.setSize(800,600);
this.setTitle("快乐无忧聊天室客户端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run()
{
while(true)
{
try
{
//读取服务器发送来的数据信息,并显示在对话框中
this.serverMeg=this.in.readUTF();
this.textArea.append(this.serverMeg);
}
catch(IOException e)
{
}
}
}
//创建进入聊天室时候的进度条并初始化
public void InitProgressDialog()
{
progressDialog=new JDialog(this,"正在进入聊天室",false);
this.progressDialog.setSize(300,700);
this.progressDialog.setLocation(700,10);
JProgressBar progressBar=new JProgressBar();
progressBar.setIndeterminate(true);
progressBar.setVisible(true);
//this.progressDialog.getContentPane().add(new JLabel("dededde"),BorderLayout.CENTER);
this.progressDialog.getContentPane().add(progressBar,BorderLayout.NORTH);
this.progressDialog.setVisible(true);
try
{
Thread.currentThread().sleep(3000);
}
catch(InterruptedException ee)
{
}
this.progressDialog.setVisible(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==this.enterButton) //点击"进入聊天室"按钮
{
InitProgressDialog();
this.textArea.setForeground(Color.GREEN);
this.textArea.setText(" 您好,欢迎来到快乐无忧聊天室!!"+"\n");
try
{
//创建socket对象
socket=new Socket(this.ip,PORT);
//创建客户端数据输入输出流,用于对服务器端发送或接收数据
this.in=new DataInputStream(this.socket.getInputStream());
this.out=new DataOutputStream(this.socket.getOutputStream());
}
catch(IOException e1)
{
System.out.println("Can not connect");
}
this.thread=new Thread(this);
this.thread.start();
isChatting=true;
}
else if(e.getSource()==this.sendButton)//当点击"发送"按钮时候
{
this.sendMeg=this.megField.getText();
if(this.sendMeg!=null)
{
//发言,向服务器发送发言的信息
try
{
this.out.writeUTF(this.userField.getText()+
"对大家说:"+this.sendMeg+"\n");
}
catch(IOException e2)
{
}
}
else
{
try
{
this.out.writeUTF("请说话");
}
catch(IOException e3)
{
}
}
}
else if(e.getSource()==this.exitButton)//点击"退出聊天室"按钮
{
if(isChatting)
{
try
{
this.socket.close();
}
catch(IOException e4)
{
}
this.thread.destroy();
}
isChatting=false;
this.setVisible(false);
}
}
public static void main(String[] args)
{
new Client().setVisible(true);
}
}
客户端的代码如下
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.util.*;public class Client extends JFrame implements Runnable,ActionListener
{
static
{
int ff=4;
JList list=new JList();
}
JDialog progressDialog=new JDialog(this,"正在登录服务器",false);
//progressDialog.add(new JLabel("dededde"),BorderLayout.CENTER);
public static final int PORT=8765;
public static final int ff=3;
//声明并创建各个组建
JPanel northPanel=new JPanel();//用于将以下各个组建装入
JLabel userLabel=new JLabel("用户名");
JTextField userField=new JTextField(10);//输入用户名
JButton enterButton=new JButton("登录");
JButton exitButton=new JButton("注销");
/******定义静态变量list,但是在ClientThread类中却无法访问***********/
public static JList list=new JList();
JPanel ceterPanel=new JPanel();//中间容器
JTextArea textArea=new JTextArea("");//显示各个客户说的话
JPanel eastPanel=new JPanel();//东边容器
JLabel serverLabel=new JLabel("服务器地址");
JTextField serverField=new JTextField("");
JLabel portLabel=new JLabel("端口");
JTextField portField=new JTextField("8765");
JPanel southPanel=new JPanel();
JTextArea megField=new JTextArea();//显示用户要发送的信息
JButton sendButton=new JButton("发送");
Container con=this.getContentPane();
Socket socket;//声明套接字对象
Thread thread;//声明线程对象
String ip=null;
String user; //用户名
String sendMeg,serverMeg;//分别是要发言的信息,从服务器接收到的信息
DataInputStream in;
DataOutputStream out;
boolean isChatting=false; //标记客户端的状态
//构造函数Client
public Client()
{
this.getContentPane().setForeground(Color.CYAN);
this.textArea.setBackground(Color.magenta);
this.textArea.setEditable(false);
//为按钮添加事件
enterButton.addActionListener(this);
exitButton.addActionListener(this);
sendButton.addActionListener(this);
this.enterButton.setBackground(Color.BLUE);
this.exitButton.setBackground(Color.BLUE);
//窗口布局
northPanel.add(userLabel);
northPanel.add(userField);
northPanel.add(enterButton);
northPanel.add(exitButton);
eastPanel.setLayout(new GridLayout(6,1));//使用网格布局
eastPanel.add(this.serverLabel);
eastPanel.add(this.serverField);
eastPanel.add(this.portLabel);
eastPanel.add(this.portField);
eastPanel.add(new JLabel("所有在线好友"));
eastPanel.add(this.list);
//*********************
this.ceterPanel.setLayout(new BorderLayout(5,5));
JScrollPane scrollPane1=new JScrollPane();
scrollPane1.getViewport().add(this.textArea);
this.ceterPanel.add(scrollPane1,BorderLayout.CENTER);
this.ceterPanel.add(this.eastPanel,BorderLayout.EAST);
JScrollPane scrollPane2=new JScrollPane();
scrollPane2.getViewport().add(this.megField);
this.southPanel.setLayout(new BorderLayout());
this.southPanel.add(scrollPane2,BorderLayout.CENTER);
this.southPanel.add(this.sendButton,BorderLayout.EAST);
con.setLayout(new BorderLayout(5,5));
con.add(this.ceterPanel,BorderLayout.CENTER);
con.add(this.northPanel,BorderLayout.NORTH);
con.add(this.southPanel,BorderLayout.SOUTH);
this.setSize(800,600);
this.setTitle("快乐无忧聊天室客户端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run()
{
while(true)
{
try
{
//读取服务器发送来的数据信息,并显示在对话框中
this.serverMeg=this.in.readUTF();
this.textArea.append(this.serverMeg);
}
catch(IOException e)
{
}
}
}
//创建进入聊天室时候的进度条并初始化
public void InitProgressDialog()
{
progressDialog=new JDialog(this,"正在进入聊天室",false);
this.progressDialog.setSize(300,700);
this.progressDialog.setLocation(700,10);
JProgressBar progressBar=new JProgressBar();
progressBar.setIndeterminate(true);
progressBar.setVisible(true);
//this.progressDialog.getContentPane().add(new JLabel("dededde"),BorderLayout.CENTER);
this.progressDialog.getContentPane().add(progressBar,BorderLayout.NORTH);
this.progressDialog.setVisible(true);
try
{
Thread.currentThread().sleep(3000);
}
catch(InterruptedException ee)
{
}
this.progressDialog.setVisible(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==this.enterButton) //点击"进入聊天室"按钮
{
InitProgressDialog();
this.textArea.setForeground(Color.GREEN);
this.textArea.setText(" 您好,欢迎来到快乐无忧聊天室!!"+"\n");
try
{
//创建socket对象
socket=new Socket(this.ip,PORT);
//创建客户端数据输入输出流,用于对服务器端发送或接收数据
this.in=new DataInputStream(this.socket.getInputStream());
this.out=new DataOutputStream(this.socket.getOutputStream());
}
catch(IOException e1)
{
System.out.println("Can not connect");
}
this.thread=new Thread(this);
this.thread.start();
isChatting=true;
}
else if(e.getSource()==this.sendButton)//当点击"发送"按钮时候
{
this.sendMeg=this.megField.getText();
if(this.sendMeg!=null)
{
//发言,向服务器发送发言的信息
try
{
this.out.writeUTF(this.userField.getText()+
"对大家说:"+this.sendMeg+"\n");
}
catch(IOException e2)
{
}
}
else
{
try
{
this.out.writeUTF("请说话");
}
catch(IOException e3)
{
}
}
}
else if(e.getSource()==this.exitButton)//点击"退出聊天室"按钮
{
if(isChatting)
{
try
{
this.socket.close();
}
catch(IOException e4)
{
}
this.thread.destroy();
}
isChatting=false;
this.setVisible(false);
}
}
public static void main(String[] args)
{
new Client().setVisible(true);
}
}
楼主【hearrt】截止到2008-07-07 15:37:38的历史汇总数据(不包括此帖):
发帖的总数量:8 发帖的总分数:200
结贴的总数量:1 结贴的总分数:60
无满意结贴数:0 无满意结贴分:0
未结的帖子数:7 未结的总分数:140
结贴的百分比:12.50 % 结分的百分比:30.00 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
{
int ff=4;
JList list=new JList();
}
JDialog progressDialog=new JDialog(this,"正在登录服务器",false);
//progressDialog.add(new JLabel("dededde"),BorderLayout.CENTER);
public static final int PORT=8765;
public static final int ff=3;
//声明并创建各个组建
JPanel northPanel=new JPanel();//用于将以下各个组建装入
JLabel userLabel=new JLabel("用户名");
JTextField userField=new JTextField(10);//输入用户名
JButton enterButton=new JButton("登录");
JButton exitButton=new JButton("注销");