首页 > 技术文章 > 用java的socket来发送一个类

miaoz 2014-01-01 09:32 原文

用socket可以简单的发送一些文本信息,太复杂的可能发送不了,比如图片音频可能要用到http来发送和接收了。最基本的使用socket来发送一个字符串,但有的时候我们希望能够发送一个类,这样server接收端处理信息将会非常方便。

对于待发送的类,要用socket来发送,则必须实现Serializable接口。实现这个接口后就将发送的对象串行化了,再使用对象输入输出流(ObjectOutputStream, ObjectInputStream)就可实现发送与接收。这个过程有点像通信里边的调制过程,在接收端server再将串行化的对象恢复成之前的类,就像解调过程。比如我们定义一个packets这个类,来发送一些信息。

import java.io.Serializable;

public class Packets implements Serializable{
    
    public int id;
    public String value;
    public int ballot_pid;
    public int ballot_num;
    public int position;
    
    public Packets(int id, String value, int ballot_pid, int ballot_num, int position){
        this.id= id;
        this.value= value;
        this.ballot_pid= ballot_pid;
        this.ballot_num= ballot_num;
        this.position= position;

    }    
}

 

  在发送的时候,我们定义一个client类。每次我们想发送packets,都先用packets的构造函数新建一个包,比如Packets pack= new Packets(......); 然后再用client里的send方法来将包发送出去,即client.send(pack)就可。

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client{
    private int port= 2013;
    private String host= "127.0.0.1";
    
    public Client(String host, int port){
        this.host= host;
        this.port= port;
    }
    
    public void send(Packets packets) throws UnknownHostException, IOException{
        Socket socket= new Socket(host, port);
        
        ObjectOutputStream os= new ObjectOutputStream(socket.getOutputStream());
        os.writeObject(packets);
        socket.getOutputStream().flush();
        socket.close();
    }
}

  

在接收的时候,用ObjectInputStream来获取这个包。

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

public class Server extends Thread{
    private int port= 2013;
    private int sequence= 1;
    
    /*flag used to stop this thread*/
    private boolean stop= false;
    
    
    /*pass addresses of clients from outside*/
    public Server(int port){
        this.port= port;
    }
    
    public void run(){
        Socket socket= null;
        try{
            ServerSocket serverSocket= new ServerSocket(port);
            while(!stop){
                socket= serverSocket.accept();
                handleSocket(socket);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
        super.run();
    }
    
    private void handleSocket(Socket socket) throws IOException, ClassNotFoundException{
        ObjectInputStream ois= new ObjectInputStream(socket.getInputStream());
        Packets packets= (Packets)ois.readObject();
        
        /*handle packets start*/
        /*if broadcast, use 'Blog.broadcast(Packets packets)'*/
        System.out.println("id : "+ packets.id);
        System.out.println("val: "+ packets.value);
        System.out.println("pid: "+ packets.ballot_pid);
        System.out.println("num: "+ packets.ballot_num);
        System.out.println("pos: "+ packets.position);    
        /*handle packets end*/
        
        socket.close();
    }
    
    /*function used to fail this server*/
    public void fail(){
        this.stop= true;
    }
    
    /*function used to restart this server*/
    public void unfail(){
        this.stop= false;
    }    
    
}

 

推荐阅读