哎,我是不是不是学编程的料啊?各位高手救命啊!(100分)

  • 主题发起人 主题发起人 jingtao
  • 开始时间 开始时间
J

jingtao

Unregistered / Unconfirmed
GUEST, unregistred user!
电脑是局部网上网,不是主机。如何编程知道现在已经上INTERNET网?还有,如何查上网后
自己的动态IP?(注意:是局部网上网)。哎,我是不是很笨啊?各位高手救命啊!
 
1 、ping 163.net
2、ipconfig
 
1、
unit ICMP;
interface
uses winsock,windows,SysUtils;
function ping(dest:string):boolean;
implementation
type
pipoptioninformation=^tipoptioninformation;
tipoptioninformation=packed record
ttl:byte;
tos:byte;
flags:byte;
optionssize:byte;
optiondata:pchar;
end;
picmpechoreply=^ticmpechoreply;
ticmpechoreply=packed record
address:dword;
status:dword;
rtt:dword;
datasize:word;
reserved:word;
data:pointer;
options:tipoptioninformation;
end;
ticmpcreatfile=function :thandle;stdcall;
ticmpclosehandle=function (imcphandle:thandle):boolean;stdcall;
ticmpsendecho=function (icmphandle:thandle;
destinationaddress:dword;
requestdata:pointer;
requestsize:word;
requestoptions:pipoptioninformation;
replybuffer:pointer;
replysize:dword;
timeout:dword)
:dword;stdcall;
var
hicmpdll:hmodule;
hicmp:thandle;
icmpcreatfile:ticmpcreatfile;
icmpclosehandle:ticmpclosehandle;
icmpsendecho:ticmpsendecho;
function ping(dest:string):boolean;
var
ipopt:tipoptioninformation;
fipaddress:dword;
preqdata,prevdata:pchar;
pipe:picmpechoreply;
fsize:dword;
mystring:string;
ftimeout:dword;
buffersize:dword;
re:dword;
begin
result:=false;
fipaddress:=inet_addr(pchar(dest));
fsize:=10;
buffersize:=sizeof(ticmpechoreply)+fsize;
getmem(prevdata,fsize);
getmem(pipe,buffersize);
fillchar(pipe^,sizeof(pipe^),0);
pipe^.data:=prevdata;
mystring:='DCDBDYHAHA';
preqdata:=pchar(mystring);
fillchar(ipopt,sizeof(ipopt),0);
ipopt.ttl:=128;
ftimeout:=100;
re:=icmpsendecho(hicmp,fipaddress,preqdata,
length(mystring),@ipopt,pipe,buffersize,ftimeout);
if re<>0 THEN
result:=preqdata^=pipe^.options.optiondata^;
if prevdata<>nil then freemem(prevdata);
if pipe<>nil then freemem(pipe);
end;

begin
hicmpdll:=0;
hicmpdll:=loadlibrary('icmp.dll');
@icmpcreatfile:=getprocaddress(hicmpdll,'IcmpCreateFile');
@icmpclosehandle:=getprocaddress(hicmpdll,'IcmpCloseHandle');
@icmpsendecho:=getprocaddress(hicmpdll,'IcmpSendEcho');
hicmp:=icmpcreatfile;
end.
2、
unit tcp_api4;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
winsock;
const
err_netnotready=$ffff;
err_range=10000;
type
tAddr=array[1..4] of byte;
tsin=TSOCKADDRin;
const
all_send=0;
single_send=1;
divcode='/';
var
gs,gr,grtemp:tsocket;
gsin:tsin;
gtd,gtd_recv:taddr;
gmy_name:string;
gsend_type:integer;
grecv_type:integer;
gi:integer;
socs:array[1..100] of tsocket;
function bind_socket(var s:tsocket;var sin:tsockaddrin):integer;
function get_socket(var s:tsocket;sock_type:integer):integer;
function get_dest_addr(name:string;var sin:TSOCKADDRin;var td:taddr):integer;
function get_host_name:string;
function get_host_addr(var sin:TSOCKADDRin;var td:taddr):integer;
function get_my_addr:taddr;
function connect_des(s:tsocket;name:string;var sin:TSOCKADDRin;var td:taddr):integer;
function sendmsgto(client:string;msg:string):integer;
function recvmsgfrom(VAR client:string;var msg:string):integer;
function check_peername(client:string):integer;
implementation
function bind_socket(var s:tsocket;var sin:tsockaddrin):integer;
var
i,res:integer;
alport:integer;
begin
alport:=IPPORT_RESERVED;
alport:=1033;
for i:=1 to IPPORT_RESERVED div 2 do
begin
sin.sin_port:=htons(alport);
RES:=BIND(S,SIN,SIZEOF(SIN));
if res=0 then break;
alport:=alport-1;
end;
gi:=alport;
if res<>0 then res:=wsagetlasterror;
bind_socket:=res;
for i:=1034 to 1134 do
begin
sin.sin_port:=htons(i);
res:=BIND(socs[i-1033],SIN,SIZEOF(SIN));
end;
end;
function bind_sockettemp(var s:tsocket;var sin:tsockaddrin):integer;
var
i,res:integer;
alport:integer;
begin
alport:=IPPORT_RESERVED;
alport:=1066;
for i:=1 to IPPORT_RESERVED div 2 do
begin
sin.sin_port:=htons(alport);
RES:=BIND(S,SIN,SIZEOF(SIN));
if res=0 then break;
alport:=alport-1;
end;
gi:=alport;
if res<>0 then res:=wsagetlasterror;
result:=res;
end;

