2015-05-17 172 views
0

虽然试图从服务器获取一个表,我得到:我无法从服务器发送对象到客户端

java.io.StreamCorruptedException:无效类型代码:00

而且

异常在线程 “AWT-EventQueue的 - 0” java.lang.IllegalArgumentException异常:不能设置一个空的TableModel

为什么会发生这种情况,我该如何解决?

这里是我的客户端类,我写功能:

public class Client { 

    private Socket client; 
    private DataInputStream dis; 
    private DataOutputStream dos; 
    private ObjectInputStream ois; 
    private ObjectOutputStream oos; 



    private int id; 
    private String name; 
    private String surname; 



    public void setClientInfo(int id, String name, String surname) { 
     this.id = id; 
     this.name = name; 
     this.surname = surname; 
    } 

    public int getId() { 
     return id; 
    } 

    public void connectServer() { 
     try { 

      client = new Socket("localhost", 3000); 
      dis = new DataInputStream(client.getInputStream()); 
      dos = new DataOutputStream(client.getOutputStream()); 

      oos = new ObjectOutputStream(client.getOutputStream()); 
      ois = new ObjectInputStream(client.getInputStream()); 



      System.out.println("I/O streams for client created"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void closeConnection() { 
     try { 
      sendUTFDataToServer("EXIT"); 
      dis.close(); 
      dos.close(); 
      client.close(); 
      System.out.println("CLIENT END"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public boolean sendUTFDataToServer(String msg) { 

     if (client != null) { 
      try { 
       dos.writeUTF(msg); 
       return true; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return false; 
    } 

    public boolean sendIntDataToServer(int num) { 

     if (client != null) { 
      try { 
       dos.writeInt(num); 
       return true; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return false; 
    } 

    public boolean sendDoubleDataToServer(double num) { 

     if (client != null) { 
      try { 
       dos.writeDouble(num); 
       return true; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return false; 
    } 

    public String getUTFDataFromServer() { 
     String res = ""; 
     if (client != null) { 
      try { 
       res = dis.readUTF(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return res; 
    } 

    public int getIntDataFromServer() { 
     int res = 0; 
     if (client != null) { 
      try { 
       res = dis.readInt(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return res; 
    } 

    public double getDoubleDataFromServer() { 
     double res = 0; 
     if (client != null) { 
      try { 
       res = dis.readDouble(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return res; 
    } 

    public void sendObjectToServer(Object obj) { 

     if (client != null) { 
      try { 
       oos.writeObject(obj); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 

    public Object getObjectFromServer() { 
     Object obj = null; 
     if (client != null) { 
      try { 

       obj = (DefaultTableModel) ois.readObject(); 

      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return obj; 
    } 
    public DefaultTableModel showBook(){ 
     DefaultTableModel dtm=null; 
     try { 
      dos.writeUTF("showbooks"); 

      dtm = (DefaultTableModel) ois.readObject(); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return dtm; 
    } 
    public String addBook(int id,String author,String name,int library_id) 
    { 
     String mes=""; 
     try { 
      dos.writeUTF("ADD"); 
      dos.writeInt(id); 
      dos.writeUTF(author); 
      dos.writeUTF(name); 
      dos.writeInt(library_id); 

      mes =dis.readUTF(); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    return mes; 
    } 



    public static void main(String[] args) { 
     Client client=new Client(); 
     client.connectServer(); 
     Login_Frame frame = new Login_Frame(client); 
     frame.setVisible(true); 
    } 

} 

这里是我的服务器类;

public class Server { 

    private int port = 3000; 
    private ServerSocket server; 
    public static int count = 0; 
    public static int activeClientCount = 0; 

    public Server() { 

     try { 
      server = new ServerSocket(port); 
      System.out.println("Server Started"); 

      DB.initializeDB(); 
      System.out.println("Database Connection established"); 

      while (true) { 
       System.out 
         .println("Server: Server is waiting for client connetion"); 
       Socket clientSocket = server.accept(); 
       count++; 
       activeClientCount++; 

       System.out.println("Server: Client connection is provided " 
         + count); 
       System.out.println("Active Client " + activeClientCount); 

       MyClientThread mt = new MyClientThread(clientSocket); 
       mt.start(); 
      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     System.out.println("END SERVER"); 

    } 

    public static void main(String[] args) { 
     Server server = new Server(); 
    } 

    class MyClientThread extends Thread { 


     DataInputStream dis; 
     DataOutputStream dos; 

     ObjectInputStream ois; 
     ObjectOutputStream oos; 

     Socket clientsocket; 
     public MyClientThread(Socket csocket) { 
      this.clientsocket = csocket; 
      System.out.println("Client connected"); 
      try { 
       dis = new DataInputStream(clientsocket.getInputStream()); 
       dos = new DataOutputStream(clientsocket.getOutputStream()); 

       ois = new ObjectInputStream(clientsocket.getInputStream()); 
       oos = new ObjectOutputStream(clientsocket.getOutputStream()); 



       System.out.println("I/O streams are created"); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void run() { 

      try { 
       while (true && clientsocket != null) { 

        System.out.println("waiting for opp command"); 
        String opp = dis.readUTF(); 

        String res = ""; 

        if (opp.equals("LOGIN")) { 

         String un = dis.readUTF(); 
         String pw = dis.readUTF(); 
         String type=dis.readUTF(); 

         ResultSet rs = ServerSys.login(un, pw,type); 


         if(rs.next()==false) 
         { 
          dos.writeUTF("nouser"); 

         } 
         else 
         { 
           dos.writeUTF("YES"); 
           dos.writeInt(rs.getInt(1)); 
           dos.writeUTF(rs.getString(2)); 
           dos.writeUTF(rs.getString(3));  

         } 

         } 
        else if(opp.equals("showbooks")) 
        { 
         System.out.println("book'a girdi"); 
         dos.writeUTF("YES"); 
         dos.writeUTF("HEllo"); 
         ResultSet rs=DB.executeQ("select * from book"); 

         DefaultTableModel dtm= DB.showTables(rs); 
         System.out.println(dtm.getValueAt(1,1)); 
         oos.writeObject(dtm); 


        } 


        } 


      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

and server_function class;

public class ServerSys { 
    public static ResultSet login(String un, String pw,String type) { 
     String sql=""; 
     if(type.equalsIgnoreCase("client")) 
     { 
      sql = "select * from client where clientName ='" + un 
        + "' and clientPass='" + pw + "'"; 

     } 
     else 
     { 
      sql = "select * from admin where adminName ='" + un 
        + "' and adminPassword='" + pw + "'"; 
     } 


     ResultSet rs = DB.executeQ(sql); 
     return rs; 

    } 

    public static void insertBook(int id,String bookauthor,String name,int libraryid) { 
     String sql = "insert into book values(" + id + ",'" + bookauthor + "',"+libraryid+ 
       ")"; 
     try { 
      DB.executeQ(sql); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

       } 

    public static DefaultTableModel showTable(String sql){ 
     DefaultTableModel dtm = new DefaultTableModel();   
     ResultSet rs; 
     try { 
      rs = DB.executeQ(sql); 

      ResultSetMetaData rsmd= (ResultSetMetaData) rs.getMetaData(); 
      Vector header= new Vector(); 
      Vector datarows = new Vector(); 

      int columnumber = rsmd.getColumnCount(); 
      for(int i=1; i<columnumber; i++){ 
       header.add(rsmd.getColumnName(i)); 
      } 

      while(rs.next()){ 
       Vector row=new Vector(); 
       for(int i=1; i<columnumber; i++){ 
        row.add(rs.getObject(i)); 
       }    
       datarows.add(row); 
      } 
      dtm.setDataVector(datarows, header); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return dtm; 

    } 

} 

通过我,所以我想的问题是只用的DefaultTableModel的ObjectOutputStream例如字符串数组发送的方式。

+0

'尝试从服务器获取表格时,我得到:'。那么代码是造成这种情况? – greenapps

回答

0

我看到你的代码:

 dis = new DataInputStream(client.getInputStream()); 
     dos = new DataOutputStream(client.getOutputStream()); 

     oos = new ObjectOutputStream(client.getOutputStream()); 
     ois = new ObjectInputStream(client.getInputStream()); 

我不认为你应该做的是:你有两个不同的高度流写入或在同一水平低流中读取。取决于他们如何缓冲数据,这将以灾难结束。只需使用ObjectOutputStream,因为ObjectOutputStream实现了DataOutput,所以您也可以从中写入原语。所以只需摆脱dis和dos。

+0

任何改变这样做? –

相关问题