基於java.net.socket包的對象傳遞


本案例代碼使用java.net.socket和java.io包下的相關api實現了對象傳遞。

相關代碼如下:

服務端代碼  ServerSocketHomework:

package com.etc.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.etc.entity.Emp;

/**
 * 1. 實現傳遞對象;自定義對象; emp對象?
 * 
 * @author Administrator
 *
 */
public class ServerSocketHomework {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ObjectInputStream ois = null;
        // 處於監聽狀態,等待連接
        try {
            
            serverSocket = new ServerSocket(1234);
            System.out.println("服務端正在監聽....");
            // 返回一個socket對象,這個socket對象的功能?
            socket = serverSocket.accept();

            is = socket.getInputStream();
            // 讀對象[反序列化]
            ois = new ObjectInputStream(is);

            Object obj = ois.readObject();

            if (obj instanceof Emp) {
                Emp emp = (Emp) obj;
                System.out.println("emp :" + emp);
            }
        } catch (IOException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 釋放資源
            try {
                serverSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                ois.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

 

 

客戶端代碼:ClientHomework

package com.etc.test;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

import com.etc.entity.Emp;

/**
 * 1. 實現傳遞對象;自定義對象; emp對象?
 * 
 * @author Administrator
 *
 */
public class ClientHomework {

    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        ObjectOutputStream oos = null;
        Scanner input = new Scanner(System.in);
        // 處於監聽狀態,等待連接
        try {
            // 換這個socket是和serversocket進行交互的對象
            socket = new Socket("127.0.0.1", 1234);
            os = socket.getOutputStream();
            // 寫[序列化的過程]
            oos = new ObjectOutputStream(os);

                System.out.println("請輸入員工的id,name,age,address,job,sal");
                int id = input.nextInt();
                String name = input.next();
                int age = input.nextInt();
                String address = input.next();
                String job = input.next();
                double sal = input.nextDouble();

                Emp emp = new Emp(id, name, age, address, job, sal);

                oos.writeObject(emp);
                System.out.println("傳輸成功~");
            

                input.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 釋放資源
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                oos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

 

上例中使用的實體類,要自己定義,注意要實現序列化接口:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM