Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 java与C服务端的互操作
softtiger





发贴: 46
积分: 10
于 2003-12-16 11:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
附件说明: 附件为原理实验性的代码,最终代码属职务作品,不便于公布,请见谅.
背景: 已存在C写的服务端程序,现要求用java编写客户端与之进行通讯.
请求格式:
  typedef struct stStbRequest
  {
   unsigned char    cFunctionCode;  //业务码
   unsigned long int  nPackageIndex;  //包序号-可用time()的返回值
   unsigned long int  nCommitIndex;  //受理编号
   unsigned long int  nUserNumber;  //用户号--暂采用IC卡号
   unsigned long int  nPgmNumber;  //节目编号
   unsigned long int  nPlayTime;  //定位播放位置-时间(秒)
  }STBREQUEST;
  
响应包: 略

java客户端请求格式定义

  public class STBRequest {
    private int cFunctionCode=0;
    private int nPackageIndex=0;
    private int nCommitIndex=0;
    private int nUserNumber=0;
    private int nPgmNumber=0;
    private int nPlayTime=0;
    public int getCFunctionCode() {
      return cFunctionCode;
          }  
    public void setCFunctionCode(int cFunctionCode) {
       this.cFunctionCode = cFunctionCode;
   ....
   public void write(DataOutputStream out) throws IOException
      {
         out.write(getCFunctionCode());
         out.writeint(getNPackageIndex());
         out.writeInt(getNCommitIndex());
         out.writeInt(getNUserNumber());
         out.writeInt(getNPgmNumber());
         out.writeInt(getNPlayTime());
      }
    ..............  
}      

版本1.
客户段代码片段
     String destAddress="192.168.0.9";
        int port=7100;
        InetAddress iaddress=InetAddress.getByName(destAddress);
        Socket socket=new Socket(iaddress,port);
     DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
        DataInputStream socketIn=new DataInputStream(socket.getInputStream());
     qa.STBRequest stbRequest=new qa.STBRequest();
     stbRequest.setNPgmNumber(1);
     stbRequest.setNPackageIndex(40);
     stbRequest.setCFunctionCode(20);
     stbRequest.setNUserNumber(900);
     stbRequest.write(dout);
     dout.flush();
运行现象: 服务端读出的数据乱糟糟的,只有functioncode是对的.
调 试: 我们用java编写服务端,用read逐字节读出,结果是对的.
原因分析:   服务端接收片段
          ...............
          STBREQUEST request;
          zerof(request,sizeof(request));
          recv(acceptSocket,&request,21,0);
          ..................
    服务端是用recv读入整个数据包的,且没有使用MSG_WAITALL选项,
    而客户端虽然只用了一个flush()方法,但实际上是发出网络发送调用,所以一个recv只能读出一个java客户端的writexxx方法
    写的数据.
版本2.
  STBREQUEST的write方法改为
      public void write(DataOutputStream out) throws IOException
      {
        byte bytes[]=new byte[21];
        java.nio.ByteBuffer byteBuffer=java.nio.ByteBuffer.allocate(21);
        byte functionCode=new Integer(getCFunctionCode()).byteValue();
        byteBuffer.put(functionCode);
        byteBuffer.putInt(getNPackageIndex());
        byteBuffer.putInt(getNCommitIndex());
        byteBuffer.putInt(getNUserNumber());
        byteBuffer.putInt(getNPgmNumber());
        byteBuffer.putInt(getNPlayTime());
        out.write(byteBuffer.array());
      }
    则服务端接收成功.
    注:要求服务程序采用字节对齐方式 #pragma pack(0),否则java客户短要手动按字对齐.

src.rar (3.34k)




话题树型展开
人气 标题 作者 字数 发贴时间
2239 java与C服务端的互操作 softtiger 3096 2003-12-16 11:03

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923