inObject=new ObjectInputStream(inl);
inl是已经得到的输入流.
服务器程序如下:
package qq;
import qq.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;public class server extends Thread{
static ServerSocket serve;
BufferedReader in;
PrintWriter out;
Socket client;
String str;//用来存储执行哪种命令 
    String str1;//用来存储账号
    
public server(Socket client){
this.client=client;
}

public void run(){
String result="";
try{//while(true){
in=new BufferedReader(new InputStreamReader(client.getInputStream()));
out=new PrintWriter(client.getOutputStream(),true);
str=in.readLine();

System.out.println(str);

if(str.equals("1")){
str1=in.readLine();
if(str!=null){
System.out.println(str);
    String sql="SELECT code FROM accustom WHERE account='"+str1+"'";
    System.out.println(sql);
      try{
    query c=new query();
    result=c.query(sql,"date");
    out.println(result);//返回查询结果,只是把print改成了println,它咋就行了???
    out.flush();
      System.out.print(result);
    out.close();
    in.close();
    }
    catch(Exception e) {JOptionPane.showMessageDialog(null,e.getMessage());} 
   //}
          }
        }
if(str.equals("2")){
landSocket serve=new landSocket();
accustom person=serve.cinObject();
System.out.println(str);
System.out.print(person.getSex());
}

    }
    catch(Exception e){JOptionPane.showMessageDialog(null,e.getMessage());}
}
public static void main(String args[]){
try{
    serve=new ServerSocket(2008);
   }
catch(Exception e){System.out.println("error");}
while(true){
  try{
   server s=new server(serve.accept());
    s.start();
     }
  catch(Exception e){System.out.println("error1");}
   }
   
}
}
其中调用的方法均来自:
package qq;
import qq.*;
import java.io.*;
import java.net.*;
import javax.swing.*;public class landSocket{
Socket server;
public BufferedReader in;
public PrintWriter out;
ObjectOutputStream outObject;
ObjectInputStream inObject;

    public landSocket(){
     try{
     server=new Socket("localhost",2008);
    
        }
     catch(Exception e){System.out.print("error");}
     } 
    public String cin(){
     String str="";
     try{
     in=new BufferedReader(new InputStreamReader(server.getInputStream()));
     str=in.readLine();
     in.close();
     }
     catch(Exception e){JOptionPane.showMessageDialog(null,e.getMessage());}
     return str;
     }
    public void cout(String str){
     try{
     out=new PrintWriter(server.getOutputStream(),true);
     }catch(Exception e){System.out.print("error");}
     out.println(str);
     out.flush();
     }
    public void coutObject(accustom person){
     try{
     try{
     outObject=new ObjectOutputStream(server.getOutputStream()); 
     outObject.writeObject(person);
        }
        catch(Exception e){System.out.print("出错!");}     
            //outObject.flush();   
            outObject.close();
            }
            catch(Exception e){System.out.print("传对象出错!");}        }
    public accustom cinObject(){
     accustom person=null;
     try{System.out.print("传对象出错!");
     while(true){
     InputStream inl=server.getInputStream();
     System.out.print("传对象出错!");
     inObject=new ObjectInputStream(inl);
     if(inObject!=null)
        break;
     }
         System.out.print("传对象出错!");
         person=(accustom)inObject.readObject();
         System.out.print("传对象出错!");
         }
         catch(Exception e){System.out.print("传对象");}
     return person;
}
}
向服务器传对象的程序为:
package qq;
import qq.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class zhuce extends JFrame implements ActionListener{
private JButton zhuce,cancel;
private JTextField nicheng,age,account;
private JPasswordField code,reCode;
private JRadioButton sexM,sexF;
private ButtonGroup sex;

public zhuce(){
super("注册新用户");
this.setSize(660,495);
this.setLocation(200,100);
this.setLayout(null);
this.setResizable(false);

Font f = new Font("新宋体",Font.PLAIN,14);

ImageIcon bt=new ImageIcon("land/7.jpg");//设置背景图片
ImagePanel image=new ImagePanel(bt);
this.add(image);
image.setBounds(0,0,650,460);
image.setLayout(null);

nicheng=new JTextField();
image.add(nicheng);
nicheng.setBounds(225,45,230,20);

age=new JTextField();
image.add(age);
age.setBounds(225,93,230,20);

sexM=new JRadioButton("男",true);
sexF=new JRadioButton("女");
sex=new ButtonGroup();
sex.add(sexM);
sex.add(sexF);
image.add(sexM);
sexM.setBounds(225,122,39,20);
image.add(sexF);
sexF.setBounds(264,122,39,20);

account=new JTextField();
image.add(account);
account.setBounds(225,203,230,20);

code=new JPasswordField();
image.add(code);
code.setBounds(225,247,230,20);

reCode=new JPasswordField();
image.add(reCode);
reCode.setBounds(225,310,230,20);

zhuce=new JButton("提交");
image.add(zhuce);
zhuce.setBounds(160,400,100,20);
zhuce.setBackground(Color.white);
zhuce.setFont(f);
zhuce.addActionListener(this);

cancel=new JButton("取消");
image.add(cancel);
cancel.setBounds(360,400,100,20);
cancel.setBackground(Color.white);
cancel.setFont(f);
cancel.addActionListener(this);

this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="提交"){
if(code.getText().equals(reCode.getText())){
accustom person=new accustom();
    person.setZhangHao(account.getText());
    if(sexM.isSelected()){
     person.setSex("男");
    } 
    else
        person.setSex("女");
    person.setCode(code.getText());
    person.setNiCheng(nicheng.getText());
    landSocket serve=new landSocket();
    serve.cout("2");
    serve.coutObject(person);
    System.out.print(person.getSex());
    }
else{
JOptionPane.showMessageDialog(null,"两次密码输入不一致,请重新输入!");
code.setText("");
reCode.setText("");
}
}
}
public static void main(String args[]){
new zhuce();
}
}

