在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”)用于向客户机程序报告待查询的名字不存在;
请问怎么实现?
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”)用于向客户机程序报告待查询的名字不存在;
请问怎么实现?
解决方案 »
- 求教一个算法题
- java绘制多边形
- 关于模拟http post 图片文件时,出现的问题
- 日..期..问..题.. 如何获得某年某周的起始日期和结束日期 比如2005年第10周的起始日期和结束日期
- 怎么输出动态的unicode 值 ?
- 做完的程序如何才能在Java Web Start中运行?
- java的getClass()的问题
- 请问在JTABLE中如何将某几行设置成可以被选择的,其他几行设置成不能被选中的?
- 提供数据一方向接受数据一方通过网络提供数据,或者接受数据一方主动采集数据有什么好的方案,具体要求见里面
- 关于在线播放视频文件的解决方案,各位高人推荐一下吧!!
- applet读图片的问题
- 急急求,怎样可以让JTextField上怎样实现TextListener()里的textValueChanged(TextEvent e)方法;
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类
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();
}
}
}