谁帮我把这个翻译成delphi的呀!我急需 ...

buff 2003-05-24 03:40:16
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define SOURCE_PORT 7234
#define MAX_RECEIVEBYTE 255

typedef struct ip_hdr //定义IP首部
{
unsigned char h_verlen; //4位首部长度,4位IP版本号
unsigned char tos; //8位服务类型TOS
unsigned short total_len; //16位总长度(字节)
unsigned short ident; //16位标识
unsigned short frag_and_flags; //3位标志位
unsigned char ttl; //8位生存时间 TTL
unsigned char proto; //8位协议 (TCP, UDP 或其他)
unsigned short checksum; //16位IP首部校验和
unsigned int sourceIP; //32位源IP地址
unsigned int destIP; //32位目的IP地址
}IPHEADER;

typedef struct tsd_hdr //定义TCP伪首部
{
unsigned long saddr; //源地址
unsigned long daddr; //目的地址
char mbz;
char ptcl; //协议类型
unsigned short tcpl; //TCP长度
}PSDHEADER;

typedef struct tcp_hdr //定义TCP首部
{
USHORT th_sport; //16位源端口
USHORT th_dport; //16位目的端口
unsigned int th_seq; //32位序列号
unsigned int th_ack; //32位确认号
unsigned char th_lenres; //4位首部长度/6位保留字
unsigned char th_flag; //6位标志位
USHORT th_win; //16位窗口大小
USHORT th_sum; //16位校验和
USHORT th_urp; //16位紧急数据偏移量
}TCPHEADER;

//CheckSum:计算校验和的子函数
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while(size >1)
{
cksum+=*buffer++;
size -=sizeof(USHORT);
}
if(size )
{
cksum += *(UCHAR*)buffer;
}

cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}

void useage()
{
printf("******************************************\n");
printf("TCPPing\n");
printf("\t Written by Refdom\n");
printf("\t Email: refdom@263.net\n");
printf("Useage: TCPPing.exe Target_ip Target_port \n");
printf("*******************************************\n");
}

int main(int argc, char* argv[])
{
WSADATA WSAData;
SOCKET sock;
SOCKADDR_IN addr_in;
IPHEADER ipHeader;
TCPHEADER tcpHeader;
PSDHEADER psdHeader;

char szSendBuf[60]={0};
BOOL flag;
int rect,nTimeOver;

useage();

if (argc!= 3)
{ return false; }

if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0)
{
printf("WSAStartup Error!\n");
return false;
}

if
((sock=WSASocket(AF_INET,SOCK_RAW,IPPROTO_RAW,NULL,0,WSA_FLAG_OVERLAP
PED))==INVALID_SOCKET)
{
printf("Socket Setup Error!\n");
return false;
}
flag=true;
if (setsockopt(sock,IPPROTO_IP, IP_HDRINCL,(char
*)&flag,sizeof(flag))==SOCKET_ERROR)
{
printf("setsockopt IP_HDRINCL error!\n");
return false;
}

nTimeOver=1000;
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&nTimeOver,
sizeof(nTimeOver))==SOCKET_ERROR)
{
printf("setsockopt SO_SNDTIMEO error!\n");
return false;
}
addr_in.sin_family=AF_INET;
addr_in.sin_port=htons(atoi(argv[2]));
addr_in.sin_addr.S_un.S_addr=inet_addr(argv[1]);

//
//
//填充IP首部
ipHeader.h_verlen=(4<<4 | sizeof(ipHeader)/sizeof(unsigned long));
// ipHeader.tos=0;
ipHeader.total_len=htons(sizeof(ipHeader)+sizeof(tcpHeader));
ipHeader.ident=1;
ipHeader.frag_and_flags=0;
ipHeader.ttl=128;
ipHeader.proto=IPPROTO_TCP;
ipHeader.checksum=0;
ipHeader.sourceIP=inet_addr("本地地址");
ipHeader.destIP=inet_addr(argv[1]);

//填充TCP首部
tcpHeader.th_dport=htons(atoi(argv[2]));
tcpHeader.th_sport=htons(SOURCE_PORT); //源端口号
tcpHeader.th_seq=htonl(0x12345678);
tcpHeader.th_ack=0;
tcpHeader.th_lenres=(sizeof(tcpHeader)/4<<4|0);
tcpHeader.th_flag=2; //修改这里来实现不同的标志位探测,2是SYN,1是FIN,16是ACK
探测 等等
tcpHeader.th_win=htons(512);
tcpHeader.th_urp=0;
tcpHeader.th_sum=0;

psdHeader.saddr=ipHeader.sourceIP;
psdHeader.daddr=ipHeader.destIP;
psdHeader.mbz=0;
psdHeader.ptcl=IPPROTO_TCP;
psdHeader.tcpl=htons(sizeof(tcpHeader));

//计算校验和
memcpy(szSendBuf, &psdHeader, sizeof(psdHeader));
memcpy(szSendBuf+sizeof(psdHeader), &tcpHeader, sizeof(tcpHeader));
tcpHeader.th_sum=checksum((USHORT *)szSendBuf,sizeof(psdHeader)+sizeof(tcpHeader));

