在Client-Server中客户短发送的消息规定了格式:查询请求和回答采用如下共同的消息格式:
Message Type
(1 byte) String Length 
(1 byte) String 
(<= 255 bytes)
其中:
1. Message Type = “Q” (ASCII 81) 表示请求;   Message Type = “R” (ASCII 82) 表示回答;
2. String Length 是一个无符号8位整数,用于指出String域的长度;
3. 对于查询请求,String包含待查询的名字;对于查询回答,String包含查询所得的电子邮件地址;
4. 消息(Message Type = “R”,String Length = “8”,String = “No Match”)用于向客户机程序报告待查询的名字不存在;
请问怎么实现?

解决方案 »

  1.   

    报文组装例子:
    String s_send="";
    String s_query="where are you?";
    s_send="Q"+String.valueOf(s_query.length())+s_query;ByteArrayInputStream s_ret = XXXXX.send(xmlData);//XXXX是ClientServer类
      

  2.   

    较完整的发送例子:
     public ByteArrayInputStream send(String xmlData) throws MyException {
        ByteArrayInputStream s_ret = null;
       
        //发送给网关报文的方法
        UtilClient client = new UtilClient();
        try {
          if (!client.connect()) {
            throw new Exception("与网关通讯失败");
          }
          client.sendDataToServer(xmlData);
          byte abyte[] = client.recevieByteDataFromServer();
          client.close();
          s_ret = new ByteArrayInputStream(abyte);
        }
        catch (Exception e) {
          throw new MyException(e, "", "与网关通讯失败");
        }
        return s_ret;
      }
    }----------------------------------import java.io.*;
    import java.net.*;
    import java.util.*;public class UtilClient {  private static final int WRITE_BYTE = 256;
      private static final int HEAD_LEN = 4;
      private static final int DELAY_TIME = 20;  private static int GatewayPort = 0;
      private static String GatewayIP = null;
      private static int retryTimes = 3;  private Socket clientSocket = null;
      private InputStream clientReceive = null;
      private OutputStream clientSend = null;  public UtilClient() {
      }  private byte[] read(InputStream inputstream) throws Exception {
        try {
          byte abyte0[] = readBytes(inputstream, HEAD_LEN);
          int i = Integer.parseInt(new String(abyte0));
          return readBytes(inputstream, i);
        }
        catch (Exception e) {
          throw e;
        }
      }  
      private void write(OutputStream outputstream, int i, byte abyte0[]) throws
          IOException {
        byte abyte1[] = new byte[HEAD_LEN];
        String strLen = Integer.toString(i);
        for (int iLen = 0; iLen < HEAD_LEN - strLen.length(); iLen++) {
          strLen = " " + strLen;
        }
        abyte1 = (strLen).getBytes();
        outputstream.write(abyte1);
        int j = 0;
        do {
          int k = i - j <= WRITE_BYTE ? i - j : WRITE_BYTE;
          outputstream.write(abyte0, j, k);
          j += k;
        }
        while (i > j);
      } 
      public boolean connect() throws MyException {
     
       for (int i = 0; i < retryTimes; i++) {
          try {
            clientSocket = new Socket(GatewayIP, GatewayPort);
            clientReceive = clientSocket.getInputStream();
            clientSend = clientSocket.getOutputStream();
            return true;
          }
          catch (Exception e) {
          }
          try {
            Thread.sleep(DELAY_TIME);
          }
          catch (Exception e) {
          }
        }
        return false;
      }  public void sendDataToServer(String s) throws Exception {
        if (s != null) {
          byte abyte0[] = s.getBytes();
          int i = abyte0.length;
          if (clientSend != null) {
            write(clientSend, i, abyte0);
          }
          else {
            throw new Exception("outputStream is null in sendDataToServer");
          }
        }
        else {
          throw new Exception("send data is null in sendDataToServer");
        }
      }  public String recevieStrDataFromServer() throws Exception {
        String s;
        if (clientReceive != null) {
          byte abyte0[] = read(clientReceive);
          s = new String(abyte0);
        }
        else {
          throw new Exception("inputStream is null in recevieDataFromServer");
        }
        return s;
      }  public void close() throws Exception {
        if (clientSend != null) {
          clientSend.close();
        }
        if (clientReceive != null) {
          clientReceive.close();
        }
        if (clientSocket != null) {
          clientSocket.close();
        }
      }
    }