一个关于网络的问题

hwmweimin 2006-01-01 03:43:57
请问各位高手,我要我的程序知道本机是否连到了网上,我要怎样要写啊

请讲白一点我好莱啊
...全文
190 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwmweimin 2006-02-25
  • 打赏
  • 举报
回复
请各位高手看,这个急啊

请高手指点指点

我的MSN:hwmweimin@163.com

我的邮箱:hwmweimin@163.com

我的QQ:349908106

有时间可以连联一下,较留一下,问一下问题
Dickson 2006-02-25
  • 打赏
  • 举报
回复
加我QQ 18963652 或者群 11742273 12468202
hwmweimin 2006-02-23
  • 打赏
  • 举报
回复
我不知道有什么方法可以在我的程序里知道有两个IP啊,高手IamDeane(冷冷的风(Deane)) 你能说的明白一点吗?

用Winsock 控件要能侦查一次,我要它自己工作就不知道怎样了?

我用这种方法可以侦查网络但我不知它准不准,就是它侦查有点慢

Private Sub Timer3_Timer() 'Interval
If Len(Inet1.OpenURL("http://www.163.com")) <> 0 Then
MsgBox "连接" ‘这里可以写上我连接上要实现功能的代码
Else
'MsgBox "没连上"
Timer4.Enabled = True
End If
Timer3.Enabled = False
End Sub

Private Sub Timer4_Timer() 'Interval = 1
Timer3.Enabled = True
Timer4.Enabled = False
End Sub

请高手指点指点

我的MSN:hwmweimin@163.com

我的邮箱:hwmweimin@163.com

我的QQ:349908106

有时间可以连联一下,较留一下,问一下问题
IamDeane 2006-02-20
  • 打赏
  • 举报
回复
就是我拔号上网时,我的程序就知道我连上了网,就开始计时
那就判断是不是有两个IP就可以了,(在ADSL环境下,不能是多网卡哦)
IamDeane 2006-02-20
  • 打赏
  • 举报
回复
你确定是要在ADSL里使用么?如果是的话你还可以使用获得IP的办法,因为ADSL拨号是有两个IP的
如果不是的话就 Dickson(沧海冷心) 的办法你可以试试看啊,实在不行就多弄几个WINSOCK好了
多连几个站点比如百度啊,GOOGLE什么的
用WINSOCK控件
remoteHost设为"www.163.com"
remotePort为80
协议为tcp/ip
然后连接,winsock1.connect
hwmweimin 2006-02-06
  • 打赏
  • 举报
回复
高手这个急得很啊
hwmweimin 2006-02-06
  • 打赏
  • 举报
回复
请csdn里的高手看看这个问题应该怎样解决啊,我真的十分苦脑啊
hwmweimin 2006-02-05
  • 打赏
  • 举报
回复
那这个问题我该怎样解决好呢?请个位高手指点指点啊

我真的好迷惑啊
qkaibo 2006-01-20
  • 打赏
  • 举报
回复
这个问题有两面

1。比如你连在局域网上 但是上不了外网
2。你的机器真的没连接任何网络
IamDeane 2006-01-19
  • 打赏
  • 举报
回复
连网站的办法应该对任何的是否连网检测都是有效的
楼主可以都连几个网站防止出现不可预见的情况
只要一个连上就证明连上了
hwmweimin 2006-01-19
  • 打赏
  • 举报
回复
连网站是可靠,但我不上这个网站,或我只连上,但我没有打开IE上网,哪还行吗?

其实我就是想对于ADSL检查,那怎样写啊
anticlimax 2006-01-19
  • 打赏
  • 举报
