Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA-使用套接字和线程接收对象不工作_Java_Multithreading_Javafx_Socket.io_Client Server - Fatal编程技术网

JAVA-使用套接字和线程接收对象不工作

JAVA-使用套接字和线程接收对象不工作,java,multithreading,javafx,socket.io,client-server,Java,Multithreading,Javafx,Socket.io,Client Server,我正在尝试从服务器获取对象,但它不起作用 服务器的相关部分(通过调试,我发现他确实发送了正确的对象): 根据我在这里读到的答案,我尝试将线程分为两个(一个输入,另一个输出在服务器和客户机中),但没有帮助。 他还试图改变输入和输出的顺序,但没有成功 没有错误 客户端无法获取对象 这里有什么问题 编辑。学生班级: public class Student implements Externalizable { private final SimpleIntegerProperty I

我正在尝试从服务器获取对象,但它不起作用

服务器的相关部分(通过调试,我发现他确实发送了正确的对象):

根据我在这里读到的答案,我尝试将线程分为两个(一个输入,另一个输出在服务器和客户机中),但没有帮助。 他还试图改变输入和输出的顺序,但没有成功

  • 没有错误
  • 客户端无法获取对象
这里有什么问题

编辑。学生班级:

public class Student implements  Externalizable
{ 
    private final SimpleIntegerProperty ID;
    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty address;
    private final SimpleObjectProperty<Date> birthDate;
    private final SimpleStringProperty department;
    private final SimpleIntegerProperty pointsAmount;
    private final SimpleObjectProperty<Date> startStudyingDate;
    private final SimpleIntegerProperty failedAmount;
    private final SimpleDoubleProperty average;
    private final SimpleIntegerProperty lavelByGrade;
    private final SimpleStringProperty pic;

    public Student(int ID, String firstName, String lastName, String address,
            Date  birthDate, String department,
            int pointsAmount, Date startStudyingDate, int failedAmount, 
            double average, int  lavelByGrade, String pic){

        this.ID= new SimpleIntegerProperty(ID);
        this.firstName= new SimpleStringProperty(firstName);
        this.lastName= new SimpleStringProperty(lastName);
        this.address= new SimpleStringProperty(address);
        this.birthDate= new SimpleObjectProperty<Date>(birthDate);
        this.department= new SimpleStringProperty(department);
        this.pointsAmount= new SimpleIntegerProperty(pointsAmount);
        this.startStudyingDate= new  SimpleObjectProperty<Date>(startStudyingDate);
        this.failedAmount= new SimpleIntegerProperty(failedAmount);
        this.average= new SimpleDoubleProperty(average);
        this.lavelByGrade= new SimpleIntegerProperty(lavelByGrade);
        this.pic = new SimpleStringProperty(pic);
    }

    public int getID() {
        return ID.get();
    }
    public void setID(int ID) {
        this.ID.set(ID);
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }

    public String getAddress() {
        return address.get();
    }

    public void setAddress(String address) {
        this.address.set(address);
    }

    public Date getBirthDate() {
        return birthDate.get();
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate.set(birthDate);
    }

    public String getDepartment() {
        return department.get();
    }

    public void setDepartment(String department) {
        this.department.set(department);
    }

    public int getPointsAmount() {
        return pointsAmount.get();
    }

    public void setPointsAmount(int pointsAmount) {
        this.pointsAmount.set(pointsAmount);
    }

    public Date getStartStudyingDate() {
        return startStudyingDate.get();
    }

    public void setStartStudyingDate(Date startStudyingDate) {
        this.startStudyingDate.set(startStudyingDate);
    }

    public int getFailedAmount() {
        return failedAmount.get();
    }

    public void setFailedAmount(int failedAmount) {
        this.failedAmount.set(failedAmount);
    }

    public double getAverage() {
        return average.get();
    }

    public void setAverage(Double average) {
        this.average.set(average);
    }

    public int getLavelByGrade() {
        return lavelByGrade.get();
    }

    public void setLavelByGrade(int lavelByGrade) {
        this.lavelByGrade.set(lavelByGrade);
    }

    public String getPic() {
        return pic.get();
    }

    public void setPic(String pic) {
        this.pic.set(pic);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException,
            ClassNotFoundException {

        setID(in.readInt());
        setFirstName((String)in.readObject());
        setLastName((String)in.readObject());
        setAddress((String)in.readObject());
        setBirthDate((Date)in.readObject());
        setDepartment((String)in.readObject());
        setPointsAmount(in.readInt());
        setStartStudyingDate((Date)in.readObject());
        setFailedAmount(in.readInt());
        setAverage(in.readDouble());
        setLavelByGrade(in.readInt());
        setPic((String)in.readObject());
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {

        out.writeInt(getID());
        out.writeObject(getFirstName()); 
        out.writeObject(getLastName()); 
        out.writeObject(getAddress()); 
        out.writeObject(getBirthDate());
        out.writeObject(getDepartment());
        out.writeInt(getPointsAmount()); 
        out.writeObject(getStartStudyingDate()); 
        out.writeInt(getFailedAmount()); 
        out.writeDouble(getAverage()); 
        out.writeInt(getLavelByGrade());
        out.writeObject(getPic()); 

    }
}   
公共类学生实现可外部化
{ 
私有最终SimpleIntegerProperty ID;
私有最终SimpleStringProperty名字;
私有最终SimpleStringProperty姓氏;
私人最终SimpleStringProperty地址;
私有最终SimpleObject属性生日;
私人物业署;
私有最终SimpleIntegerProperty pointsAmount;
私有最终SimpleObject属性开始学习日期;
私有最终SimpleIntegerProperty失败装载;
私人最终简化不动产平均值;
lavelByGrade私有最终简单集成财产;
私人最终财产;
公立学生(int-ID、String-firstName、String-lastName、String-address、,
日期出生日期弦乐部,
int pointsAmount,Date STARTSTUDINGDATE,int failedAmount,
双倍平均值,整数lavelByGrade,字符串pic){
this.ID=新的SimpleIntegerProperty(ID);
this.firstName=新的SimpleStringProperty(firstName);
this.lastName=新的SimpleStringProperty(lastName);
this.address=新的SimpleStringProperty(地址);
this.birthDate=新的SimpleObject属性(birthDate);
this.department=新SimpleStringProperty(department);
this.pointsAmount=新的SimpleIntegerProperty(pointsAmount);
this.startstudingDate=新的SimpleObject属性(startstudingDate);
this.failedAmount=新的SimpleIntegerProperty(failedAmount);
this.average=新的SimpleDoubleProperty(平均值);
this.lavelByGrade=新的SimpleIntegerProperty(lavelByGrade);
this.pic=新的SimpleStringProperty(pic);
}
公共int getID(){
返回ID.get();
}
公共无效集合ID(内部ID){
此.ID.set(ID);
}
公共字符串getFirstName(){
返回firstName.get();
}
public void setFirstName(字符串firstName){
this.firstName.set(firstName);
}
公共字符串getLastName(){
返回lastName.get();
}
public void setLastName(字符串lastName){
this.lastName.set(lastName);
}
公共字符串getAddress(){
返回地址。get();
}
公共无效设置地址(字符串地址){
this.address.set(地址);
}
公共日期getBirthDate(){
返回生日。get();
}
公共无效设置出生日期(日期出生日期){
this.birthDate.set(birthDate);
}
公共部门(){
返回部门。get();
}
公共部门(字符串部门){
本.department.set(department);
}
public int getPointsAmount(){
返回点samount.get();
}
公共无效setPointsAmount(int pointsAmount){
this.pointsAmount.set(pointsAmount);
}
公共日期getStartStudyingDate(){
return startstudingDate.get();
}
公共无效设置开始学习日期(开始学习日期){
this.startstudingDate.set(startstudingDate);
}
public int getFailedAmount(){
返回failedAmount.get();
}
公共void setFailedAmount(int failedAmount){
this.failedAmount.set(failedAmount);
}
公共双平均值(){
返回average.get();
}
公众平均(双倍平均){
此.average.set(平均值);
}
公共int getLavelByGrade(){
返回lavelByGrade.get();
}
公共无效setLavelByGrade(int lavelByGrade){
this.lavelByGrade.set(lavelByGrade);
}
公共字符串getPic(){
返回pic.get();
}
公共void setPic(字符串pic){
此.pic.set(pic);
}
@凌驾
public void readExternal(ObjectInput in)引发IOException,
ClassNotFoundException{
setID(in.readInt());
setFirstName(.readObject()中的字符串);
setLastName(.readObject()中的字符串);
setAddress(.readObject()中的字符串);
setBirthDate(.readObject()中的(日期));
setDepartment(.readObject()中的字符串);
setPointsAmount(in.readInt());
setStartStudyingDate(.readObject()中的(日期));
setFailedAmount(in.readInt());
setAverage(in.readDouble());
setLavelByGrade(in.readInt());
setPic(.readObject()中的(字符串);
}
@凌驾
public void writeExternal(ObjectOutput out)引发IOException{
out.writeInt(getID());
writeObject(getFirstName());
writeObject(getLastName());
writeObject(getAddress());
writeObject(getBirthDate());
out.writeObject(getDepartment());
writeInt(getPointsAmount());
writeObject(getStartStudyingDate());
out.writeInt(getFailedAmount());
out.writeDouble(getAverage());
out.writeInt(getLavelByGrade());
writeObject(getPic());
}
}   

试试这篇文章,用下面的文本搜索部分,并且可以外部化:很长一段时间以来,我认为JavaFX属性缺乏对序列化的支持,这确实阻止了它们在任何服务器端容量中使用


JavaFX属性不可序列化。因此,如果您尝试序列化一个使用JavaFX属性作为其状态的对象,您将得到一个异常

你有几个选择。一种是不使用Java对象序列化,而是使用其他一些序列化技术,例如
    private void connectToServer(){
    try{
        // Create a socket to connect to the server
        socket = new Socket(host, 8000);
        // Create an input stream to receive Object from the server
        fromServer = new ObjectInputStream(socket.getInputStream());
        // Create an output stream to send data to the server
        toServer = new DataOutputStream(socket.getOutputStream());

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

private void sendAndGetFromServer(String sqlQuery){
    new Thread(() ->{
        try{

            System.out.println("a1");
            // Send sql query to server
            toServer.writeUTF(sqlQuery);
            //toServer.flush();
            // Get notification from the server

            Student[] rows = (Student[])fromServer.readObject();
            setRowsInTable(rows);
        }
        catch(SocketException ex){
            try{
                socket.close();
            } 
            catch (IOException e){ 
            }
        } 
        catch (Exception ex){
        }
    }).start();
public class Student implements  Externalizable
{ 
    private final SimpleIntegerProperty ID;
    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty address;
    private final SimpleObjectProperty<Date> birthDate;
    private final SimpleStringProperty department;
    private final SimpleIntegerProperty pointsAmount;
    private final SimpleObjectProperty<Date> startStudyingDate;
    private final SimpleIntegerProperty failedAmount;
    private final SimpleDoubleProperty average;
    private final SimpleIntegerProperty lavelByGrade;
    private final SimpleStringProperty pic;

    public Student(int ID, String firstName, String lastName, String address,
            Date  birthDate, String department,
            int pointsAmount, Date startStudyingDate, int failedAmount, 
            double average, int  lavelByGrade, String pic){

        this.ID= new SimpleIntegerProperty(ID);
        this.firstName= new SimpleStringProperty(firstName);
        this.lastName= new SimpleStringProperty(lastName);
        this.address= new SimpleStringProperty(address);
        this.birthDate= new SimpleObjectProperty<Date>(birthDate);
        this.department= new SimpleStringProperty(department);
        this.pointsAmount= new SimpleIntegerProperty(pointsAmount);
        this.startStudyingDate= new  SimpleObjectProperty<Date>(startStudyingDate);
        this.failedAmount= new SimpleIntegerProperty(failedAmount);
        this.average= new SimpleDoubleProperty(average);
        this.lavelByGrade= new SimpleIntegerProperty(lavelByGrade);
        this.pic = new SimpleStringProperty(pic);
    }

    public int getID() {
        return ID.get();
    }
    public void setID(int ID) {
        this.ID.set(ID);
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }

    public String getAddress() {
        return address.get();
    }

    public void setAddress(String address) {
        this.address.set(address);
    }

    public Date getBirthDate() {
        return birthDate.get();
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate.set(birthDate);
    }

    public String getDepartment() {
        return department.get();
    }

    public void setDepartment(String department) {
        this.department.set(department);
    }

    public int getPointsAmount() {
        return pointsAmount.get();
    }

    public void setPointsAmount(int pointsAmount) {
        this.pointsAmount.set(pointsAmount);
    }

    public Date getStartStudyingDate() {
        return startStudyingDate.get();
    }

    public void setStartStudyingDate(Date startStudyingDate) {
        this.startStudyingDate.set(startStudyingDate);
    }

    public int getFailedAmount() {
        return failedAmount.get();
    }

    public void setFailedAmount(int failedAmount) {
        this.failedAmount.set(failedAmount);
    }

    public double getAverage() {
        return average.get();
    }

    public void setAverage(Double average) {
        this.average.set(average);
    }

    public int getLavelByGrade() {
        return lavelByGrade.get();
    }

    public void setLavelByGrade(int lavelByGrade) {
        this.lavelByGrade.set(lavelByGrade);
    }

    public String getPic() {
        return pic.get();
    }

    public void setPic(String pic) {
        this.pic.set(pic);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException,
            ClassNotFoundException {

        setID(in.readInt());
        setFirstName((String)in.readObject());
        setLastName((String)in.readObject());
        setAddress((String)in.readObject());
        setBirthDate((Date)in.readObject());
        setDepartment((String)in.readObject());
        setPointsAmount(in.readInt());
        setStartStudyingDate((Date)in.readObject());
        setFailedAmount(in.readInt());
        setAverage(in.readDouble());
        setLavelByGrade(in.readInt());
        setPic((String)in.readObject());
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {

        out.writeInt(getID());
        out.writeObject(getFirstName()); 
        out.writeObject(getLastName()); 
        out.writeObject(getAddress()); 
        out.writeObject(getBirthDate());
        out.writeObject(getDepartment());
        out.writeInt(getPointsAmount()); 
        out.writeObject(getStartStudyingDate()); 
        out.writeInt(getFailedAmount()); 
        out.writeDouble(getAverage()); 
        out.writeInt(getLavelByGrade());
        out.writeObject(getPic()); 

    }
}   
import java.io.Externalizable ;
import java.io.IOException ;
import java.io.ObjectInput ;
import java.io.ObjectOutput ;

import javafx.beans.property.IntegerProperty ;
import javafx.beans.property.SimpleIntegerProperty ;
import javafx.beans.property.SimpleStringProperty ;
import javafx.beans.property.StringProperty ;

public class Person implements Externalizable {

    private final StringProperty name = new SimpleStringProperty();
    private final IntegerProperty id = new SimpleIntegerProperty();

    public StringProperty nameProperty() {
        return name ;
    }
    public final String getName() {
        return nameProperty().get();
    }
    public final void setName(String name) {
        nameProperty().set(name);
    }

    public IntegerProperty idProperty() {
        return id ;
    }
    public final int getId() {
        return idProperty().get();
    }
    public final void setId(int id) {
        idProperty().set(id);
    }

    // important: must have a no-arg constructor:
    public Person() { }

    public Person(int id, String name) {
        setId(id);
        setName(name);
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        // write id then name
        // note we write the contents of the properties, not the properties 
        // themselves, as the properties are not serializable:
        out.writeInt(getId());
        out.writeObject(getName());
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        // read data back in same order:
        setId(in.readInt());
        setName((String)in.readObject());
    }
}
ObjectOutputStream oos = ... ;
oos.writeObject(new Person(007, "James Bond"));