尝试从服务器获取表时,得到:
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();
}
}
}
}
和server_function类;
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;
}
}
顺便说一下,我可以发送带有objectoutputstream的字符串数组,所以我猜问题仅在于defaulttablemodel。
最佳答案
我在您的代码中看到:
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。