function get_socket(var s:tsocket;sock_type:integer):integer;
var
af,typ,procol:integer;
wsdata:twsadata;
wver:word;
res:integer;
begin
af:=PF_INET;
TYP:=sock_type;
PROCOL:=0;
wver:=makeword(1,1);
res:=wsastartup(wver,wsdata);
s:=socket(af,typ,procol);
get_socket:=res;
end;
function get_dest_addr(name:string;var sin:TSOCKADDRin;var td:taddr):integer;
var
i,alport:integer;
st:string;
hp:phostent;
server:array[1..80] of char;
begin
fillchar(td,sizeof(td),0);
i:=sizeof(server);
fillchar(server,sizeof(server),0);
st:=name;
move(st[1],server,length(st));
hp:=gethostbyname(@server);
if hp=nil then
begin
get_dest_addr:=-1;
exit;
{halt;}
end;
move(hp^.h_addr_list^,sin.sin_addr.s_addr,hp^.h_length);
SIN.SA_FAMILY:=hp^.h_addrtype;
alport:=IPPORT_RESERVED;
sin.sin_port:=htons(alport);
sin.sin_addr.s_un_b.s_b1:=hp^.h_addr_list^[0];
sin.sin_addr.s_un_b.s_b2:=hp^.h_addr_list^[1];
sin.sin_addr.s_un_b.s_b3:=hp^.h_addr_list^[2];
sin.sin_addr.s_un_b.s_b4:=hp^.h_addr_list^[3];
move(hp^.h_addr_list^[0],td[1],4);
end;
function get_host_name:string;
var
i:integer;
hp:phostent;
server:array[1..80] of char;
begin
i:=sizeof(server);
gethostname(@server,i);
hp:=gethostbyname(@server);
get_host_name:=hp^.h_name;
end;
function get_host_addr(var sin:TSOCKADDRin;var td:taddr):integer;
var
i,alport:integer;
st:string;
hp:phostent;
server:array[1..80] of char;
begin
fillchar(td,sizeof(td),0);
i:=sizeof(server);
gethostname(@server,i);
hp:=gethostbyname(@server);
move(hp^.h_addr_list^,sin.sin_addr.s_addr,hp^.h_length);
SIN.SA_FAMILY:=hp^.h_addrtype;
alport:=IPPORT_RESERVED;
sin.sin_port:=htons(alport);
sin.sin_addr.s_un_b.s_b1:=hp^.h_addr_list^[0];
sin.sin_addr.s_un_b.s_b2:=hp^.h_addr_list^[1];
sin.sin_addr.s_un_b.s_b3:=hp^.h_addr_list^[2];
sin.sin_addr.s_un_b.s_b4:=hp^.h_addr_list^[3];
move(hp^.h_addr_list^[0],td[1],4);
end;

function get_my_addr:taddr;
VAR
s:tsocket;
RES:INTEGER;
td:taddr;
SIN:TSOCKADDRin;
begin
get_socket(gs,SOCK_dgram);
get_host_addr(sin,td);
bind_socket(gs,sin);
move(sin.sin_addr.s_addr,result,4);
{ closesocket(gs);}
{******}
end;
function connect_des(s:tsocket;name:string;var sin:TSOCKADDRin;var td:taddr):integer;
VAR
i,res:INTEGER;
st:string;
alport:integer;
begin
get_dest_addr(name,sin,td);
RES:=connect(S,SIN,SIZEOF(SIN));
if res=0
then
connect_des:=res
else
connect_des:=wsagetlasterror;
end;
function sendmsgto(client:string;msg:string):integer;
var
st:string;
i,res:integer;
sin:tsockaddrin;
td:taddr;
ts:tsocket;
t1:double;
buf:array[1..10] of byte;
l:integer;