解决方案 »

  1.   

    run里面没有无限循环即:while(true){......}
      

  2.   

    所有文件都在这儿了,大家可以在机子上编译一下:
    package qq;
    import qq.*;
    import java.io.*;
    import java.sql.*;public class accustom implements Serializable{
    private String sex,nicheng,zhanghao,code;
    public void setSex(String sex){
    this.sex=sex;
    }
    public String getSex(){
    return this.sex;
    }
    public void setNiCheng(String nicheng){
    this.nicheng=nicheng;
    }
    public String getNiCheng(){
    return this.nicheng;
    }
    public void setZhangHao(String zhanghao){
    this.zhanghao=zhanghao;
    }
    public String getZhangHao(){
    return this.zhanghao;
    }
    public void setCode(String code){
    this.code=code;
    }
    public String getCode(){
    return this.code;
    }
    public void set(ResultSet rs){
    try{
    while(rs.next()){
      this.nicheng=rs.getString("nicheng");
      this.code=rs.getString("code");
                  }
                }
                catch(Exception e){}
    }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////
    package qq;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.*;
    import java.sql.*;
    public class DbOperation
    {   public Connection conn;
    public Statement stmt;
    public ResultSet rs;
    public String dbname;
    public DbOperation(String name)throws SQLException,ClassNotFoundException
    { dbname=name;
    try 

    {
    String dburl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+dbname+".mdb";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       conn=DriverManager.getConnection(dburl);
       }
       catch(SQLException ex)
    {
    JOptionPane.showMessageDialog(null,"连接数据库失败!ooo\n" );
    }

    } public ResultSet executeQuery(String sql)throws SQLException,ClassNotFoundException
    {

    stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    try{
    rs=stmt.executeQuery(sql);
    }
    catch(Exception e){System.out.print("bbb");}
    return rs;
    }
    public void executeUpdate(String sql)
    {
    try
    {

    stmt = conn.createStatement();
    stmt.executeUpdate(sql);
    }
    catch(SQLException ex)
    {
    JOptionPane.showMessageDialog(null,ex.getMessage());

    }
    }
    public void executeUpdateI(String sql)
    {
    try
    {

    stmt = conn.createStatement();
    stmt.executeUpdate(sql);
    JOptionPane.showMessageDialog(null,"注册成功!");
    }
    catch(SQLException ex)
    {
    //JOptionPane.showMessageDialog(null,ex.getMessage());
    JOptionPane.showMessageDialog(null,"用户名出现重复,或必填字段为空,请重新输入");
    }
    }
    public void close()throws SQLException,ClassNotFoundException
    {
    if (rs!=null) rs.close();
    stmt.close();
    conn.close();
    }

       
    }
     
     
    ///////////////////////////////////////////////////////////////////////////////////////////////
    package qq;
    import qq.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class landFrame extends JFrame implements ActionListener{
    private JPanel chief;
    private JButton zhuce,land,cancel;
    private JTextField account;
    private JLabel account1,code1;
    private JPasswordField code;

    public landFrame(){
    super("QQ用户登陆");
    this.setLocation(320,270);
    this.setSize(327,250);
    this.setLayout(null);
    this.setResizable(false);

    ImageIcon bt=new ImageIcon("land/3.jpg");
    ImagePanel image=new ImagePanel(bt);
    this.add(image);
    image.setBounds(0,0,320,270);
    image.setLayout(null);
    ImageIcon landImage=new ImageIcon("land/4.jpg");

    Font f = new Font("新宋体",Font.PLAIN,14);

    account=new JTextField("<请在这儿输入号码>");
    image.add(account);
    account.setBounds(95,75,150,19);
    account.setFont(f);

    code=new JPasswordField();
    image.add(code);
    code.setBounds(95,112,150,19);
    code.setFont(f);

    zhuce=new JButton("申请号码");
    zhuce.setFont(new Font("新宋体",Font.PLAIN,12));
    image.add(zhuce);
    zhuce.setBounds(100,182,85,20);
    zhuce.addActionListener(this);

    land=new JButton("",landImage);//登陆按钮设置
    image.add(land);
    land.setFont(f);
    land.setBounds(236,182,76,20);
    land.addActionListener(this);

    cancel=new JButton("取消");
    //image.add(cancel);
    cancel.setBounds(180,170,110,20);
    cancel.setFont(f);
    cancel.addActionListener(this);

    this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e){
    if(e.getActionCommand()==""){
    System.out.print("aaa");
    landSocket serve=new landSocket();
    String str=account.getText();
    serve.cout("1");
    serve.cout(str);
    System.out.print("aaaaaaa");
    str="";
        str=serve.cin();
    if(code.getText().equals(str)){
    new mainFrame();
    }
    else
        JOptionPane.showMessageDialog(null,"密码输入错误!请重新输入!");

    //land.setEnabled(false);

    }
    if(e.getActionCommand()=="退出"){
    System.exit(0);
    }
    if(e.getActionCommand()=="申请号码")
        new zhuce();
    }
        
        public static void main(String args[]){
    new landFrame();
    }
    }
    //////////////////////////////////////////////////////////////////
    class ImagePanel extends JPanel{
    public ImagePanel(ImageIcon imageIcon){  
              setOpaque(false);
              _img=imageIcon.getImage();
              setPreferredSize(new Dimension(imageIcon.getIconWidth(),imageIcon.getIconHeight()));
              }    protected void paintComponent(Graphics g){
         super.paintComponent(g);
            g.drawImage(_img,0,0,this);
            }
                    
        public void setImage(ImageIcon img){
         if(img!=null){
         this._img=img.getImage();
                }
              }
        private   Image   _img;
       
       }
    //////////////////////////////////////////////////////////////////////
    package qq;
    import qq.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class mainFrame extends JFrame{
    private JButton cancel;

    public mainFrame(){
    super("QQ聊天系统");
    this.setBackground(Color.BLUE);
    this.setLocation(780,80);
    this.setSize(213,530);
    this.setLayout(null);

    ImageIcon image=new ImageIcon("land/5.jpg");
    ImagePanel image1=new ImagePanel(image);
    this.add(image1);
    image1.setBounds(0,0,205,497);
    image1.setLayout(null);

    ImageIcon cancel1=new ImageIcon("land/6.jpg");
    cancel=new JButton("",cancel1);
    image1.add(cancel);
    cancel.setBounds(57,275,90,24);

    this.setVisible(true);
    }
    public static void main(String args[]){
    new mainFrame();
    }
    }
    //////////////////////////////////////////////////////////////////////////////////////
    package qq;
    import qq.*;
    import java.sql.*;
    import javax.swing.*;
    public class query
    {DbOperation db;
     ResultSet rs;
     public String query(String sql,String name)throws SQLException,ClassNotFoundException
     {db=new DbOperation(name);
      try{
       rs=db.executeQuery(sql);
       }
       catch(Exception e){System.out.print("aaa");}
       String str="";
       //String str=rs.getString("nicheng");
      while(rs.next())
      {str=rs.getString("code");
      }
      db.close();
      return str;
     }
    }
      

  3.   

    程序在创建对象输入流时就卡住了,执行不下去了。
    if(str.equals("2")){ 
    landSocket serve=new landSocket(); 
    accustom person=serve.cinObject(); 
    System.out.println(str); 
    System.out.print(person.getSex()); 

    这段代码中调用的cinObject(),
    就是这个方法
    public accustom cinObject(){ 
         accustom person=null; 
         try{System.out.print("传对象出错!"); 
         while(true){ 
         InputStream inl=server.getInputStream(); 
         System.out.print("传对象出错!"); 
         inObject=new ObjectInputStream(inl); 
         if(inObject!=null) 
            break; 
         } 
             System.out.print("传对象出错!"); 
             person=(accustom)inObject.readObject(); 
             System.out.print("传对象出错!"); 
             } 
             catch(Exception e){System.out.print("传对象");} 
         return person; 

    其中
    inObject=new ObjectInputStream(inl); 这一句话卡住了
      

  4.   

    程序太多我没看完,但有一点不知你注意到没,就是一个程序中对输入流的关闭好像只能进行一次,因为在一个程序中输入流只有一个System.in!!!
    你将输入流的关闭一直到不再使用输入流时关闭.看能不能行
      

  5.   

    看到这么长的就愁了 帮你排个版
    inObject=new ObjectInputStream(inl); 
    inl是已经得到的输入流. 
    服务器程序如下: 
    package qq; 
    import qq.*; 
    import java.io.*; 
    import java.net.*; 
    import java.util.*; 
    import java.sql.*; 
    import javax.swing.*; public class server extends Thread{ 
    static ServerSocket serve; 
    BufferedReader in; 
    PrintWriter out; 
    Socket client; 
    String str;//用来存储执行哪种命令  
        String str1;//用来存储账号 
         
    public server(Socket client){ 
    this.client=client; 
    } public void run(){ 
    String result=""; 
    try{//while(true){ 
    in=new BufferedReader(new InputStreamReader(client.getInputStream())); 
    out=new PrintWriter(client.getOutputStream(),true); 
    str=in.readLine(); System.out.println(str); if(str.equals("1")){ 
    str1=in.readLine(); 
    if(str!=null){ 
    System.out.println(str); 
        String sql="SELECT code FROM accustom WHERE account='"+str1+"'"; 
        System.out.println(sql); 
          try{ 
        query c=new query(); 
        result=c.query(sql,"date"); 
        out.println(result);//返回查询结果,只是把print改成了println,它咋就行了??? 
        out.flush(); 
          System.out.print(result); 
        out.close(); 
        in.close(); 
        } 
        catch(Exception e) {JOptionPane.showMessageDialog(null,e.getMessage());}  
       //} 
              } 
            } 
    if(str.equals("2")){ 
    landSocket serve=new landSocket(); 
    accustom person=serve.cinObject(); 
    System.out.println(str); 
    System.out.print(person.getSex()); 
    }     } 
        catch(Exception e){JOptionPane.showMessageDialog(null,e.getMessage());} 

    public static void main(String args[]){ 
    try{ 
        serve=new ServerSocket(2008); 
       } 
    catch(Exception e){System.out.println("error");} 
    while(true){ 
      try{ 
       server s=new server(serve.accept()); 
        s.start(); 
         } 
      catch(Exception e){System.out.println("error1");} 
       } 
        


    其中调用的方法均来自: 
    package qq; 
    import qq.*; 
    import java.io.*; 
    import java.net.*; 
    import javax.swing.*; public class landSocket{ 
    Socket server; 
    public BufferedReader in; 
    public PrintWriter out; 
    ObjectOutputStream outObject; 
    ObjectInputStream inObject;     public landSocket(){ 
         try{ 
         server=new Socket("localhost",2008); 
         
            } 
         catch(Exception e){System.out.print("error");} 
         }  
        public String cin(){ 
         String str=""; 
         try{ 
         in=new BufferedReader(new InputStreamReader(server.getInputStream())); 
         str=in.readLine(); 
         in.close(); 
         } 
         catch(Exception e){JOptionPane.showMessageDialog(null,e.getMessage());} 
         return str; 
         } 
        public void cout(String str){ 
         try{ 
         out=new PrintWriter(server.getOutputStream(),true); 
         }catch(Exception e){System.out.print("error");} 
         out.println(str); 
         out.flush(); 
         } 
        public void coutObject(accustom person){ 
         try{ 
         try{ 
         outObject=new ObjectOutputStream(server.getOutputStream());  
         outObject.writeObject(person); 
            } 
            catch(Exception e){System.out.print("出错!");}      
                //outObject.flush();    
                outObject.close(); 
                } 
                catch(Exception e){System.out.print("传对象出错!");}         } 
        public accustom cinObject(){ 
         accustom person=null; 
         try{System.out.print("传对象出错!"); 
         while(true){ 
         InputStream inl=server.getInputStream(); 
         System.out.print("传对象出错!"); 
         inObject=new ObjectInputStream(inl); 
         if(inObject!=null) 
            break; 
         } 
             System.out.print("传对象出错!"); 
             person=(accustom)inObject.readObject(); 
             System.out.print("传对象出错!"); 
             } 
             catch(Exception e){System.out.print("传对象");} 
         return person; 


    向服务器传对象的程序为: 
    package qq; 
    import qq.*; 
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; public class zhuce extends JFrame implements ActionListener{ 
    private JButton zhuce,cancel; 
    private JTextField nicheng,age,account; 
    private JPasswordField code,reCode; 
    private JRadioButton sexM,sexF; 
    private ButtonGroup sex; public zhuce(){ 
    super("注册新用户"); 
    this.setSize(660,495); 
    this.setLocation(200,100); 
    this.setLayout(null); 
    this.setResizable(false); Font f = new Font("新宋体",Font.PLAIN,14); ImageIcon bt=new ImageIcon("land/7.jpg");//设置背景图片 
    ImagePanel image=new ImagePanel(bt); 
    this.add(image); 
    image.setBounds(0,0,650,460); 
    image.setLayout(null); nicheng=new JTextField(); 
    image.add(nicheng); 
    nicheng.setBounds(225,45,230,20); age=new JTextField(); 
    image.add(age); 
    age.setBounds(225,93,230,20); sexM=new JRadioButton("男",true); 
    sexF=new JRadioButton("女"); 
    sex=new ButtonGroup(); 
    sex.add(sexM); 
    sex.add(sexF); 
    image.add(sexM); 
    sexM.setBounds(225,122,39,20); 
    image.add(sexF); 
    sexF.setBounds(264,122,39,20); account=new JTextField(); 
    image.add(account); 
    account.setBounds(225,203,230,20); code=new JPasswordField(); 
    image.add(code); 
    code.setBounds(225,247,230,20); reCode=new JPasswordField(); 
    image.add(reCode); 
    reCode.setBounds(225,310,230,20); zhuce=new JButton("提交"); 
    image.add(zhuce); 
    zhuce.setBounds(160,400,100,20); 
    zhuce.setBackground(Color.white); 
    zhuce.setFont(f); 
    zhuce.addActionListener(this); cancel=new JButton("取消"); 
    image.add(cancel); 
    cancel.setBounds(360,400,100,20); 
    cancel.setBackground(Color.white); 
    cancel.setFont(f); 
    cancel.addActionListener(this); this.setVisible(true); 

    public void actionPerformed(ActionEvent e){ 
    if(e.getActionCommand()=="提交"){ 
    if(code.getText().equals(reCode.getText())){ 
    accustom person=new accustom(); 
        person.setZhangHao(account.getText()); 
        if(sexM.isSelected()){ 
         person.setSex("男"); 
        }  
        else 
            person.setSex("女"); 
        person.setCode(code.getText()); 
        person.setNiCheng(nicheng.getText()); 
        landSocket serve=new landSocket(); 
        serve.cout("2"); 
        serve.coutObject(person); 
        System.out.print(person.getSex()); 
        } 
    else{ 
    JOptionPane.showMessageDialog(null,"两次密码输入不一致,请重新输入!"); 
    code.setText(""); 
    reCode.setText(""); 



    public static void main(String args[]){ 
    new zhuce(); 

    }