套接字事件 [英] Socket events

查看:99
本文介绍了套接字事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道如何在C#中用TCP编写套接字连接事件和套接字断开事件。我的意思是,当插入网络电缆时,我的程序显示"已连接",如果它已拔下,则会显示"Unplugged"。
请帮帮我。

问候。

解决方案

好吧,通过使用TCP,您无法检测错误发生的原因。您只能看到您无法访问特定地址。




























尝试{
TcpClient tcpClient = new 的TcpClient();
tcpClient.Connect(QUOT; your_server.com",80);
//存在连接
tcpClient.Close();
}
catch(Exception ex)
{
//没有连接
}

Hi,

I'd to know how to write socket connected event and socket disconnected event in TCP in C#. I mean that when network cable is plugged, my program displays "Connected", if it's unplugged, it displays "Unplugged".
Please help me.

Regards.

解决方案

Well, by using TCP you cannot detect why the error ocured. You can only see that you can't access a specific address.

try{  
TcpClient tcpClient = new TcpClient ();  
 
tcpClient.Connect ("your_server.com", 80);  
//Connection is present  
tcpClient.Close();  
}  
catch(Exception ex)  
{  
//no connection  


这篇关于套接字事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