在尝试从服务器获取表时,我得到:
java.io.StreamCorruptedException:类型代码无效:00
和
线程中的异常“AWT-EventQueue-0”java.lang.IllegalArgumentException:无法设置null TableModel
为什么会发生这种情况,如何解决?
这是我编写函数的客户端类:
eigs
这是我的服务器类;
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);
}
}
和server_function类;
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();
}
}
}
}
顺便说一句,我可以使用objectoutputstream发送例如字符串数组,所以我猜问题只有defaulttablemodel。
答案 0 :(得分: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。