首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

java一对一聊天程序,客户端发的消息无法在服务端显示 小弟我用的是eclipse

2012-01-14 
java一对一聊天程序,客户端发的消息无法在服务端显示 我用的是eclipse服务端:import java.io.*import jav

java一对一聊天程序,客户端发的消息无法在服务端显示 我用的是eclipse
服务端:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.net.*;

public class L_serve implements ActionListener
{
  JTextArea said;
  JTextField chat;
  JButton send;

 public L_serve()
 {
  JFrame f=new JFrame("JAVA QQ服务端");
  f.setLayout(null);
  f.setSize(450,400);
  said=new JTextArea();
  said.setBounds(0,0,450,200);
  chat=new JTextField(20);
  chat.setBounds(0,210,450,100);
 chat.addActionListener(this);
  send=new JButton("发送");
  send.setBounds(100,320,70,20);
  send.addActionListener(this);
  f.add(chat);
  f.add(said);
  f.add(send);
  f.setVisible(true);
  } 
 public void listenClient() throws IOException
 {
 try{
  ServerSocket server=null;
  try{ 
  server=new ServerSocket(7641);//创建一个ServerSocket在端口4700监听客户请求
  }catch(Exception e) {
  System.out.println("can not listen t"+e); //在屏幕上显示出错信息
  said.append("duankou"+e);
  }
  Socket socket=null;
  try{
  socket=server.accept();//使用accept()阻塞等待客户请求,有客户
  //请求到来则产生一个Socket对象,并继续执行
  }catch(Exception e) {
  System.out.println("Error."+e);
  said.append("ERROR."+e);
  }
  String line;
  BufferedReader in=new BufferedReader(new InputStreamReader(
  socket.getInputStream()));
  //由Socket对象得到输入流,并构造相应的BufferedReader对象
  PrintWriter out=new PrintWriter(socket.getOutputStream());
  //由Socket对象得到输出流,并构造PrintWriter对象
  BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
  //由系统标准输入设备构造BufferedReader对象
  System.out.println("Client:"+in.readLine());//在标准输出上打印从客户端读入的字符串
  said.append("Client:"+in.readLine());
  line=sin.readLine(); //从标准输入读入一字符串
  while(!line.equals("bye")){ //如果该字符串为 "bye",则停止循环
  out.println(line); //向客户端输出该字符串
  out.flush(); //刷新输出流,使Client马上收到该字符串
  System.out.println("Server:"+line); //在屏幕上显示读入的字符串
  said.append("Server:"+line);
  System.out.println("Client:"+in.readLine());//从Client读入一字符串,并打印到标准输出上
  said.append("Client:"+in.readLine());
  line=sin.readLine(); //从系统标准输入读入一字符串
  } //继续循环
  out.close(); //关闭Socket输出流
  in.close(); //关闭Socket输入流
  socket.close(); //关闭Socket
  server.close(); //关闭ServerSocket
  }catch(Exception e){
  System.out.println("Error:"+e); 
  }
 }
 public void actionPerformed(ActionEvent e)
 {  
  if(e.getSource()==send){
  String str=chat.getText();
  said.append("server:"+str+'\n');}
  chat.setText(null);
 }
 public static void main(String args[]) throws IOException
 {
  L_serve s=new L_serve();  
  s.listenClient();
 }
}

客户端:

import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.net.*;

public class L_client implements ActionListener
{


  JTextArea said;
  JTextField chat;
  JButton send;

 public L_client()
 {
  JFrame f=new JFrame("JAVA QQ客户端");
  f.setLayout(null);
  f.setSize(450,400);
  said=new JTextArea();
  said.setBounds(0,0,450,200);
  chat=new JTextField(20);
  chat.setBounds(0,210,450,100);
 chat.addActionListener(this);
  send=new JButton("发送");
  send.setBounds(100,320,70,20);
  send.addActionListener(this);
  f.add(chat);
  f.add(said);
  f.add(send);
  f.setVisible(true);
  } 
 public void startNet() throws IOException
 {
 try{
  Socket socket=new Socket("127.0.0.1",7641); 
  BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
  //由系统标准输入设备构造BufferedReader对象
  PrintWriter out=new PrintWriter(socket.getOutputStream());
  //由Socket对象得到输出流,并构造PrintWriter对象
  BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
  //由Socket对象得到输入流,并构造相应的BufferedReader对象
  String readline;
  readline=sin.readLine(); //从系统标准输入读入一字符串
  while(!readline.equals("bye")){ //若从标准输入读入的字符串为“bye”则停止循环
  out.println(readline); //将从系统标准输入读入的字符串输出到Server
  out.flush(); //刷新输出流,使Server马上收到该字符串
  System.out.println("Client:"+readline); //在系统标准输出上打印读入的字符串
  said.append("Client:"+readline);
  System.out.println("Server:"+in.readLine());
  said.append("Server:"+in.readLine());
  //从Server读入一字符串,并打印到标准输出上
  readline=sin.readLine(); //从系统标准输入读入一字符串
  } //继续循环
  out.close(); //关闭Socket输出流
  in.close(); //关闭Socket输入流
  socket.close(); //关闭Socket
  }catch(Exception e) {
  System.out.println("Error"+e); //出错,则打印出错信息
  said.append("Error"+e);
  }
 }
 public void actionPerformed(ActionEvent e)
 {  
  if(e.getSource()==send){
   
  String str=chat.getText();
  said.append("client: "+str+'\n');}
  chat.setText(null);
 }
 public static void main(String args[]) throws IOException
 {
  L_client c=new L_client();  
  c.startNet();
 }
}


[解决办法]
检查一下连接是否建立
[解决办法]
System.out.println("Client:"+in.readLine());//从Client读入一字符串,并打印到标准输出上

said.append("Client:"+in.readLine());//服务端会阻塞在这里

line=sin.readLine(); //从系统标准输入读入一字符串,这里又是一个阻塞,循环会一直停在这里等待你从屏幕输入字符,后面的都不会执行

改成
String temp = in.readLine();
System.out.println("Client:"+temp);
said.append("Client:"+temp);

程序里面有很多地方都发生了阻塞

热点排行