我的服务器端程序是采用多线程的方式来处理客户端的请求的,但是当客户端发送来字符串以后,服务器端 的线程可以启动,但是就是读不出 客户端发送的数据,一直弄不懂是怎么回事,我的服务器端代码如下:
import java.io.*;
import java.net.*;
import java.util.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Serverside {
ServerSocket SSsocket;
Socket socket;
Vector vet=new Vector();
public Serverside(){
try{
SSsocket=new ServerSocket(4445);
System.out.println("服务已经启动....");
}catch(IOException ioe){
System.out.println("IOE:"+ioe.getMessage());
System.out.println("服务终止!");
System.exit(-1);
}
while(true){
try{
BufferedReader br;
socket=SSsocket.accept();
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(IOException ioe){
System.out.println("ioe:"+ioe.getMessage());
}
vet.addElement(socket);
multiThread multh=new multiThread(vet,socket);
multh.start();
// new multiThread(vet,socket).start();
}
}
public static void main(String[] args){
new Serverside();
}
}class multiThread extends Thread{
Vector vet;
Socket socket;
BufferedReader in;
PrintWriter pw;
String line;
Enumeration enu;
Socket client;
boolean running = true;
public multiThread(Vector vet,Socket socket){
this.vet=vet;
this.socket=socket;
enu=vet.elements();
System.out.println("测试1");
}
public void run(){
try{
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
System.out.println("测试2");
// line=in.readLine();
// System.out.println("LINE:"+line);
}catch(IOException ioe){
System.out.println("ioE:"+ioe.getMessage());
}
while(running){
System.out.println("测试3");
try{
line=in.readLine();//服务器端侦听到来自客户端的连接以后到了这里为什么读不数据出来?
System.out.println("收到消息:"+line);
System.out.println("测试4");//这里无法打印
}catch(IOException ioe){
System.out.println("读错误:"+ioe.getMessage());
}
if(line.equalsIgnoreCase("end")){
enu=vet.elements();
while(enu.hasMoreElements()){
if(socket==((Socket)enu.nextElement())){
vet.removeElement(socket);
break;
}
}
running=false;//结束当前线程
}
else{
if(line!=null&&!line.equalsIgnoreCase("RegistToServer")){
while(enu.hasMoreElements()){
client=(Socket)enu.nextElement();
try{
pw=new PrintWriter(client.getOutputStream(),true);
pw.println(client.getInetAddress().getHostName()+":");
pw.println(line);
}catch(IOException ioe){
System.out.println("内循环:"+ioe.getMessage());
}
}
}
}
}
try{
if(in!=null)
in.close();
if(pw!=null)
pw.close();
if(client!=null)
client.close();
if(socket!=null)
socket.close();
}catch(IOException ioe){
System.out.println("关闭线程:"+ioe.getMessage());
}
}
}客户端代码如下:import java.awt.*;
import java.awt.event.*;import javax.swing.*;import java.io.*;
import java.net.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientSide extends JFrame{
JTextArea JEPdis=new JTextArea();
JTextArea JEPinput=new JTextArea();
JButton JBsend=new JButton("Send");
JPanel priJP=new JPanel(new BorderLayout());
JPanel botJP=new JPanel(new GridLayout(2,1));//底部2行1列
JPanel botJP1=new JPanel(new BorderLayout());
JPanel botJP2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JScrollPane jsc1;
JScrollPane jsc2;
BufferedReader in;
PrintWriter pw;
Socket socket;
String line;
boolean reading=true;
public ClientSide(){
init();
}
public void init(){
setPaneLayout();//面板布局设置
ConnetToServer();//连接到服务器
AddListentor();//添加监听
}
public void ConnetToServer(){
try{
socket=new Socket("shanghai",4445);
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw=new PrintWriter(socket.getOutputStream());
pw.println("RegistToServer");//在服务器上面注册
}catch(UnknownHostException ioe){
System.out.println("法连接到主机:"+ioe.getMessage());
System.exit(-1);
}catch(IOException ukh){
System.out.println("客户端ukh:"+ukh.getMessage());
System.exit(-1);
}
// ReadFromServer();
}
public void ReadFromServer(){
while(reading){
try{
if(in!=null){
line=in.readLine();
JEPdis.append(line);
}
}catch(IOException iot){
System.out.println("客户端2:"+iot.getMessage());
iot.printStackTrace();
System.exit(-1);
}
}
}
public void setPaneLayout(){
jsc1=new JScrollPane(JEPdis);
jsc2=new JScrollPane(JEPinput);
JEPdis.setEditable(false);
getContentPane().add(priJP);
priJP.add(jsc1,BorderLayout.CENTER);
priJP.add(botJP,BorderLayout.SOUTH);
botJP.add(botJP1);
botJP.add(botJP2);
botJP1.setBackground(Color.RED);
botJP1.add(jsc2,BorderLayout.CENTER);
botJP2.add(JBsend);
setTitle("java群聊");
setSize(600,400);
setVisible(true);
}
public void AddListentor(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
reading=false;
pw.println("end");
if(in!=null)
try{
in.close();
}catch(IOException ioe){
System.out.println("客户端关闭in:"+ioe.getMessage());
}
if(pw!=null)
pw.close();
if(socket!=null)
try{
socket.close();
}catch(IOException ioe){
System.out.println("客户端关闭socket:"+ioe.getMessage());
}
System.exit(0);
}
});
JBsend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(JEPinput.getText()!="")
pw.println(JEPinput.getText());
}
});
}
public static void main(String[] args){
ClientSide cs=new ClientSide();
cs.ReadFromServer();
}
}
import java.io.*;
import java.net.*;
import java.util.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Serverside {
ServerSocket SSsocket;
Socket socket;
Vector vet=new Vector();
public Serverside(){
try{
SSsocket=new ServerSocket(4445);
System.out.println("服务已经启动....");
}catch(IOException ioe){
System.out.println("IOE:"+ioe.getMessage());
System.out.println("服务终止!");
System.exit(-1);
}
while(true){
try{
BufferedReader br;
socket=SSsocket.accept();
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(IOException ioe){
System.out.println("ioe:"+ioe.getMessage());
}
vet.addElement(socket);
multiThread multh=new multiThread(vet,socket);
multh.start();
// new multiThread(vet,socket).start();
}
}
public static void main(String[] args){
new Serverside();
}
}class multiThread extends Thread{
Vector vet;
Socket socket;
BufferedReader in;
PrintWriter pw;
String line;
Enumeration enu;
Socket client;
boolean running = true;
public multiThread(Vector vet,Socket socket){
this.vet=vet;
this.socket=socket;
enu=vet.elements();
System.out.println("测试1");
}
public void run(){
try{
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
System.out.println("测试2");
// line=in.readLine();
// System.out.println("LINE:"+line);
}catch(IOException ioe){
System.out.println("ioE:"+ioe.getMessage());
}
while(running){
System.out.println("测试3");
try{
line=in.readLine();//服务器端侦听到来自客户端的连接以后到了这里为什么读不数据出来?
System.out.println("收到消息:"+line);
System.out.println("测试4");//这里无法打印
}catch(IOException ioe){
System.out.println("读错误:"+ioe.getMessage());
}
if(line.equalsIgnoreCase("end")){
enu=vet.elements();
while(enu.hasMoreElements()){
if(socket==((Socket)enu.nextElement())){
vet.removeElement(socket);
break;
}
}
running=false;//结束当前线程
}
else{
if(line!=null&&!line.equalsIgnoreCase("RegistToServer")){
while(enu.hasMoreElements()){
client=(Socket)enu.nextElement();
try{
pw=new PrintWriter(client.getOutputStream(),true);
pw.println(client.getInetAddress().getHostName()+":");
pw.println(line);
}catch(IOException ioe){
System.out.println("内循环:"+ioe.getMessage());
}
}
}
}
}
try{
if(in!=null)
in.close();
if(pw!=null)
pw.close();
if(client!=null)
client.close();
if(socket!=null)
socket.close();
}catch(IOException ioe){
System.out.println("关闭线程:"+ioe.getMessage());
}
}
}客户端代码如下:import java.awt.*;
import java.awt.event.*;import javax.swing.*;import java.io.*;
import java.net.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientSide extends JFrame{
JTextArea JEPdis=new JTextArea();
JTextArea JEPinput=new JTextArea();
JButton JBsend=new JButton("Send");
JPanel priJP=new JPanel(new BorderLayout());
JPanel botJP=new JPanel(new GridLayout(2,1));//底部2行1列
JPanel botJP1=new JPanel(new BorderLayout());
JPanel botJP2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JScrollPane jsc1;
JScrollPane jsc2;
BufferedReader in;
PrintWriter pw;
Socket socket;
String line;
boolean reading=true;
public ClientSide(){
init();
}
public void init(){
setPaneLayout();//面板布局设置
ConnetToServer();//连接到服务器
AddListentor();//添加监听
}
public void ConnetToServer(){
try{
socket=new Socket("shanghai",4445);
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw=new PrintWriter(socket.getOutputStream());
pw.println("RegistToServer");//在服务器上面注册
}catch(UnknownHostException ioe){
System.out.println("法连接到主机:"+ioe.getMessage());
System.exit(-1);
}catch(IOException ukh){
System.out.println("客户端ukh:"+ukh.getMessage());
System.exit(-1);
}
// ReadFromServer();
}
public void ReadFromServer(){
while(reading){
try{
if(in!=null){
line=in.readLine();
JEPdis.append(line);
}
}catch(IOException iot){
System.out.println("客户端2:"+iot.getMessage());
iot.printStackTrace();
System.exit(-1);
}
}
}
public void setPaneLayout(){
jsc1=new JScrollPane(JEPdis);
jsc2=new JScrollPane(JEPinput);
JEPdis.setEditable(false);
getContentPane().add(priJP);
priJP.add(jsc1,BorderLayout.CENTER);
priJP.add(botJP,BorderLayout.SOUTH);
botJP.add(botJP1);
botJP.add(botJP2);
botJP1.setBackground(Color.RED);
botJP1.add(jsc2,BorderLayout.CENTER);
botJP2.add(JBsend);
setTitle("java群聊");
setSize(600,400);
setVisible(true);
}
public void AddListentor(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
reading=false;
pw.println("end");
if(in!=null)
try{
in.close();
}catch(IOException ioe){
System.out.println("客户端关闭in:"+ioe.getMessage());
}
if(pw!=null)
pw.close();
if(socket!=null)
try{
socket.close();
}catch(IOException ioe){
System.out.println("客户端关闭socket:"+ioe.getMessage());
}
System.exit(0);
}
});
JBsend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(JEPinput.getText()!="")
pw.println(JEPinput.getText());
}
});
}
public static void main(String[] args){
ClientSide cs=new ClientSide();
cs.ReadFromServer();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class chatServer extends JFrame {
//以下为图形界面变量
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JLabel jLabel1 = new JLabel();
static java.awt.List jList1 = new java.awt.List(13);
JScrollPane scrollpane=new JScrollPane(jList1);
//以下为网络相关变量
static Vector clients=new Vector(10); //用vector向量数组存储连接客户变量
static ServerSocket server=null; //建立服务器socket
static int active_connects=0; //用来存储目前连接的客户数
static Socket socket=null; //用来存储一个套接字连接
//chatServer main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
chatServer chatServer1=new chatServer(); //实例化一个chatServer类
chatServer1.show();
System.out.println("Server starting ...");
try
{
server=new ServerSocket(2525); //使用端口2525初始化服务器套接字
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
while(true)
{
if(clients.size()<5) //当客户数小于5个时开始连接
{
try
{
socket=server.accept(); //用来存储连接上的客户socket
if(socket!=null)
{
System.out.println(socket+"连接"); //在控制台打印客户连接信息
}
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
int i=0;
do{
Client c=new Client(socket); //定义并实例化一个Client线程类,一个就对应一个客户连接
clients.addElement(c); //加入clients数组中
if(checkName(c)) //调用checkName方法验证c的合法性
{
int connum=++chatServer1.active_connects; //定义connum来存储活动连接数
String constr="目前有"+connum+"客户相连"; //在状态栏里显示连接数
chatServer1.statusBar.setText(constr);
Client listdata=(Client)clients.elementAt(i); //将连接客户的socket信息存储进listdata数组
chatServer1.jList1.addItem(listdata.ip+"连接",i); //将客户socket信息写入list框
c.start(); //启动线程
notifyRoom(); //用notifyRoom方法来监视聊天室连接变化
//不断改变clients数组并刷新客户端信息
}
else
{
//如果名字不合法
c.ps.println("TAKEN");
disconnect(c);
}
i++;
break;
}
while(i<clients.size());
}
else //如果clients数组超过了5个
{
try{Thread.sleep(200);}
catch(InterruptedException e)
{
}
}
}//end of while
}// end of main method /**Construct the frame*/
public chatServer() //chatServer类的构造器用来初始化一些UI信息
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("简易聊天服务器端");
statusBar.setText("目前的连接数为:");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuFileExit_actionPerformed(e);
}
});
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuHelpAbout_actionPerformed(e);
}
});
jPanel1.setLayout(borderLayout2);
jLabel1.setText("服务器端连接客户");
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, BorderLayout.NORTH);
jPanel1.add(scrollpane, BorderLayout.CENTER);
}//end of jbinit
/**File | Exit action performed*/
public void jMenuFileExit_actionPerformed(ActionEvent e) //实现退出菜单方法
{
sendClients(new StringBuffer("QUIT")); //向客户端发送断开连接信息
closeAll(); //调用closeAll方法关闭所有连接
System.exit(0);
}
/**Help | About action performed*/
public void jMenuHelpAbout_actionPerformed(ActionEvent e) //实现about对话框,意义不大,可以去掉
{
chatServer_AboutBox dlg = new chatServer_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.show();
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) //实现关闭服务器程序要进行的操作
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
} /*以下实现各种方法*/
public static void notifyRoom() //用来监视连接信息,不断刷新clients数组并刷新客户端用户列表信息
{
StringBuffer people=new StringBuffer("PEOPLE");
for(int i=0;i<clients.size();i++)
{
Client c=(Client)clients.elementAt(i);
people.append(":"+c.name);
}
sendClients(people); //用sendClients方法向客户端发送信息
}
{
for(int i=0;i<clients.size();i++)
{
Client c=(Client)clients.elementAt(i);
c.send(msg);
}
} public static void closeAll() //实现关闭所有连接信息
{
while(clients.size()>0) //遍历clients数组删除所有连接客户信息
{
Client c=(Client)clients.firstElement();
try
{
c.socket.close();
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
finally
{
clients.removeElement(c);
}
}//end of while
}//end of closeAll method public static boolean checkName(Client newclient) //实现检查连接客户的socket信息是否合法
{
for(int i=0;i<clients.size();i++)
{
Client c=(Client)clients.elementAt(i);
if((c!=newclient)&&c.equals(newclient.name))
return false;
}
return(true);
}//end of checkName method public static synchronized void disconnect(Client c) //实现断开单个客户的方法
{
try
{
jList1.addItem(c.ip+"断开连接"); //在服务器端程序的list框中显示断开信息
chatServer.active_connects--; //将连接数减1
c.send(new StringBuffer("QUIT")); //向客户发送断开连接信息
c.socket.close();
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
finally
{
clients.removeElement(c); //从clients数组中删除此客户的相关socket等信息
}
}
}
class Client extends Thread //实现 Client线程类
{
Socket socket; //用来存储一个连接客户的socket信息
String name; //用来存储客户的连接姓名
String ip; //用来存储客户的ip信息
DataInputStream dis; //用来实现接受从客户端发来的数据流
PrintStream ps; //用来实现向客户端发送信息的打印流 public void send(StringBuffer msg) //实现想客户端发送信息的方法
{
ps.println(msg); //用打印流发送信息
ps.flush();
} public Client(Socket s) //Client线程类的构造器
{
socket=s;
try
{
dis=new DataInputStream(s.getInputStream()); //存储特定客户socket的输入流接受s这个客户发送到服务器端的信息
ps=new PrintStream(s.getOutputStream()); //存储特定客户socket的输出流发送服务器给s这个客户的信息
String info=dis.readLine(); //读取接受来的信息
StringTokenizer stinfo=new StringTokenizer(info,":"); //用StringTokenizer类来读取用":"分段字符
String head=stinfo.nextToken(); //head用来存储类似于关键字的头信息
if(stinfo.hasMoreTokens())
name=stinfo.nextToken(); //关键字后的第二段数据是客户名信息
if(stinfo.hasMoreTokens())
ip=stinfo.nextToken(); //关键字后的第三段数据是客户ip信息
System.out.println(head); //在控制台打印头信息
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
}//end of Client constrator public void run() //线程运行方法
{
while(true)
{
String line=null;
try
{
line=dis.readLine(); //读取客户端发来的数据流
}
catch(IOException e)
{
System.out.println("Error"+e);
chatServer.disconnect(this);
chatServer.notifyRoom();
return;
} if(line==null) //客户已离开
{
chatServer.disconnect(this);
chatServer.notifyRoom();
return;
}
StringTokenizer st=new StringTokenizer(line,":");
String keyword=st.nextToken(); if(keyword.equals("MSG")) //如果关键字是MSG则是客户端发来的聊天信息
{
StringBuffer msg=new StringBuffer("MSG:"); //在服务器端再重新建立一个字符缓冲
msg.append(name);
msg.append(st.nextToken("\0"));
chatServer.sendClients(msg); //再将某个客户发来的聊天信息发送到每个连接客户的聊天栏中
}
else if(keyword.equals("QUIT")) //如果关键字是QUIT则是客户端发来断开连接的信息
{
chatServer.disconnect(this); //服务器断开与这个客户的连接
chatServer.notifyRoom(); //继续监听聊天室并刷新其他客户的聊天人名list
this.stop();
}
}
}
} //end of class Client
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.util.*;public class chatApplet extends Applet {
/*以下用于定义UI变量*/
Panel panel1 = new Panel(); //用于放置输入姓名和连接两个按钮
BorderLayout borderLayout1 = new BorderLayout();
Panel panel2 = new Panel(); //用于放置聊天信息显示和聊天人员列表
Panel panel3 = new Panel(); //用于放置发送信息区域
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
Label label1 = new Label();
TextField name_txt = new TextField(15);
Button button1 = new Button();
Button button2 = new Button();
TextArea chat_txt = new TextArea(15,30);
Label label2 = new Label();
Button button3 = new Button();
TextField msg_txt = new TextField(20);
java.awt.List list1 = new java.awt.List(13);
/*以下定义数据流和网络变量*/
Socket soc=null; //定义连接套接字
PrintStream ps=null; //定义打印流
Listen listen=null; //定义一个客户端线程
public void init() //初始化图形界面
{
resize(475,350);
this.setLayout(borderLayout1);
panel2.setLayout(flowLayout1);
panel3.setLayout(flowLayout2);
label1.setText("姓名:");
button1.setLabel("连接");
button2.setLabel("断开连接"); chat_txt.setEditable(false);
panel2.setBackground(Color.cyan);
panel1.setBackground(Color.cyan);
label2.setText("聊天信息:");
button3.setLabel("发送");
msg_txt.setText("请输入聊天信息");
panel3.setBackground(Color.cyan);
this.add(panel1, BorderLayout.NORTH);
panel1.add(label1, null);
panel1.add(name_txt, null);
panel1.add(button1, null);
panel1.add(button2, null);
this.add(panel2, BorderLayout.CENTER);
panel2.add(chat_txt, null);
panel2.add(list1, null);
this.add(panel3, BorderLayout.SOUTH);
panel3.add(label2, null);
panel3.add(msg_txt, null);
panel3.add(button3, null);
} public boolean action(Event evt,Object obj) //事件触发代码
{
if(evt.target instanceof Button)
{
String label=(String) obj;
if(label.equals("连接")) //如果点击连接后
{
if(soc==null)
{
try
{
soc=new Socket(InetAddress.getLocalHost(),2525); //使用端口2525实例化一个本地套接字
System.out.println(soc); //在控制台打印实例化的结果
ps=new PrintStream(soc.getOutputStream()); //将ps指向soc的输出流
StringBuffer info=new StringBuffer("INFO: "); //定义一个字符缓冲存储发送信息
//其中INFO为关键字让服务器识别为连接信息
//并将name和ip用":"分开,在服务器端将用一个
//StringTokenizer类来读取数据
String userinfo=name_txt.getText()+":"+InetAddress.getLocalHost().toString();
ps.println(info.append(userinfo));
ps.flush();
listen=new Listen(this,name_txt.getText(),soc); //将客户端线程实例化
listen.start(); //启动线程
}
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
} //end of if
}//end of if
else if(label.equals("断开连接")) //如果点击断开连接按钮则运行disconnect()
{
disconnect();
}
else if(label.equals("发送")) //如果点击发送按钮
{
if(soc!=null)
{
StringBuffer msg=new StringBuffer("MSG: "); //定义并实例化一个字符缓冲存储发送的聊天信息
//其中MSG为关键词
try
{
String msgtxt=new String(msg_txt.getText());
}
catch(Exception e){}
ps.println(msg.append(msg_txt.getText())); //用打印流发送聊天信息
ps.flush();
}
}
}
return true;
} //end of method action public void disconnect() //客户端点击断开连接要运行的方法
{
if(soc!=null)
{
try
{
listen.suspend();
ps.println("QUIT"); //用打印流发送QUIT信息通知服务器断开此次通信
ps.flush();
soc.close(); //关闭套接字
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
finally
{
}
}// end of if
} class Listen extends Thread //客户端线程类用来监听服务器传来的信息
{
String name=null; //用来存储客户端连接后的name信息
DataInputStream dis=null; //用来实现客户端接受服务器数据的输入流
PrintStream ps=null; //用来实现从客户端发送数据到服务器的打印流
Socket socket=null; //用来存储客户端的socket信息
chatApplet parent=null; //用来存储当前运行的chatApplet实例 public Listen(chatApplet p,String n,Socket s) //Listen类的构造器
{
//接受参数
parent=p;
name=n;
socket=s; try
{
//实例化两个数据流
dis=new DataInputStream(s.getInputStream());
ps=new PrintStream(s.getOutputStream()); }
catch(IOException e)
{
System.out.println("Error:"+e);
parent.disconnect();
}
} //end of Listen constractor
public void run() //线程运行方法
{
String msg=null;
while(true)
{
try{msg=dis.readLine();} //读取从服务器传来的信息
catch(IOException e)
{
System.out.println("Error:"+e);
parent.disconnect();
}
if (msg==null) //如果从服务器传来的信息为空则断开此次连接
{
parent.listen=null;
parent.soc=null;
parent.list1.clear();
return;
}
StringTokenizer st=new StringTokenizer(msg,":"); //用StringTokenizer类来实现读取分段字符
String keyword=st.nextToken(); //读取信息头即关键字用来识别是何种信息 if(keyword.equals("PEOPLE")) //如果是PEOPLE则是服务器发来的客户连接信息
//主要用来刷新客户端的用户列表
{
parent.list1.clear();
while(st.hasMoreTokens()) //遍历st取得目前所连接的客户
{
String str=st.nextToken();
parent.list1.addItem(str);
}
}
else if(keyword.equals("MSG")) //如果关键字是MSG则是服务器传来的聊天信息
//主要用来刷新客户端聊天信息区将每个客户的聊天内容显示出来
{
String usr=st.nextToken();
parent.chat_txt.appendText(usr);
parent.chat_txt.appendText(st.nextToken("\0"));
parent.chat_txt.appendText("\n\n");
}
else if(keyword.equals("QUIT")) //如果关键字是QUIT则是服务器关闭的信息
//用来切断此次连接
{
System.out.println("Quit");
try
{parent.listen.stop();
parent.listen=null;
parent.soc.close();
parent.soc=null;
}catch(IOException e)
{
System.out.println("Error:"+e);
}
parent.list1.clear();
return;
}
} } //end of run method
} //end of Listen inner class
} //end of chatApplet class
很可能问题出现在这里,改成这个试一下:
ps=new PrintStream(soc.getOutputStream(), true); //将ps指向soc的输出流
socket=SSsocket.accept();
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));Your "br" never used
pw=new PrintWriter(socket.getOutputStream());
...
改成
pw=new PrintWriter(socket.getOutputStream(), true);
试试