我有一个Client_Sock应该将对象客户端发送到Server_Sock。 我正在使用Netbeans,在两个项目中,Client_Sock和Server_Sock,我添加了Client类。
Server_Sock
package com.app;
@SpringBootApplication // contains @EnableAutoConfiguration @ComponentScan @Configuration
public class App {
@Autowired
private Service service;
public static void main(String[] args) {
final SpringApplication app = new SpringApplication(App.class);
//app.setShowBanner(false);
app.run();
}
@PostConstruct
public void foo() {
System.out.println("Instantiated service name = " + service.serviceName);
}
}
Client_Sock
class Connection extends Thread {
static C Ob;
Socket cs;
Connection(Socket cs) throws Exception {
this.cs = cs;
this.start();
}
public void run() {
try {
InputStream is = cs.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
Client cl = (Client)ois.readObject();
Ob.Write(cl);
cs.close(); is.close(); ois.close();
}
catch(IOException e) {e.printStackTrace(); }
catch(ClassNotFoundException e) {e.printStackTrace(); }
}
}
客户端
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("connect")){
try{
//add the data in client
Client cl = new Client();
cl._age=Integer.parseInt(age_.getText());
cl._id=Integer.parseInt(id_.getText());
cl._name=name_.getText();
//check if data is good
if(cl._age>18 && !cl._name.equals("") && cl._id>1000 && cl._id<9999){
Socket cs = new Socket("localhost",5555);
OutputStream os = cs.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(cl);
os.close(); oos.close(); cs.close();
this.setVisible(false);
this.dispose();
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
当我运行项目时,我在Client_Sock上没有错误,但是当服务器应该收到客户端对象时,我收到此错误:
import java.io.Serializable;
class Client implements Serializable{
public String _name;
public Integer _age;
public Integer _id;
Client(){
}
Client(String myName, Integer myAge, Integer myId){
_name = myName;
_age = myAge;
_id = myId;
}
}
我已经阅读了一些关于套接字传输和序列化的文章,但我找不到我收到此错误的原因。你能帮我吗?谢谢。