回复
remMsg(0) = "This is the easiest way to check connection. Checking registry value of System\CurrentControlSet\Services\RemoteAccess from HKEY_LOCAL_MACHINE. Using RegOpenKey function from advapi32.dll. Unfortunately, checking is ONLY for MODEM connection. If you are connecting to Internet via Local Area Network (LAN), this method return False even if you are connected"
这种仅能检查你使用modem拨号是否上网,对于ADSL等不行
remMsg(1) = "This method use InternetGetConnectedStateEx function from wininet.dll. In addition, you can receive some more information about connection - Type of connection (LAN/Modem), Using of Proxy, RAS installing, OnLine/OffLine. It's work fine, but there is one problem. If your computer is in Local Area Network but you are connecting to Internet via modem, this method always returns True, in case you are connecting to Internet or not"
这种的局限是你你又处于局域网,比如你的机器是作为服务器,还有另外的机子通过你的机子上网,总是说你在上网
remMsg(2) = "This method use direct ping to some Internet address (URL) and checking for connection errors. Now it use http:/www.yahoo.com. It's not so quickly, as previous two, but this method is the most reliable"
这种是最可靠的,通过连一个网站,试试
hwmweimin 2006-01-19
  • 打赏
  • 举报
回复
楼上的这位 anticlimax() 高手能不能解释一下啊,真的不太明白啊,
谢谢
hwmweimin 2006-01-16
  • 打赏
  • 举报
回复
就是我拔号上网时,我的程序就知道我连上了网,就开始计时
Dickson 2006-01-02
  • 打赏
  • 举报
回复
那个太难啦~~
用WINSOCK控件
remoteHost设为"www.163.com"
remotePort为80
协议为tcp/ip
然后连接,winsock1.connect
如果连接成功就连上了
hwmweimin 2006-01-02
  • 打赏
  • 举报
回复
第一个回复太难了
我想我的程序无论是否有打开网页,只要网络就来个提示
anticlimax 2006-01-01
  • 打赏
  • 举报
回复
拿去吧,别说英文也菜哟

'Working with registry declarations and constants
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Const ERROR_SUCCESS = 0&
Private Const APINULL = 0&
Private Const HKEY_LOCAL_MACHINE = &H80000002
'Working with wininet.dll declarations and constants
Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" Alias "InternetGetConnectedStateExA" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Long, ByVal dwReserved As Long) As Long 'Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long 'this function used with IE4
'Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long 'this function used with IE4
Private Const INTERNET_CONNECTION_MODEM = &H1&
Private Const INTERNET_CONNECTION_LAN = &H2&
Private Const INTERNET_CONNECTION_PROXY = &H4&
Private Const INTERNET_RAS_INSTALLED = &H10&
Private Const INTERNET_CONNECTION_OFFLINE = &H20&
Private Const INTERNET_CONNECTION_CONFIGURED = &H40&
'Declares for direct ping
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInet As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Long

Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Dim checkType As Integer
Dim remMsg(2) As String

Private Sub Command1_Click()
Select Case checkType
Case 0
CheckConnection1
Case 1
CheckConnection2
Case 2
CheckConnection3
Case Else
End Select
End Sub

Private Sub Form_Load()
remMsg(0) = "This is the easiest way to check connection. Checking registry value of System\CurrentControlSet\Services\RemoteAccess from HKEY_LOCAL_MACHINE. Using RegOpenKey function from advapi32.dll. Unfortunately, checking is ONLY for MODEM connection. If you are connecting to Internet via Local Area Network (LAN), this method return False even if you are connected"
remMsg(1) = "This method use InternetGetConnectedStateEx function from wininet.dll. In addition, you can receive some more information about connection - Type of connection (LAN/Modem), Using of Proxy, RAS installing, OnLine/OffLine. It's work fine, but there is one problem. If your computer is in Local Area Network but you are connecting to Internet via modem, this method always returns True, in case you are connecting to Internet or not"
remMsg(2) = "This method use direct ping to some Internet address (URL) and checking for connection errors. Now it use http:/www.yahoo.com. It's not so quickly, as previous two, but this method is the most reliable"
Option1(0).Value = True
Option1(0).Caption = "Using registry"
Option1(1).Caption = "Using InternetGetConnectedStateEx"
Option1(2).Caption = "Using direct ping to www.yahoo.com"
Text1 = remMsg(0)
End Sub

Private Sub Option1_Click(Index As Integer)
checkType = Index
Text1 = remMsg(Index)
End Sub
'This part of code is from http://www.VB-world.net with my corrections
Private Sub CheckConnection1()
Dim ReturnCode As Long
Dim hKey As Long
Dim lpSubKey As String
Dim phkResult As Long
Dim lpValueName As String
Dim lpReserved As Long
Dim lpType As Long
Dim lpData As Long
Dim lpcbData As Long
lpSubKey = "System\CurrentControlSet\Services\RemoteAccess" & Chr$(0)
ReturnCode = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, phkResult)
If ReturnCode = ERROR_SUCCESS Then
hKey = phkResult
lpValueName = "Remote Connection"
lpReserved = APINULL
lpType = APINULL
lpData = APINULL
lpcbData = APINULL
ReturnCode = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, ByVal lpData, lpcbData)
lpcbData = Len(lpData)
ReturnCode = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData)
If ReturnCode = ERROR_SUCCESS Then
If lpData = 0 Then
MsgBox "Your computer is not connected to Internet via modem", vbInformation, "Checing connection"
Else
MsgBox "Your computer is connected to Internet via modem", vbInformation, "Checing connection"
End If
Else
MsgBox "Your computer is not connected to Internet via modem, but it can be connected via LAN", vbInformation, "Checing connection"
End If
End If
RegCloseKey (hKey)
End Sub

Private Sub CheckConnection2(Optional ByRef ConnectionInfo As Long, Optional ByRef sConnectionName As String)
Dim dwFlags As Long
Dim sNameBuf As String, msg As String
Dim lPos As Long
sNameBuf = String$(513, 0)
If InternetGetConnectedStateEx(dwFlags, sNameBuf, 512, 0&) Then
lPos = InStr(sNameBuf, vbNullChar)
If lPos > 0 Then
sConnectionName = Left$(sNameBuf, lPos - 1)
Else
sConnectionName = ""
End If
msg = "Your computer is connected to Internet" & vbCrLf & "Connection Name: " & sConnectionName
If (dwFlags And INTERNET_CONNECTION_LAN) Then
msg = msg & vbCrLf & "Connection use LAN"
ElseIf lFlags And INTERNET_CONNECTION_MODEM Then
msg = msg & vbCrLf & "Connection use modem"
End If
If lFlags And INTERNET_CONNECTION_PROXY Then msg = msg & vbCrLf & "Connection use Proxy"
If lFlags And INTERNET_RAS_INSTALLED Then
msg = msg & vbCrLf & "RAS INSTALLED"
Else
msg = msg & vbCrLf & "RAS NOT INSTALLED"
End If
If lFlags And INTERNET_CONNECTION_OFFLINE Then
msg = msg & vbCrLf & "You are OFFLINE"
Else
msg = msg & vbCrLf & "You are ONLINE"
End If
If lFlags And INTERNET_CONNECTION_CONFIGURED Then
msg = msg & vbCrLf & "Your connection is Configured"
Else
msg = msg & vbCrLf & "Your connection is not Configured"
End If
Else
msg = "Your computer is NOT connected to Internet"
End If
MsgBox msg, vbInformation, "Checking connection"
End Sub

Private Sub CheckConnection3()
Dim sTmp As String
Dim hInet As Long
Dim hUrl As Long
Dim Flags As Long
Dim url As Variant
hInet = InternetOpen(App.Title, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0&)
sTmp = Me.Caption
Me.Caption = "Checking connection with www.yahoo.com..."
If hInet Then
Flags = INTERNET_FLAG_KEEP_CONNECTION Or INTERNET_FLAG_NO_CACHE_WRITE Or INTERNET_FLAG_RELOAD
hUrl = InternetOpenUrl(hInet, "http://www.yahoo.com", vbNullString, 0, Flags, 0)
If hUrl Then
MsgBox "Your computer is connected to Internet", vbInformation, "Checing connection"
Call InternetCloseHandle(hUrl)
Else
MsgBox "Your computer is not connected to Internet", vbInformation, "Checing connection"
End If
End If
Call InternetCloseHandle(hInet)
Me.Caption = sTmp
End Sub

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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