memcpy(szSendBuf, &ipHeader, sizeof(ipHeader));
memcpy(szSendBuf+sizeof(ipHeader), &tcpHeader, sizeof(tcpHeader));
memset(szSendBuf+sizeof(ipHeader)+sizeof(tcpHeader), 0, 4);
ipHeader.checksum=checksum((USHORT *)szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader));

memcpy(szSendBuf, &ipHeader, sizeof(ipHeader));

rect=sendto(sock, szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader),
0, (struct sockaddr*)&addr_in, sizeof(addr_in));
if (rect==SOCKET_ERROR)
{
printf("send error!:%d\n",WSAGetLastError());
return false;
}
else
printf("send ok!\n");

closesocket(sock);
WSACleanup();

return 0;
}

先谢谢高手了!!!
...全文
20 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjqyb 2003-08-23
  • 打赏
  • 举报
回复
uses
windows,
sysutils,
winsock,
winsock2;

const SOURCE_PORT=7234 ;
MAX_RECEIVEBYTE=255 ;

type
TIPHEADER=packed record //定义IP首部
h_verlen:byte; //4位首部长度,4位IP版本号
tos:byte; //8位服务类型TOS
total_len:word; //16位总长度(字节)
ident:word; //16位标识
frag_and_flags:word; //3位标志位
ttl:byte; //8位生存时间 TTL
proto:byte; //8位协议 (TCP, UDP 或其他)
checksum:word; //16位IP首部校验和
sourceIP:Dword; //32位源IP地址
destIP:dword; //32位目的IP地址
end;
//R;

TPSDHEADER=record //定义TCP伪首部
saddr:dword; //源地址
daddr:dword; //目的地址
mbz:byte;
ptcl:byte; //协议类型
tcpl:word; //TCP长度
end;
//;

TTCPHEADER=record //定义TCP首部

th_sport:word; //16位源端口
th_dport:word; //16位目的端口
th_seq:Dword; //32位序列号
th_ack:Dword; //32位确认号
th_lenres:byte; //4位首部长度/6位保留字
th_flag:byte; //6位标志位
th_win:word; //16位窗口大小
th_sum:word; //16位校验和
th_urp:word; //16位紧急数据偏移量
end;


//CheckSum:计算校验和的子函数

function checksum(buffer:pword; size:integer):word ;
var cksum:Dword;
Begin
cksum:=0;
while(size >1) do
Begin
cksum:=cksum+buffer^;
inc(buffer);
size:=size-sizeof(word);
End;
if(size>0 ) then
cksum:=cksum+ord(pchar(buffer)[0]);
cksum := cksum shr 16 + (cksum and $ffff);
cksum:=cksum+(cksum shr 16);
result:=word(not cksum);
End;

procedure useage() ;
Begin
writeln('******************************************');
writeln('TCPPing');
writeln(#9'Written by Refdom');
writeln(#9'Email: refdom@263.net');
writeln('Useage: TCPPing.exe Target_ip Target_port ');
writeln('*******************************************');
End;
var
WSAData:TWSADATA;
sock:TSocket;
addr_in:SOCKADDR_IN ;
ipHeader: TIPHEADER;
tcpHeader:TTCPHEADER;
psdHeader:TPSDHEADER;

szSendBuf:array[0..59] of char;
flag:Bool;
rect,nTimeOver:integer;
tail:pchar;
Begin

fillchar(szSendBuf,sizeof(szSendBuf),0);
useage();
if ( paramcount<> 2) then
exit;
if WSAStartup(MAKEWORD(2,2),WSAData)<>0 then
Begin
writeln('WSAStartup Error!');
exit;
End;
sock:=WSASocket(AF_INET,SOCK_RAW,IPPROTO_RAW,nil,0,WSA_FLAG_OVERLAPPED);

if sock=INVALID_SOCKET then
Begin
writeln('Socket Setup Error!');
exit;
End;
flag:=true;
if setsockopt(sock,IPPROTO_IP, IP_HDRINCL,pchar(@flag),sizeof(flag))=SOCKET_ERROR then
Begin
writeln('setsockopt IP_HDRINCL error!');
exit;
End;
nTimeOver:=1000;
if setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, pchar(@nTimeOver),sizeof(nTimeOver))=SOCKET_ERROR then
Begin
writeln('setsockopt SO_SNDTIMEO error!');
exit;
End;
addr_in.sin_family:=AF_INET;
addr_in.sin_port:=htons(strtoint(paramstr(2)));
addr_in.sin_addr.S_addr:=inet_addr(pchar(paramstr(1)));

//
//
//填充IP首部
ipHeader.h_verlen:=integer(4 shl 4) or integer(sizeof(ipHeader) div sizeof(dword));
// ipHeader.tos=0;
ipHeader.total_len:=htons(sizeof(ipHeader)+sizeof(tcpHeader));
ipHeader.ident:=1;
ipHeader.frag_and_flags:=0;
ipHeader.ttl:=128;
ipHeader.proto:=IPPROTO_TCP;
ipHeader.checksum:=0;
ipHeader.sourceIP:=inet_addr('192.168.2.10');
ipHeader.destIP:=inet_addr(pchar(paramstr(1)));

//填充TCP首部
tcpHeader.th_dport:=htons(strtoint(paramstr(2)));
tcpHeader.th_sport:=htons(SOURCE_PORT); //源端口号
tcpHeader.th_seq:=htonl($12345678);
tcpHeader.th_ack:=0;
tcpHeader.th_lenres:=(sizeof(tcpHeader)div 4) shl 4;
tcpHeader.th_flag:=2;
tcpHeader.th_win:=htons(512);
tcpHeader.th_urp:=0;
tcpHeader.th_sum:=0;

psdHeader.saddr:=ipHeader.sourceIP;
psdHeader.daddr:=ipHeader.destIP;
psdHeader.mbz:=0;
psdHeader.ptcl:=IPPROTO_TCP;
psdHeader.tcpl:=htons(sizeof(tcpHeader));

//计算校验和
move(szSendBuf, psdHeader, sizeof(psdHeader));
move((pchar(@szSendBuf)+sizeof(psdHeader))^, tcpHeader, sizeof(tcpHeader));
tcpHeader.th_sum:=checksum(@szSendBuf,sizeof(psdHeader)+sizeof(tcpHeader));
move(szSendBuf, ipHeader, sizeof(ipHeader));
move((pchar(@szSendBuf)+sizeof(ipHeader))^, tcpHeader, sizeof(tcpHeader));
tail:= pchar(@szSendBuf)+sizeof(ipHeader)+sizeof(tcpHeader);
fillchar(tail[0],4, 0);
ipHeader.checksum:=checksum(@szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader));
move(szSendBuf, ipHeader, sizeof(ipHeader));
rect:=sendto(sock, szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader), 0, addr_in, sizeof(addr_in));
if (rect=SOCKET_ERROR) then
Begin
writeln('send error!:',WSAGetLastError());
exit;
End else
writeln('send ok!');
closesocket(sock);
WSACleanup();
End.

