请教关于Socket通信的协议问题(100分)

  • 主题发起人 主题发起人 weyoung
  • 开始时间 开始时间
W

weyoung

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟用Delphi写一个Socket通信的程序,数据在发送给服务器之前,需要按照一定的格式打包,协议如下:
ushOrgMachineID(2) | ushOrgCommPort(2) | ushDestMachineID(2) | ushDestCommPort | ushPacketLength | ushCheckSum | xml content
说明:发送端主机编码 | 发送端通信端口号 | 接收端主机编码 | 接收端通信端口号 | xml包体的长度 | 校验码 | xml包体的内容
(括弧内为字节数,帧内容为文本格式,其它为16进制格式)

在Delphi中如何打包发送?
 
以下是我在Java上的实现代码,只要把它转换成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 >" +
" <type > client </type >" +
" <command >doCall</command >" +
" <role > 0 </role >" +
" <isresult > false </isresult >" +
" <time >17-03-2004 12:18:24 cst</time >" +
" <level > 0 </level >" +
" <version>1.0.0 </version >" +
" <param1/>" +
" <param2/>" +
" </user >" +
" <body>" +
" <groupid > 123 </groupid >" +
" <groupname > 群组名称 </groupname >" +
" <members >" +
" <membername > 成员名称 </membername >" +
" <mobilecode > 13777453515 </mobilecode >" +
" </members >" +
" </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;

}
 

Similar threads

后退
顶部