以下是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;}
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;}
解决方案 »
- 串口编程问题,急
- 有关TListView问题
- 关于grid数据导出到excel表格的效率的问题?
- 抓屏如何把鼠标图象一起抓进去?
- 我用ADO +TCP/ip访问sql2000 server,如果访问非缺省端口的sql2000server?
- 会用钩子(hook)了,但是不是全局的,请高手指点。
- VC++和delphi
- (版主别骂我, 我已经找过了, 但是没找着)怎样用{$define}定义一个几个单元都能用{$ifdef}来使用的符号常量?
- 100分~~100分~~高分求购。。。我也‘倾家荡产求购优秀系统分析案例’(paste from yyzh)。。。100分~~
- oracle 8.1.6中为什么Enterprice Manager Console进不去,急急急!!!
- 如何取表中的第11-20条记录?表中有不只10条的记录。
- 感谢Nyf1220!接分!
| ushOrgMachineID(2) | ushOrgCommPort(2) | ushDestMachineID(2) | ushDestCommPort | ushPacketLength | ushCheckSum | xml content
说明:发送端主机编码 | 发送端通信端口号 | 接收端主机编码 | 接收端通信端口号 | xml包体的长度 | 校验码 | xml包体的内容
(括弧内为字节数,帧内容为文本格式,其它为16进制格式)在Delphi中该如何组包?
联系我:[email protected]