gpt4 book ai didi

java - 我无法将对象从服务器发送到客户端

转载 作者:行者123 更新时间:2023-11-29 22:25:36 24 4
gpt4 key购买 nike

在尝试从服务器获取表格时,我得到:

java.io.StreamCorruptedException: invalid type code: 00

还有

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot set a null 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 就可以了。

关于java - 我无法将对象从服务器发送到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30286408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com