请问如何将下列程序中的upd包组建成用java发送的UDP#define  OCISS_IM_EXT_CMD_SMS_CALL          129typedef struct {
unsigned char iOffset;
unsigned char iCmdID;
    unsigned short iMesgID;
}PDU_head;#define     OCISS_FLOAT_STRING_SIZE                16
#define     OCISS_MAX_SUB_PIN_SIZE 16
#define     OCISS_MAX_EXT_NO   8
#define     OCISS_NCC_MAX_DIGITS                 31 typedef struct
{
  int  m_nIMserverId;    //IM server authenticate.
  int  m_nGatewayId;     //tell the gateway id that this cmd designates to.
  int  m_nIMcallId;      //the call id applied from IM server for smd,sms call.
  int  m_nRadServerId;   //IM tell gateway which radius to send cdr
  int  m_nSrcTrunkId;    //src trunk id.
  int  m_nSrcLineGrade;  //src line grade.
  int  m_nSrcCCodeLen;   //src country code length ***  newly added here.
  int  m_nDnisTrunkId;   //dest trunk id.
  int  m_nDnisLineGrade; //dnis line grade.
  int  m_nDnisCCodeLen;  //dnis country code length.***newly added here.
  int  m_nUnitSeconds;   //call charge unit seconds.
  int  m_nMaxCallDurMin; //maximum call duration for this company.  char m_fszSrcRate [OCISS_FLOAT_STRING_SIZE+1];     //call back src no
  char m_fszDnisRate[OCISS_FLOAT_STRING_SIZE+1];     //call back dest no
  char m_fszCreditLeft[OCISS_FLOAT_STRING_SIZE+1];   //user account credit left.  char m_szUserPin[OCISS_MAX_SUB_PIN_SIZE+1];
  char m_szSrcNo [OCISS_NCC_MAX_DIGITS+1];
  char m_szSrcPrefix[OCISS_MAX_EXT_NO+1];
  char m_szDnisNo[OCISS_NCC_MAX_DIGITS+1];
  char m_szDnisPrefix[OCISS_MAX_EXT_NO+1];
}OCISS_IM_SMD_CALL_INIT;

解决方案 »

  1.   

    JAVA 中一切都是类,C 中的结构需要写成类。GOOGLE: java UDPhttp://www.javaresearch.org/article/showarticle.jsp?column=291&thread=2242
    http://tech.sina.com.cn/c/2002-08-14/15097.html
    ...大把。:)
      

  2.   

    呵呵十分感谢doway(john)的帮助,可是你提供的第一篇文章对如何构造数据报的格式的类不是很详细,我不太明白,你能看看我这个结构怎样写成类呢?
      

  3.   


    import java.lang.Thread;
    import java.net.*;
    import java.io.*;
    public class UDPClient extends Thread{

    private DatagramSocket s;
    private InetAddress hostAddress;
    private byte[] buf=new byte[1000];
    private DatagramPacket dp=new DatagramPacket(buf,buf.length);
    private int id;

    public UDPClient(int identifier){
    id=identifier;
    try{
    s=new DatagramSocket();
    hostAddress=InetAddress.getByName("localhost");
    }catch(UnknownHostException e){
    System.err.println("Cannot find host");
    System.exit(1);
    }catch(SocketException e){
    System.err.println("Can't open Socket");
    e.printStackTrace();
    System.exit(1);
    }
    }

    public void run(){
    try{
    for(int i=0;i<1;i++){//消息数

    String outMessage="Client #"+
    id+",message#"+i;

    s.send(Dgram.toDatagram(outMessage,hostAddress,UDPServer.INPORT));
    s.receive(dp);
    String rcvd="Client #"+id+",rcvd from "+
    dp.getAddress()+","+
    dp.getPort()+":"+
    Dgram.toString(dp);
    System.out.println(rcvd);

    }
    }catch(IOException e){
    e.printStackTrace();
    System.exit(1);

    }
    }

    public static void main(String[] args){
    for (int i=0;i<1;i++)//客户数量
    new UDPClient(i).start();
    }
    }//Dgram.java 数据报格式
    import java.net.*;public class Dgram {
    public static DatagramPacket toDatagram(
    String s,InetAddress destIA,int destPort){
    byte[] buf=new byte[s.length()+1];
    s.getBytes(0,s.length(),buf,0);
    return new DatagramPacket(buf,buf.length,destIA,destPort);
    }

    public static String toString(DatagramPacket p){
    return new String(p.getData(),0,p.getLength());
    }
    }
      

  4.   

    public class PDU_head {
      byte iOffset;
      byte iCmdID;
      char iMesgID;
    }public class OCISS_IM_SMD_CALL_INIT
    {
      int  m_nIMserverId;    //IM server authenticate.
      int  m_nGatewayId;     //tell the gateway id that this cmd designates to.
      int  m_nIMcallId;      //the call id applied from IM server for smd,sms call.
      int  m_nRadServerId;   //IM tell gateway which radius to send cdr
      int  m_nSrcTrunkId;    //src trunk id.
      int  m_nSrcLineGrade;  //src line grade.
      int  m_nSrcCCodeLen;   //src country code length ***  newly added here.
      int  m_nDnisTrunkId;   //dest trunk id.
      int  m_nDnisLineGrade; //dnis line grade.
      int  m_nDnisCCodeLen;  //dnis country code length.***newly added here.
      int  m_nUnitSeconds;   //call charge unit seconds.
      int  m_nMaxCallDurMin; //maximum call duration for this company.  byte m_fszSrcRate [SizeConstants.OCISS_FLOAT_STRING_SIZE+1];     //call back src no
      byte m_fszDnisRate[SizeConstants.OCISS_FLOAT_STRING_SIZE+1];     //call back dest no
      byte m_fszCreditLeft[SizeConstants.OCISS_FLOAT_STRING_SIZE+1];   //user account credit left.  byte m_szUserPin[SizeConstants.OCISS_MAX_SUB_PIN_SIZE+1];
      byte m_szSrcNo [SizeConstants.OCISS_NCC_MAX_DIGITS+1];
      byte m_szSrcPrefix[SizeConstants.OCISS_MAX_EXT_NO+1];
      byte m_szDnisNo[SizeConstants.OCISS_NCC_MAX_DIGITS+1];
      byte m_szDnisPrefix[SizeConstants.OCISS_MAX_EXT_NO+1];
    }public class SizeConstants {
      public static final byte OCISS_FLOAT_STRING_SIZE = 255;
      ...
    }结构写成类定义,为常量专门定义一个类。UDP 我完全不熟悉,TCP 也只知道一点点而已,至于 UDP 是怎么发送的,以及接收后怎么需要的转换操作我就都不清楚了。还没学呢。:)
      

  5.   

    首先Suddy(风),doway(john) 2位在深夜还能回贴,在此表示我最真挚的谢意.不过suddy的程序并没有说明如何构造数据包格式,Dgram类只是简单的发了个字符串而已,而 doway(john) 的程序只是构造了几个类,可怎样把这个类转变成UDP包发送呢?