gub 2003-07-06
  • 打赏
  • 举报
回复
C header文件,找个软件转换一下试试。
好像Dr. Bob写过一个。
上网找一找。
Eastunfail 2003-07-06
  • 打赏
  • 举报
回复
昏~怎么不是Windows平台阿
看这就看得出来“#include <winsock2.h>”
Kakyo 2003-06-23
  • 打赏
  • 举报
回复
这段代码,好象不是用于 Windows 平台的,它用的 IO 头文件是 stdio.h,而不是 windows.h,并且多个地方用了printf语句,如果不是用于控制台程序,一定会出现运行错误。
建议去网上找找,应该有很多现成的用 Delphi 制作的窗口界面的 Ping 代码。很多书里也能找到。
ckwan 2003-06-21
  • 打赏
  • 举报
回复
要是楼主的问题还是没有解决,干脆自己研究C++和Delphi(OP),然后写一个CPP2OP转换/翻译器程序好了。
nmqhzh 2003-06-21
  • 打赏
  • 举报
回复
可爱的小豆子,
善良的恶鱼杀手(为什么只有78分信誉)
Eastunfail 2003-06-20
  • 打赏
  • 举报
回复
就是說對此無能為力,還是幫妳頂頂帖子~~~~
zlredfox 2003-06-20
  • 打赏
  • 举报
回复
ljmanage(过客) :
你好我是新来的(学生来的,没经验,上来学习的!)
"up一下算了"是什么意思啊?
softboysxp 2003-06-19
  • 打赏
  • 举报
回复
写成DLL就行了
hammer_shi 2003-06-19
  • 打赏
  • 举报
回复
o o
ly_liuyang 2003-06-19
  • 打赏
  • 举报
回复
写ping程序?有很多现成的了,down就是,自己改不是办法
布学无数 2003-06-18
  • 打赏
  • 举报
回复
楼主一定给分吗?
你先回答我,我一定翻译(其实也不长嘛~~~~:)
Eastunfail 2003-06-18
  • 打赏
  • 举报
回复
呵呵~~~还好,偶翻译过的有比这更长的。如果楼主真的需要,我也可以翻译
ffwin 2003-06-18
  • 打赏
  • 举报
回复
长倒是不长,不能生硬的译
ksaiy 2003-06-18
  • 打赏
  • 举报
回复
真的太长了~
你就说你要实现什么功能,大家还可以帮你~至少可以帮你找找代码~
这真的太长了~
ptpa 2003-05-25
  • 打赏
  • 举报
回复
我反以不了,长
Eastunfail 2003-05-25
  • 打赏
  • 举报
回复
既然这样,那我就不帮忙了~~~~
偶平日最喜欢看翻译贴子~~~~油水大,别人回帖几率小,哈
buff 2003-05-24
  • 打赏
  • 举报
回复
myling(阿德) :
你可以发给我一个看看,对这个有没有用
buffers@ls-softt.com
李_军 2003-05-24
  • 打赏
  • 举报
回复
!太长了点吧.......帮你up一下算了
bingbing2008 2003-05-24
  • 打赏
  • 举报
回复
帮你顶一下吧
加载更多回复(2)

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