begin
st:=gmy_name+divcode+msg;
{ get_socket(ts,sock_dgram);
get_host_addr(sin,td);
bind_socket(ts,sin);}
get_dest_addr(client,sin,td);
{ if gtd_recv[1]=0 then}

{ else
begin
sin.sin_addr.s_un_b.s_b1:=char(GTD_RECV[1]);
sin.sin_addr.s_un_b.s_b2:=char(GTD_RECV[2]);
sin.sin_addr.s_un_b.s_b3:=char(GTD_RECV[3]);
sin.sin_addr.s_un_b.s_b4:=char(GTD_RECV[4]);
end;}
{ if gsend_type=all_send then
begin
st:=char(255)+st;}
{ sin.sin_addr.s_un_b.s_b1:=char($ff);
sin.sin_addr.s_un_b.s_b2:=char($ff);
sin.sin_addr.s_un_b.s_b3:=char($ff);
sin.sin_addr.s_un_b.s_b4:=char($ff);
{ end
else
st:=char(0)+st;}
{ fillchar(buf,sizeof(buf),0);
buf[1]:=1;l:=sizeof(buf);
res:=setsockopt(ts,sol_socket,SO_BROADCAST,
pchar(@buf),l);}
res:=sendto(gr,st[1],length(st),0,sin,sizeof(sin));
if res=-1 then
begin
res:=wsagetlasterror;
{closesocket(ts);}
halt;
end;
t1:=time;
{ repeat
until (time-t1)>0.000001574;}
sendmsgto:=res;
{ closesocket(ts);}
end;
procedure init_Sock;
var
buf:array[1..10] of byte;
res,l,i:integer;
begin
fillchar(buf,sizeof(buf),0);
buf[1]:=1;
buf[2]:=1;
l:=sizeof(buf);
res:=setsockopt(GR,sol_socket,SO_rcvtimeo,
pchar(@buf),l);
fillchar(buf,sizeof(buf),0);
buf[1]:=1;l:=sizeof(buf);
res:=setsockopt(GR,sol_socket,SO_BROADCAST,
pchar(@buf),l);
for i:=1 to 100 do
res:=setsockopt(socs,sol_socket,SO_rcvtimeo,
pchar(@buf),l);

res:=wsagetlasterror;
end;
function recvmsgfrom(VAR client:string;var msg:string):integer;
var
st:string;
t,i,j,res:integer;
sin:tsockaddrin;
td:taddr;

hp:phostent;
ts:tsocket;
t1:double;
begin
{ td:=get_my_addr;}
{closesocket(gs);}
{ get_my_addr;}
{ get_socket(ts,sock_dgram);}
{ get_host_addr(sin,td);

bind_socket(gs,sin);
{ get_my_addr;}
{ get_host_addr(sin,td);
bind_socket(gs,sin);}
{get_dest_addr(client,sin,td);}
{ closesocket(gs);
{ wsacleanup;}
{ td:=get_my_addr;}
{ fillchar(sin,sizeof(sin),0);}

i:=1;j:=sizeof(sin);t:=0;
{ fillchar(buf,sizeof(buf),0);
buf[1]:=100;
l:=sizeof(buf);
res:=setsockopt(GR,sol_socket,SO_rcvtimeo,
pchar(@buf),l);
res:=wsagetlasterror;
inc(t);}
SETLENGTH(ST,3000);
{ fillchar(sin,sizeof(sin),0);}
{ WSACancelBlockingCall;}
{ t1:=time;
repeat
until (time-t1)>0.0000001574*5;}
{ closesocket(gr);
wsacleanup;
GET_SOCKet(GR,sock_dgram);
init_sock;
get_host_addr(Gsin,Gtd);
bind_socket(GR,Gsin);}

fillchar(gtd_recv,sizeof(gtd_recv),0);
sin:=gsin;
res:=recvfrom(GR,ST[1],3000,0,sin,j);
IF (RES<3000) AND (RES>0) THEN
BEGIN
{ if st[1]=char(255)
then
grecv_type:=all_send
else
grecv_type:=single_send;}
SETLENGTH(ST,RES);
move(sin.sin_addr.s_addr,gtd_recv,4);
{ hp:=gethostbyaddr(@td,10,pf_inet);}
CLIENT:=COPY(ST,1,POS(divcode,ST)-1);
st:=copy(st,pos(divcode,st)+1,length(st)-length(client)-1);
end;
if res=-1 then
begin
res:=wsagetlasterror;
recvmsgfrom:=res;
exit;
end;
msg:=st;


recvmsgfrom:=length(st);
end;
function check_peername(client:string):integer;
begin
check_peername:=get_dest_addr(client,gsin,gtd);
end;
var
i:byte;
begin
for i:=1 to 100 do
get_socket(socs,sock_dgram);
GET_SOCKet(GR,sock_dgram);
GET_SOCKet(grtemp,sock_dgram);
get_host_addr(Gsin,Gtd);
bind_socket(GR,Gsin);
bind_sockettemp(GRtemp,Gsin);
init_sock;
{ gtd:=get_my_addr;}
gmy_name:=get_host_name;
fillchar(gtd_recv,sizeof(gtd_recv),0);
{closesocket(gs);
wsacleanup;}

end.
自己找一段回去试一下吧
 
>>面条
我是说用程序来是实现啊.在程序中调用如果当时服务器忙也没有办法啊
>>netwind
这东西我知道,但不是主机,没有经过ISP接入商,好像返回来的不是动态啊
 
局域网上网有两种,一种是动态IP(主机分派)
一种是静态ip,类似192.168.x.x

第一个问题简单,向DNS(非主机的)发送请求,若有响应即通;

第二个比较麻烦,分两种情况讨论应可解决!
 
>>zyy04:
第一个问题简单,向DNS(非主机的)发送请求,若有响应即通;

怎么搞啊?
 
我是局域网连入的:怎么确定 IP 呀

====================================
C:/WINDOWS>ipconfig

Windows 98 IP Configuration

0 Ethernet adapter :

IP Address. . . . . . . . . : 192.168.1.194
Subnet Mask . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . :
 
>>zwhc:那是静态的IP和网关啊。另外,我要在程序中实现啊
 
我有四个方法
1。Checking registry value
2。use InternetGetConnectedStateEx function from wininet.dll.
In addition, you can receive some more information about connection。
3。ping www.gislab.ecnu.edu.cn

4.shellexecute(Handle,nil,'www.gislab.ecnu.edu.cn,nil,nil,SW_SHOWNORMAL);
让IE告述你 满意吗,我没分啦。
 
我有源程序VB
 
>>ye peng
你说的方法我也知道,但好象只对主机有用而已。
另外,我要用程序实现。总不能让用户打开网页来判断啊,否则,人家谁要你的
程序啊。答了就有分给你了:)
>>zwhc:那是静态的IP和子网掩码啊。
 
>>电脑是局部网上网,不是主机
局域网?通过网关?那么动态IP是指什么?
 
<b>jingtao</b>:
我和你一样笨 ---- 有伴儿了吧!
 
静态的IP和子网掩码,请把你现在的网络的详细情况告述我,
是computer->交换机->路由器->Internet,还是简单的computer->NT(由NT代理)拨号->Internet
前者没问题,看样子你的网络是后者,讲清楚了才行。关于动态IP,静态IP,子网掩码,网关
等是网络的基本问题。
 
>>温柔一刀
通过一台主机啊
》cat.yy
您还笨?
》ye peng
是computer->交换机->路由器->Internet

 
检测当前是上否上Internet
uses registry
function HasInternetConnect:boolean;
var reg:Tregistry;
dat:array[0..3] of char;
begin
reg:=tregistry.create;
reg.rootkey:=Hkey_Local_Machine;
reg.openkey('Syetem/currentcontrolset/services/remoteaccess', false);
reg.readbinarydata('remote connection',dat,4);
reg.free;
result:=(dat[0]=#1);
end;
但对局部网没有用啊
 
jingtao,我比你笨多了

吾一直关注
(将问题提前)
 
我觉得像你这种接入方式,如果绕过os直接对硬件编程应该要简单一些,
参考你的路由器或交换机的编程说明,相信你会有答案的。
 
你这种连接还是用ping internet上的主机比较简单,
ics控件组有 TPing ,用起来非常简单.

 
后退
顶部