以下是Java写的Socket通信代码,哪位大虾能帮忙改写成Delphi的?
import java.net.*;
import java.io.*;public class DispatchTest {
  public DispatchTest() {
  }  public static void main(String[] args){
      String str = "" +
          "<message>" +
          "     <user>" +
          "          <serialid > 591304001 </serialid >" +
          "          <mobilecode > 13040045001 </mobilecode >" +
          "    </user >" +
          "    <body>" +
          "          <groupid > 123 </groupid >" +
          "     </body >" +
          "</message >" +
          "";
      socket = null;
        try {
          socket = new Socket("localhost", 9070);
          socket.setKeepAlive(true);
          in = socket.getInputStream();
          out = socket.getOutputStream();
          write(str, out);
          byte[] bytes = new byte[str.length()];
          for(int j=0;j<12;j++){
            bytes[j] = 49;
          }
          String response = read();
          System.out.println(response);
          for (int i = 0; i < 3; i++) {
            response = read();
            System.out.println("心跳内容:" + response);
          }
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        try {
          if (socket != null) {
            socket.close();
            System.out.println(socket.isClosed());
          }
        }
        catch (Exception e) {
          e.printStackTrace();
        }
  }
  /**
   *  接收client信息
   *
   * 帧:
   * |-----------------------------------------|
   * | ushOrgMachineID(2) | ushOrgCommPort(2) | ushDestMachineID(2) | ushDestCommPort | ushPacketLength | ushCheckSum | xml content
   * |    发送端主机编码    |   发送端通信端口号  |     接收端主机编码    |  接收端通信端口号  |   xml包体的长度  |     校验码   |  xml包体的内容
   * |-----------------------------------------|
   * (括弧内为字节数,帧内容为文本格式,其它为16进制格式)
   *
   * @return
   */
  private static String read(){      try{
        bOrgID = readShort();
        if (bOrgID == -1) {
          return null;
        }
        bOrgPort = readShort();
        if( bOrgPort == -1){
          return  null;
        }
        bDestID = readShort();
        if(bDestID == -1){
          return null;
        }
        bDestPort = readShort();
        if(bDestID == -1){
          return null;
        }
        short bPackageLen = readShort();
        if(bPackageLen == -1){
          return null;
        }
        short bCheckSum = readShort();
        if(bCheckSum == -1){
          return null;
        }        byte[] byteData = new byte[bPackageLen];        for (int loop = 0; loop < bPackageLen; loop++) {
          int b = in.read();
          if (b != -1) {
            byteData[loop] = (byte) b;
          }else {
            return null;
          }
        }//for        return new String(byteData,"GB2312");
      }catch(Exception e){
        e.printStackTrace();
      }
      return null;
  }  private static short readShort(){
    try{
      short firstByte = (short) in.read();
      short secondeByte = (short) in.read();
      if(firstByte == -1||secondeByte == -1){
        return -1;
      }
      return (short)(firstByte | secondeByte<<8) ;
    }catch(Exception e){
      return -1;
    }
  }  public static boolean write(String str,OutputStream out){
    boolean result = true;
    try{
      byte[] data = str.getBytes("GB2312");
      int len = data.length;      byte[] byteData = new byte[len + 12];      write(bOrgID,byteData,0);
      write(bOrgPort,byteData,2);
      write(bDestID,byteData,4);
      write(bDestPort,byteData,6);
      write((short)len,byteData,8);
      write((short)0,byteData,10);      System.arraycopy(data, 0, byteData, 12, len);
      System.out.println("out = "+byteData.toString());
      out.write(byteData,0,len+12);
      out.flush();
    }catch(Exception e){
      e.printStackTrace();
      return false;
    }
    return result ;
  }  private static boolean write(short b,byte[] dest,int start){
    dest[start] = (byte) ((b >>> 0) & 0xFF);;
    dest[start+1] = (byte) ((b >>> 8) & 0xFF);;
    return true;
  }
  private static short bOrgID ;
  private static short bOrgPort;
  private static short bDestID;
  private static short bDestPort;
  private static Socket socket;
  private static InputStream in;
  private static OutputStream out;}

解决方案 »

  1.   

    我碰到的问题主要是如果按照那个协议将数据发给服务端,协议如下:
    | ushOrgMachineID(2) | ushOrgCommPort(2) | ushDestMachineID(2) | ushDestCommPort | ushPacketLength | ushCheckSum | xml content
    说明:发送端主机编码    |   发送端通信端口号  |     接收端主机编码    |  接收端通信端口号  |   xml包体的长度  |     校验码   |  xml包体的内容
    (括弧内为字节数,帧内容为文本格式,其它为16进制格式)在Delphi中该如何组包?
      

  2.   

    好办,真的好办.
    联系我:[email protected]