vb.net中关于 socket的连接服务器失败问题! 怎么处理连接失败? 关于vb.net中socket编程实例:服务器端的一些问题
vb.net socket\u600e\u4e48\u5224\u65ad\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7684\u8fde\u63a5\u65ad\u5f00\u4e86? thank you!\u4f60\u53ef\u4ee5\u6839\u636e\u670d\u52a1\u5668\u6536\u5230\u7684\u6570\u636e\u7684\u957f\u5ea6\u6765\u5224\u65ad\uff0c\u5982\u679c\u670d\u52a1\u5668\u6536\u5230\u7684\u6570\u636e\u957f\u5ea6\u662f0\uff0c\u90a3\u4e48\u610f\u5473\u7740\u4f60\u7684\u5ba2\u6237\u7aef\u7a0b\u5e8f\u5df2\u7ecf\u65ad\u5f00\u4e86\u8fde\u63a5\u3002\u4eceTCP/IP\u534f\u8bae\u6808\u7684\u89d2\u5ea6\u6765\u8bf4\uff0c\u5c31\u662f\u5ba2\u6237\u7aef\u7a0b\u5e8f\u5173\u95ed\u4e86\u81ea\u5df1\u5199\u7684\u8fd9\u4e00\u534a\u8fde\u63a5\uff0c\u5411\u670d\u52a1\u5668\u53d1\u51fa\u4e86\u4e00\u4e2aFIN\u3002\u8fd9\u6d89\u53ca\u5230TCP\u7684\u72b6\u6001\u8fc1\u79fb\uff0c\u5173\u4e8e\u8fd9\u65b9\u9762\u7684\u77e5\u8bc6\uff0c\u5efa\u8bae\u4f60\u770b\u4e00\u4e0bRichard Stevens\u5148\u751f\u7684\u300aTCP/IP \u8be6\u89e3\u300b\u5377\u4e00\u548c\u300aUnix\u7f51\u7edc\u7f16\u7a0b\u300b\u5377\u4e00\uff0c\u4e0a\u9762\u6709\u8be6\u7ec6\u7684\u89e3\u91ca\u3002
\u5173\u4e8e\u4f60\u7684\u7b2c\u4e8c\u4e2a\u95ee\u9898\uff0c\u5efa\u8bae\u4f60\u4ed4\u7ec6\u770b\u4e00\u4e0b\u81ea\u5df1\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u4ee3\u7801\u3002\u670d\u52a1\u5668\u7a0b\u5e8f\u9996\u5148\u8981\u5efa\u7acb\u4e00\u4e2a\u76d1\u542csocket\uff0c\u5f53\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u4e0a\u6765\u65f6\uff0c\u670d\u52a1\u5668\u4f1a\u5728\u4e00\u4e2a\u65b0socket\u4e0a\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u6240\u4ee5\u5e76\u4e0d\u5b58\u5728\u201c\u4e71\u201d\u7684\u95ee\u9898\u3002\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u540c\u6837\u63a8\u8350\u4f60\u770b\u4e0a\u9762\u7684\u4e24\u672c\u5173\u4e8e\u7f51\u7edc\u7f16\u7a0b\u7684\u7ecf\u5178\u8457\u4f5c\u3002
Socket\u7c7b\u8fde\u63a5\u540e \u53ef\u4ee5\u901a\u8fc7\u7c7b\u4e2d\u7684 RemoteEndPoint \u6765\u83b7\u53d6\u8fdc\u7a0bIP\u548c\u7aef\u53e3\u4fe1\u606f.
\u8981\u6ce8\u610f\u7684\u662f.\u9700\u8981\u5c06\u5b83\u5f3a\u5236\u8f6c\u5316\u4e3a IPEndPoint\u7c7b\u578b \u7136\u540e\u901a\u8fc7 IPEndPoint.Address\u4ee5\u53caIPEndPoint.Port\u6765\u83b7\u53d6\u5bf9\u5e94\u7684IP\u53ca\u7aef\u53e3
\u4f8b:
Dim REV_SCK As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
'\u8fd9\u91cc\u521b\u5efa\u4e00\u4e2aUDP Socket\u8fde\u63a5\u7684\u5b9e\u4f8b TCP\u4e5f\u662f\u4e00\u6837\u7684
'\u7701\u7565SOCKET\u8fde\u63a5\u7684\u4ee3\u7801 \u5f53SOCKET\u8fde\u63a5\u6210\u529f\u540e \u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u8bed\u53e5\u83b7\u53d6\u8fdc\u7a0b\u4fe1\u606f
Dim IP as string ,Port as integer
'\u5b9a\u4e49IP\u548cPort\u53d8\u91cf \u7528\u6765\u4fdd\u5b58\u5730\u5740\u53ca\u7aef\u53e3
IP = IPAddress.Parse(CType(REV_SCK.RemoteEndPoint, IPEndPoint).Address.ToString())
Port = IPAddress.Parse(CType(REV_SCK.RemoteEndPoint, IPEndPoint).Port)
用 try-catch 结构,捕获 SocketException,示例:
Try
// 可能产生错误的代码
Catch ex As SocketException
MessageBox.Show("连接失败,原因是:" & ex.Message)
End Try
P.S. 好久不写 VB.NET 的了,这个是根据 C# 和记忆中的样子写的,题主看明白思路就好。
扩展阅读:韩国macbookpro ... macbook pro 16 ... xbox series x ... macbook pro2022 ... java blockingqueue ... matebook14 ... www.sony.com.cn ... macbook pro最新版本 ... macbook系列 ...