请教java的网络传输中数据处理的问题
城中企鹅 2008-01-21 08:24:07 [size=11px][size=10px]我用服务器端建立了一个UDP的Socket,监听接受客户端传来的包。但是客户端是C语言写的,一个命令格式是一个结构体;
java中没有结构体,我在服务器端只能建立一个类封装这个命令格式。我要提问的是如何将客户端结构体中的成员数据传到java那个类的成员中?
这是这个类,按照C的结构体设计。
public class StructCommandData {
byte commandtype;
int sourceip;
int sourceport;
byte dst[]=new byte[6];
int priv_length;
public StructCommandData(){
}
public StructCommandData(byte commandtype,int sourceip,int sourceport,byte dst[],int priv_length){
this.command_type=command_type;
this.source_ip=source_ip;
this.source_port=source_port;
this.dst_mac=dst_mac;
this.priv_length=priv_length;
}}
服务器端
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
public class SocketPart {
DatagramSocket toRoutersock=null;
SocketPart(){
try {
toRoutersock = new DatagramSocket(2120);
byte[] buf = new byte[1024];//接受结构体中各个成员数据
DatagramPacket p =new DatagramPacket(buf,buf.length);//打包;
toRoutersock.receive(p);//将数据包接受到服务器端
//请问:然后如何将p.getDate()中的数据为java这个StructCommandData 类赋值?
且如果回传到客户端的话,如何按照C的结构体读取数据呢?
} catch (SocketException e) {
System.err.println("Can't open socket");
System.exit(1);
} catch (IOException e) {
System.err.println("Communication error");
e.printStackTrace();
}
}
}[/size][/size]