通过udp socket如何传递自定义类对象
展开全部
1.定义类
定义类,implements Serializable 接口,但不用实现任何方法;
发送和接收双方,进行类定义时,package 名 和 类名 必须一致
如:
package com.baggio.dhs.protocol;
import Java.io.Serializable;
public class PacketHead implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private byte start;
private byte type;
private short datalen;
private int sender_address;
private short serial_no;
private short frame_count;
private static short data_id=0;
private byte subtype;
private byte check_bit;
public PacketHead()
{}
public PacketHead(byte type, byte subtype, short datalen)
{
this.start=0x7F;
this.type=type;
this.datalen=datalen;
this.sender_address=0;
this.serial_no=0;
this.frame_count=1;
this.data_id++;
this.subtype=subtype;
this.check_bit=0;
}
public byte getType(){return type;}
public PacketHead setType(byte type){this.type=type; return this;}
public byte getSubtype(){return subtype;}
public PacketHead setSubtype(byte subtype){this.subtype=subtype; return this;}
public int getSenderAddress(){return sender_address;}
public PacketHead setSenderAddress(int sender_address){this.sender_address=sender_address; return this;}
}
2.发送方,进行发送对象的序列化
package com.baggio.dhs.protocol;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.NET.DatagramPacket;
import java.Net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class SendObject
{
public static void main(String[] args)
{
DatagramSocket dSocket = null;
InetAddress local=null;
try {
local = InetAddress.getByName("192.168.1.89");
} catch (UnknownHostException e) {
e.printStackTrace();
}
try {
dSocket = new DatagramSocket(6001);
} catch (SocketException e) {
e.printStackTrace();
}
PacketHead message=new PacketHead((byte)0x71, (byte)0x41, (short)0x00);
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ObjectOutputStream oout = null;
try {
oout = new ObjectOutputStream(bout);
oout.writeObject(message);
oout.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} //序列化对象
byte[] sendBuff=bout.toByteArray(); //转化为字节数组
DatagramPacket dPacket=new DatagramPacket(sendBuff,sendBuff.length,local,24202);
/* String message=new String("hello world");
DatagramPacket dPacket=new DatagramPacket(message.getBytes(), message.length(), local, 24202);
*/
try {
dSocket.send(dPacket);
} catch (IOException e) {
e.printStackTrace();
}
dSocket.close();
System.out.println("Send success");
}
}
3.接收方 ,进行反序列化,获得对象
package com.baggio.dhs.protocol;
。。。。。。。。
mSocket.receive(packet);
String fromIP=packet.getAddress().getHostAddress();
int fromPort=packet.getPort();
PacketHead message=new PacketHead();
ByteArrayInputStream bint=new ByteArrayInputStream(recvBuff);
ObjectInputStream oint=new ObjectInputStream(bint);
message=(PacketHead)oint.readObject(); //反序列化,恢复对象
Log.i(MY_TAG, "type="+message.getType()+"subtype"+message.getSubtype());
定义类,implements Serializable 接口,但不用实现任何方法;
发送和接收双方,进行类定义时,package 名 和 类名 必须一致
如:
package com.baggio.dhs.protocol;
import Java.io.Serializable;
public class PacketHead implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private byte start;
private byte type;
private short datalen;
private int sender_address;
private short serial_no;
private short frame_count;
private static short data_id=0;
private byte subtype;
private byte check_bit;
public PacketHead()
{}
public PacketHead(byte type, byte subtype, short datalen)
{
this.start=0x7F;
this.type=type;
this.datalen=datalen;
this.sender_address=0;
this.serial_no=0;
this.frame_count=1;
this.data_id++;
this.subtype=subtype;
this.check_bit=0;
}
public byte getType(){return type;}
public PacketHead setType(byte type){this.type=type; return this;}
public byte getSubtype(){return subtype;}
public PacketHead setSubtype(byte subtype){this.subtype=subtype; return this;}
public int getSenderAddress(){return sender_address;}
public PacketHead setSenderAddress(int sender_address){this.sender_address=sender_address; return this;}
}
2.发送方,进行发送对象的序列化
package com.baggio.dhs.protocol;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.NET.DatagramPacket;
import java.Net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class SendObject
{
public static void main(String[] args)
{
DatagramSocket dSocket = null;
InetAddress local=null;
try {
local = InetAddress.getByName("192.168.1.89");
} catch (UnknownHostException e) {
e.printStackTrace();
}
try {
dSocket = new DatagramSocket(6001);
} catch (SocketException e) {
e.printStackTrace();
}
PacketHead message=new PacketHead((byte)0x71, (byte)0x41, (short)0x00);
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ObjectOutputStream oout = null;
try {
oout = new ObjectOutputStream(bout);
oout.writeObject(message);
oout.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} //序列化对象
byte[] sendBuff=bout.toByteArray(); //转化为字节数组
DatagramPacket dPacket=new DatagramPacket(sendBuff,sendBuff.length,local,24202);
/* String message=new String("hello world");
DatagramPacket dPacket=new DatagramPacket(message.getBytes(), message.length(), local, 24202);
*/
try {
dSocket.send(dPacket);
} catch (IOException e) {
e.printStackTrace();
}
dSocket.close();
System.out.println("Send success");
}
}
3.接收方 ,进行反序列化,获得对象
package com.baggio.dhs.protocol;
。。。。。。。。
mSocket.receive(packet);
String fromIP=packet.getAddress().getHostAddress();
int fromPort=packet.getPort();
PacketHead message=new PacketHead();
ByteArrayInputStream bint=new ByteArrayInputStream(recvBuff);
ObjectInputStream oint=new ObjectInputStream(bint);
message=(PacketHead)oint.readObject(); //反序列化,恢复对象
Log.i(MY_TAG, "type="+message.getType()+"subtype"+message.getSubtype());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询