将填充的对象发送到服务器并接收消息

时间:2017-03-21 19:44:53

标签: java sockets objectoutputstream

我正在尝试将填充了学生详细信息的学生对象发送到特定服务器,然后阅读从服务器发回的消息。

主要课程:

public class Lab6 {
    Socket myClient;
    ObjectOutputStream outputStream;

    public void socket() {
        try {
            myClient = new Socket("217.73.66.75", 7879);
        }
        catch (UnknownHostException e) {
            System.out.println(e);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }

    public void objectWriter() {
        try {
           myClient = new Socket("217.73.66.75", 7879);
           outputStream = new ObjectOutputStream(myClient.getOutputStream());
           Student student = new Student(22, "Dave Smith", "130017639", "dave.smith@city.ac.uk");
           System.out.println("Student Details:" + student.toString());
           outputStream.writeObject(student);

        }
        catch (UnknownHostException e) {
            System.out.println(e);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }

    /**
     *
     */
    public void objectReader() {
        try {
            ObjectInputStream inputStream = new ObjectInputStream(myClient.getInputStream());
            Student returnMessage = (Student) inputStream.readObject();
            System.out.println("return Message is=" + returnMessage); 
            myClient.close();
            inputStream.close();
        }
        catch (IOException e) {
            System.out.println(e);
        }
        catch (ClassNotFoundException ex) {
            System.out.println("ERR: Cannot perform input. Class not found." + ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Lab6 ss;
        ss = new Lab6();
        ss.socket();
        ss.objectWriter();
        ss.objectReader(); 
    }
}

学生班:

import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = -1848148348931789644L; 

    private String name;
    private int age;
    private String studentID;
    private String email;
    public String gender = "na";
    public static int instances = 0;

    // Getters
    public int getAge(){
        return this.age;
    }
    public String getName(){
        return this.name;
    }
    public String getStudentID(){
        return this.studentID;
    }
    public String getEmail(){
        return this.email;
    }

    // Setters
    public void setAge(int age){
        this.age = age;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setStudentID(String studentID){
        this.studentID = studentID;
    }
    public void setEmail(String email){
        this.email = email;
    }

    /**
     * Default constructor. Populates age and gender with defaults
     */
    public Student(){
        this.age = 18;
        this.name = "Not Set";
        this.studentID = "Not Set";
        this.email = "Not Set";
    }

    /** 
     * Constructor with parameters 
     * @param age integer
     * @param name String with the name
     * @param studentID String with the studentID
     * @param email String with the email
    */
    public Student (int age, String name, String studentID, String email){
        this.age = age;
        this.name = name;
        this.studentID = studentID;
        this.email = email;
    }
    /** 
     * Gender constructor
     * @param gender 
     */
    public Student(String gender){
        this(); // Must be the first line!
        this.gender = gender;

    }

    protected void finalize() throws Throwable{
        //do finalization here
        super.finalize(); //not necessary if extending Object.
    } 

    /*************************************************************************
     * My Methods:
     * 
     *************************************************************************/

    public String toString (){
        return "Student ID: " + studentID + "Name: " + this.name + " Age: " 
                + this.age + " Gender: " + this.gender + "Email: " + email;
    }
}

问题是它所做的只是打印出控制台上的学生详细信息,而程序只是说跑步。请帮我找一下我做错了什么。

1 个答案:

答案 0 :(得分:1)

您的代码缺少服务器部分:您的代码正在打开"客户端"插座两次;一次写信给它;然后尝试从这样的套接字中读取内容。

但这不是客户端/服务器的工作方式!

服务器端需要打开 ServerSocket ;并等待传入​​的客户端连接到它。

然后客户端打开到该服务器的套接字连接;发送数据,服务器获取数据并执行某些操作。

有关该主题的第一个简单教程,请参阅here