怎么样检测本机连接上网络(Internet)

Xhunter 2004-01-01 05:38:51

我开了一条线程每2秒 Ping 主机一次,用 gethostbyname()

  发觉这个函数不是太好用,有些时间我都把网线拔掉了,它还返回连接上的信息以下是代码,大家帮我看一下

//线程函数中
DB->ISCloseNet=true;
while(DB->ISCloseNet)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,0),&wsaData);

Sleep(2000);

struct hostent *hp=gethostbyname(DB->TestHost.c_str());
if(!hp)
{
//连接失败
int ErrCode=WSAGetLastError();
}
else
//连接成功
WSACleanup();
}
...全文
226 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fronm 2004-01-15
  • 打赏
  • 举报
回复
拔网线。我试过socket和WinInet.DLL的InternetGetConnectedState(&dwFlag,0)函数不可靠。---…我没有测试成功!^_^
wide288 2004-01-14
  • 打赏
  • 举报
回复
ping icmp 不行吗?
yunuo2010000 2004-01-08
  • 打赏
  • 举报
回复
学习
goneaway1981 2004-01-05
  • 打赏
  • 举报
回复
mark
gzlcd 2004-01-05
  • 打赏
  • 举报
回复
用WinInet不太可靠,有时将网线拔掉还显示在线,函数返回的不是实时的状态。建议用Socket。
yunuo2010000 2004-01-05
  • 打赏
  • 举报
回复
学习
esiedull 2004-01-05
  • 打赏
  • 举报
回复
就拔网线来说。我试过socket也不可靠。
goldpony 2004-01-05
  • 打赏
  • 举报
回复
贴一段代码给你,检测你是否连在网上.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Psock, NMsmtp;
const
WM_LOGIN =WM_USER+100;
Type
TWM = record
Msg: WORD;
wParam:Word;
lParam:DWORD;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label4: TLabel;
Edit4: TEdit;
Label5: TLabel;
Label3: TLabel;
ListBox1: TListBox;
Label6: TLabel;
Edit5: TEdit;
Label7: TLabel;
Memo1: TMemo;
StatusBar1: TStatusBar;
btnSend: TButton;
Edit6: TEdit;
Button1: TButton;
NMSMTP1: TNMSMTP;
procedure btnSendClick(Sender: TObject);
procedure NMSMTP1Connect(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure NMSMTP1Disconnect(Sender: TObject);
procedure NMSMTP1ConnectionFailed(Sender: TObject);
procedure NMSMTP1HostResolved(Sender: TComponent);
procedure NMSMTP1InvalidHost(var Handled: Boolean);
procedure NMSMTP1SendStart(Sender: TObject);
procedure NMSMTP1Success(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ListBox1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
procedure WMLOGIN(var Msg:TWM);Message WM_LOGIN;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

procedure TForm1.WMLOGIN(var Msg: TWM);
var
i:integer;
begin
//Send mail
//fix part of mail
NMSMTP1.PostMessage.FromAddress := Edit4.Text;
NMSMTP1.PostMessage.FromName := Edit3.Text;
NMSMTP1.PostMessage.Subject := Edit5.Text;
//clear old info
NMSMTP1.PostMessage.ToAddress.Clear;
//send a mail back
//NMSMTP1.PostMessage.ToAddress.Add(Edit4.Text);
NMSMTP1.PostMessage.Body.Assign(Memo1.Lines);

//set mail list
//NMSMTP1.PostMessage.ToBlindCarbonCopy.Clear;
for i:=0 to ListBox1.Items.Count -1 do
begin
NMSMTP1.PostMessage.ToAddress.Add(
ListBox1.Items.Strings[i]);
end;
NMSMTP1.SendMail;
NMSMTP1.Disconnect;

end;

procedure TForm1.btnSendClick(Sender: TObject);
begin
//set server info
NMSMTP1.Host := Edit1.Text;
NMSMTP1.Port := StrToInt(Edit2.Text);
NMSMTP1.UserID:=Edit3.Text;
//update view
StatusBar1.SimpleText:='Connecting...';
StatusBar1.Update;
//connect
NMSMTP1.Connect;

end;

procedure TForm1.NMSMTP1Connect(Sender: TObject);
begin
//update view
StatusBar1.SimpleText:='Connected';
StatusBar1.Update;
SendMessage(Handle,WM_LOGIN,0,0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//server info
Edit1.Text:='10.13.101.10';
Edit2.Text:='25';
//User info
Edit3.Text:='Bowman';
Edit4.Text:='MatthewBowman@21cn.com';
//Empty Other
Edit5.Text:='';
Edit6.Text:='';
Memo1.Text:='';

end;

procedure TForm1.NMSMTP1Disconnect(Sender: TObject);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := 'Disconnected from server';

end;

procedure TForm1.NMSMTP1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;

procedure TForm1.NMSMTP1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'HostResolved';
end;

procedure TForm1.NMSMTP1InvalidHost(var Handled: Boolean);
begin
StatusBar1.SimpleText := 'InvalidHost';
end;

procedure TForm1.NMSMTP1SendStart(Sender: TObject);
begin
StatusBar1.SimpleText := 'Sending Envelop...';
end;

procedure TForm1.NMSMTP1Success(Sender: TObject);
begin
StatusBar1.SimpleText := 'Success';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//add list
ListBox1.Items.Add(Edit6.Text);
end;

procedure TForm1.ListBox1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
idx:integer;
begin
if ListBox1.ItemIndex=-1 then exit;
if Key=VK_DELETE then
begin
idx:=ListBox1.ItemIndex;
ListBox1.Items.Delete(idx);
end;
end;

end.
netsys2 2004-01-04
  • 打赏
  • 举报
回复
网络连接检查:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
/*WinInet.DLL的InternetGetConnectedState(&dwFlag,0)函数:

注意:为使用该函数,须在项目文件中加入:
USELIB("WinInet.LIB")
#include <wininet.h>
*/
DWORD dwFlag;
bool netLink = InternetGetConnectedState(&dwFlag,0);

if( netLink )
Label4->Caption="在线"; //
else Label4->Caption="未在线";

// 网络配置
if(dwFlag & INTERNET_CONNECTION_MODEM) Label1->Caption="Yes"; //MODEM连接
else Label1->Caption="No";
if(dwFlag & INTERNET_CONNECTION_LAN) Label2->Caption="Yes"; //LAN连接
else Label2->Caption="No";
if(dwFlag & INTERNET_CONNECTION_PROXY) Label3->Caption="Yes"; //代理连接
else Label3->Caption="No";
if(dwFlag & INTERNET_CONNECTION_OFFLINE) Label5->Caption="Yes";//离线。
else Label5->Caption="No";
if(dwFlag & INTERNET_CONNECTION_CONFIGURED) Label7->Caption="Yes";
else Label7->Caption="No";
}
yphy 2004-01-04
  • 打赏
  • 举报
回复
up

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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