通过UDP发送多个对象实例

时间:2015-07-29 02:04:39

标签: java object network-programming udp

我是java network programming的新手。 我尝试在UDP上发送一个名为Packet object的多个实例 发件人:将String更改为charArray,然后在object中发送每个字符 Receiver:接收Datagrampacket和readObject,然后将其打印出来 发件人没问题,但是接收器只接受对象的第一个实例,并将其打印的次数与charArray的长度一样多。这是我的代码:
接收机

public class Receiver {
    public static void main(String args[]) throws Exception 
    { 
        DatagramSocket rcvSocket;
        DatagramPacket rcvPacket;
        ByteArrayInputStream bis;
        ObjectInputStream ois;
        byte[] rcvData;
        Packet SnWPacket;

        rcvSocket = new DatagramSocket(7777);
        rcvData = new byte[1024];
        rcvPacket = new DatagramPacket(rcvData, rcvData.length);

        while(true)
        {
            rcvSocket.receive(rcvPacket);
            byte[] in = rcvPacket.getData();
            bis = new ByteArrayInputStream(in);
            ois = new ObjectInputStream(bis);
            SnWPacket = (Packet) ois.readObject();
            System.out.println("RECIEVED: " + SnWPacket);
            ois.close();
            bis.close();
        }

    }
}

发件人

public class Sender {
    public static void main(String args[]) throws Exception 
    { 

        DatagramPacket sndPacket;
        DatagramSocket sndSocket;
        ByteArrayOutputStream bos;
        ObjectOutputStream oos;
        InetAddress addr;
        Scanner sc;

        char[] charArray;
        int seqNo = 0;
        byte sndData[];
        Packet SnWPacket;

        addr = InetAddress.getByName("localhost");
        sndSocket = new DatagramSocket();
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        sc = new Scanner(System.in);

        System.out.println("Input: ");
        String in = sc.nextLine();
        charArray = in.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            SnWPacket = new Packet("data", seqNo, charArray[i]);
            oos.writeObject(SnWPacket);
            oos.flush();
            sndData = bos.toByteArray();
            bos.flush();
            sndPacket = new DatagramPacket(sndData, sndData.length, addr, 7777);
            sndSocket.send(sndPacket);
            System.out.println("SENT: " + SnWPacket);
            seqNo += 1;
        }
        oos.close();
        bos.close();
        sndSocket.close();
    } 
}

分组

public class Packet implements Serializable{
    private String type;
    private int seqNo;
    private char data;
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getSeqNo() {
        return seqNo;
    }
    public void setSeqNo(int seqNo) {
        this.seqNo = seqNo;
    }
    public char getData() {
        return data;
    }
    public void setData(char data) {
        this.data = data;
    }
    public Packet(String type, int seqNo, char data)
    {
        this.type = type;
        this.seqNo = seqNo;
        this.data = data;
    }
    public Packet()
    {
        this.type = "";
        this.seqNo = 0;
        this.data = ' ';
    }
    @Override
    public String toString() {
        return "Packet: \tType: " + this.type + "\tseqNo: " + this.seqNo + "\tdata: " + this.data;
    }
}

假设我发送:&#34; abc&#34;
发信人:

SENT: Packet:   Type: data  seqNo: 0    data: a
SENT: Packet:   Type: data  seqNo: 1    data: b
SENT: Packet:   Type: data  seqNo: 2    data: c

接收器:

RECIEVED: Packet:   Type: data  seqNo: 0    data: a
RECIEVED: Packet:   Type: data  seqNo: 0    data: a
RECIEVED: Packet:   Type: data  seqNo: 0    data: a

问题是接收器打印三个&#34; a&#34;。请帮我搞清楚。谢谢!

1 个答案:

答案 0 :(得分:4)

要调试此类事情,使用tcpdump或类似内容来跟踪网络上的实际情况会有所帮助:

$ tcpdump -i lo port 7777
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
19:53:20.818460 IP zoo.59305 > zoo.7777: UDP, length 83
19:53:20.822292 IP zoo.59305 > zoo.7777: UDP, length 100
19:53:20.822410 IP zoo.59305 > zoo.7777: UDP, length 117

这告诉您正在发送您期望的3个数据包,但每个数据包比前一个数据包大17个字节(而不是所有数据包大小相同)。检查你的发送代码仔细解释了这一点 - 你使用一个ObjectOutputStream来构造数据包,你不会在Packet对象之间重置它,所以发送的第一个数据包包含第一个Packet对象,而第二个数据包包含 TWO < / em>数据包对象(第一个的副本,然后是第二个副本),第三个包含所有三个。

在接收方,您只解码收到的数据包中的第一个数据包对象并进行打印,因此您打印第一个数据包三次......