Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 TcpConnection.Connect()没有反应结果,如何捕获连接的异常? #16

Closed
WengJianyang opened this issue Dec 17, 2019 · 10 comments

Comments

@WengJianyang
Copy link

No description provided.

@hiram3512
Copy link
Owner

直接使用trycatch捕获这个接口的异常即可

@WengJianyang
Copy link
Author

直接使用trycatch捕获这个接口的异常即可

有一种情况没有走进异常情况,也没捕获异常,有链接超时接口吗

@WengJianyang
Copy link
Author

直接使用trycatch捕获这个接口的异常即可

我使用connect链接一个地址,什么反应都没有,后来发现是地址输错了,然后我想在这里加一个回调,例如连接超时或者地址不存在的异常以做提醒

@hiram3512
Copy link
Owner

直接使用trycatch捕获这个接口的异常即可

我使用connect链接一个地址,什么反应都没有,后来发现是地址输错了,然后我想在这里加一个回调,例如连接超时或者地址不存在的异常以做提醒

连接超时用系统的timeout,系统会在连接是抛出各种异常,可以通过connect接口捕获到

@WengJianyang
Copy link
Author

你说的这个系统的timeout具体是指哪个?可以给一个链接吗

@WengJianyang
Copy link
Author

收发能看到,但是链接没有。。。

@hiram3512
Copy link
Owner

收发能看到,但是链接没有。。。

connect系统有默认的超时时间,等待超时后抛出异常,如果不想等待那么久自己写逻辑处理超时,源码里面有提供socket对象,可以对它进行操作,比如:
https://stackoverflow.com/questions/1062035/how-to-configure-socket-connect-timeout

多查下资料,这种网上方案很多..

@lookforit
Copy link

直接使用trycatch捕获这个接口的异常即可

我看了下源码,connect()方法最终调用了BeginConnect()。
由于异常在asynccallback 中最终被抛出,导致在主线程无法捕获。

请问如何处理

@hiram3512
Copy link
Owner

asynccallback

既然异常在asynccallback中被抛出,可以在callback中捕获异常做对应的处理.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants