如何判断网络连接是否有效?

mostneed 2005-12-10 11:05:35
比如网上邻居电脑A上共享了一个目录FolderA,并且映射到本机Z盘

通过拔电脑A的网线,或者取消共享的方式让FolderA共享变成无效.

在本机上如何判断这个FolderA是有效,可以访问的?
使用什么api?

十万火急!!!
...全文
578 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kudeet 2005-12-13
  • 打赏
  • 举报
回复
在Windows 2000 环境下,有System Event Notification Services (SENS)函数可以判断当前系统的连接状态。使用SIO_ADDRESS_LIST_CHANGE和SIO_ROUTING_INTERFACE_CHANGE并调用WSAIoctl()。这是两种可能的方式。应用程序能通过调用WSAIoctl() 和SIO_ADDRESS_LIST_CHANGE注册地址改变的notification。 当网线被拔掉,这个notification就可以被一个应用程序收到。 同时,IsNetworkAlive() 应该是另一种简单的方法,但是当它用在LAN时有一些时间反应的问题。可能要用几分钟的时间去更新在LAN中连接的状态。

在非Windows2000的环境下,没有即刻反应的方法去判断网线的插拔。nternetGetConnectedState() 的超时判断时一种基本的方法。当用户的机器在“Internet 选项”的连接里设置了“从不进行拨号连接”(Never Dial a connect)时,用InternetGetConnectedState()函数将总返回FALSE
mostneed 2005-12-13
  • 打赏
  • 举报
回复
to oyljerry(【勇敢的心】→ , arvid_gs(west)

这个函数不是实时的, 如果在拔网线或者取消共享后立刻调用, 返回的连接是有效的.
打雷啦的专栏 2005-12-13
  • 打赏
  • 举报
回复
去打开文件看看,不就知道了
arvid_gs 2005-12-12
  • 打赏
  • 举报
回复
CHAR szDeviceName[80];
DWORD dwResult, cchBuff = sizeof(szDeviceName);

// Call the WNetGetConnection function.
//
dwResult = WNetGetConnection("z:",
(LPSTR) szDeviceName,
&cchBuff);

switch (dwResult)
{
//
// Print the connection name or process errors.
//
case NO_ERROR:
TextOut(hdc, 10, 10, (LPSTR) szDeviceName,
lstrlen((LPSTR) szDeviceName));
break;
//
// The device is not a redirected device.
//
case ERROR_NOT_CONNECTED:
TextOut(hdc, 10, 10, "Device z: not connected.", 24);
//
// The device is not currently connected,
// but it is a persistent connection.
//
case ERROR_CONNECTION_UNAVAIL:
TextOut(hdc, 10, 10, "Connection unavailable.", 23);
//
// Call an application-defined error handler.
//
default:
NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetGetConnection");
return FALSE;
}
lifengice0706 2005-12-12
  • 打赏
  • 举报
回复
200分有被星星们抢走了!
oyljerry 2005-12-11
  • 打赏
  • 举报
回复
Adding a Network Connection
Windows NT/Windows 2000

To make a connection to a network resource described by a NETRESOURCE structure, an application can call the WNetAddConnection2, the WNetAddConnection3, or the WNetUseConnection function. The following example demonstrates use of the WNetAddConnection2 function.

The code sample calls the WNetAddConnection2 function, specifying that the system should update the user's profile with the information, creating a "remembered" or persistent connection. The sample calls an application-defined error handler to process errors, and the TextOut function for printing.

DWORD dwResult;
NETRESOURCE nr;
//
// Call the WNetAddConnection2 function to make the connection,
// specifying a persistent connection.
//
dwResult = WNetAddConnection2(&nr, // NETRESOURCE from enumeration
(LPSTR) NULL, // no password
(LPSTR) NULL, // logged-in user
CONNECT_UPDATE_PROFILE); // update profile with connect information

// Process errors.
// The local device is already connected to a network resource.
//
if (dwResult == ERROR_ALREADY_ASSIGNED)
{
TextOut(hdc, 10, 10, "Already connected to specified resource.", 40);
return FALSE;
}

// An entry for the local device already exists in the user profile.
//
else if (dwResult == ERROR_DEVICE_ALREADY_REMEMBERED)
{
TextOut(hdc, 10, 10,
"Attempted reassignment of remembered device.", 44);
return FALSE;
}
else if(dwResult != NO_ERROR)
{
//
// Call an application-defined error handler.
//
NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetAddConnection2");
return FALSE;
}

//
// Otherwise, report a successful connection.
//
TextOut(hdc, 10, 10, "Connected to specified resource.", 32);
howtotell 2005-12-10
  • 打赏
  • 举报
回复
WIN32_FIND_DATA fd;
memset(&fd,0,sizeof(fd0);
if(FindFirstFile("z:",&fd)==NULL)
{
//无效
}
freemme 2005-12-10
  • 打赏
  • 举报
回复
up,学习中
mostneed 2005-12-10
  • 打赏
  • 举报
回复
to oyljerry(【勇敢的心】→ ㊣Winterize√㊣)

试过了, 在什么情况下, WNetConnection都返回TRUE
oyljerry 2005-12-10
  • 打赏
  • 举报
回复
直接连接WNetConnection等连接,看能否visit
mostneed 2005-12-10
  • 打赏
  • 举报
回复
to nuaawenlin(飘人)

fopen啥?? 不知道上面有什么东西
nuaawenlin 2005-12-10
  • 打赏
  • 举报
回复
fopen :)

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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