簡體   English   中英

將填充的對象發送到服務器並接收回消息

[英]Sending populated object to server and receiving message back

我試圖將填充了學生詳細信息的學生對象發送到特定服務器,然后閱讀從服務器發送回的消息。

主班:

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;
    }
}

問題在於它所做的只是在控制台上打印出學生的詳細信息,而程序只是說正在運行。 請幫助我找出我做錯了什么。

您的代碼缺少服務器部分:您的代碼兩次打開“客戶端”套接字; 一次寫信; 然后嘗試從此類套接字讀取內容。

但這不是客戶端/服務器的工作方式!

服務器端需要打開一個ServerSocket 並等待傳入​​的客戶端連接到它。

然后客戶端打開與該服務器的套接字連接。 發送數據,服務器獲取數據並執行某些操作。

請參閱此處以獲取有關該主題的第一個簡單教程!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM