.net - tcpClient.State analogue in vb.net (versus vb6.0) -
i used next code in vb6.0 winsock object tcpclient:
dim sconst(10) string dim st integer sconst(0) = "closed" sconst(1) = "open" sconst(2) = "listening" sconst(3) = "connectionpending" sconst(4) = "resolvinghost" sconst(5) = "hostresolved" sconst(6) = "connecting" sconst(7) = "connected" sconst(8) = "closing" sconst(9) = "error" st = tcpclient.state textbox1.text = sconst(st)
now im using vb.net , want same. there no .state
method tcpclient
objects! there cpclient.connected
returns boolean
yes or not. how can alike vb6.0?
using visual vinsent's answer made this:
public class form1 dim status1 string dim status2 string private sub btn_connect5001_click(sender object, e eventargs)_ handles btn_connect5001.click ' create tcpclient , connect tcpclnt2 = new tcpclient try tcpclnt2.connect("192.168.1.177", 5001) catch end try end sub private sub btn_disconnect5001_click(sender object, e eventargs)_ handles btn_disconnect5001.click ' close tcpclient tcpclnt2.close() end sub private sub timer1_tick(sender object, e eventargs) handles timer1.tick ' check status every 300ms status2= new ipendpoint(ipaddress.parse("192.168.1.177"),5001).getstatus().tostring() textbox1.text = dns.gethostname + environment.newline + "port 1 " + status2 + environment.newline end sub end class
the problem is: @ start status2
"unknown", if connect first time status2
"established". if disconnect became "timewait". if connect 1 more time stay "timewait". never change value then.
neither tcpclient
nor underlying socket
appears have such implementation, wrote 3 extension methods you.
what they iterate every active tcp connection on computer using ipglobalproperties
class , getactivetcpconnections()
method, match connection against tcpclient
's local , remote ip-address.
extensions.vb
imports system.runtime.compilerservices imports system.reflection imports system.net imports system.net.sockets imports system.net.networkinformation public module extensions ''' <summary> ''' dynamically gets object's property name. ''' </summary> ''' <param name="obj">the object which's property get.</param> ''' <param name="propertyname">the name of property get.</param> <extension()> _ public function getproperty(byval obj object, byval propertyname string) object return obj.gettype().invokemember(propertyname, _ bindingflags.getproperty _ or bindingflags.ignorecase _ or bindingflags.public _ or bindingflags.nonpublic _ or bindingflags.instance _ or bindingflags.static, _ nothing, obj, nothing) end function ''' <summary> ''' gets status of tcp connection. ''' </summary> ''' <param name="client">the tcpclient which's status get.</param> ''' <remarks></remarks> <extension()> _ public function getstatus(byval client tcpclient) tcpstate if client nothing orelse client.client nothing return tcpstate.unknown each tcpconnection tcpconnectioninformation in ipglobalproperties.getipglobalproperties().getactivetcpconnections() if tcpconnection.localendpoint.equals(client.client.localendpoint) andalso _ tcpconnection.remoteendpoint.equals(client.client.remoteendpoint) return tcpconnection.state end if next return tcpstate.unknown end function ''' <summary> ''' gets status of tcp connection. ''' </summary> ''' <param name="endpoint">the ipendpoint (ip-address) of tcp connection which's status get.</param> ''' <remarks></remarks> <extension()> _ public function getstatus(byval endpoint ipendpoint) tcpstate if endpoint nothing return tcpstate.unknown each tcpconnection tcpconnectioninformation in ipglobalproperties.getipglobalproperties().getactivetcpconnections() if tcpconnection.localendpoint.equals(endpoint) orelse _ tcpconnection.remoteendpoint.equals(endpoint) return tcpconnection.state end if next return tcpstate.unknown end function ''' <summary> ''' gets status of tcp listener. ''' </summary> ''' <param name="listener">the tcplistener which's status get.</param> ''' <remarks></remarks> <extension()> _ public function getstatus(byval listener tcplistener) tcpstate if listener nothing orelse listener.server nothing return tcpstate.unknown 'per source code, active listeners in "listen" state: 'https://referencesource.microsoft.com/#system/net/system/net/networkinformation/systemipglobalproperties.cs,51fa569e558be704 if listener.getproperty("active") = true return tcpstate.listen return directcast(listener.localendpoint, ipendpoint).getstatus() 'the listener not in active state. end function end module
now status of connection have 3 options:
getting status of
tcpclient
:dim status string = yourclient.getstatus().tostring()
getting status of
tcplistener
:dim status string = yourlistener.getstatus().tostring()
getting status of connection specified ip-address:
dim status string = new ipendpoint(ipaddress.parse("your ip-address here"), yourporthere).getstatus().tostring()
important notes:
the
tostring()
call necessary if want status's name (for exampleestablished
), otherwise you'll enumeration value normalinteger
(for example3
).getting status of
tcpclient
ortcplistener
work if neither nor underlyingsockets
disposed. if wish status of disconnectedtcpclient/-listener
have use option no. 3 , exact ip-address , port.- disconnected tcp connections can interesting because still exist after being closed , have state little while. examples of disconnected states
closewait
ortimewait
.
- disconnected tcp connections can interesting because still exist after being closed , have state little while. examples of disconnected states
read more:
tcp protocol operation - wikipedia (a list of different tcp connection states)
edit:
to fix problem connections lingering in close_wait
or time_wait
state can set underlying socket
linger 0 seconds , dispose it. doing cause socket perform so-called "hard close" sending rst
(reset) packet instead of fin
(fin
tells socket connection closed , should go close-/time_wait
). method forces connection close , no late/retransmitted packets mapped application after that.
it's mentioned on msdn documentation:
if
l_onoff
member of linger structure nonzero ,l_linger
member zero,closesocket
not blocked if queued data has not yet been sent or acknowledged. called hard or abortive close, because socket's virtual circuit reset immediately, , unsent data lost. on windows,recv
call on remote side of circuit failwsaeconnreset
.
this can done in simple extension method (put in extensions
module):
''' <summary> ''' forces specified tcpclient close without sending fin other endpoint. stop connection going time_wait, close_wait or fin_* state. ''' </summary> ''' <param name="client">the tcpclient close.</param> ''' <remarks></remarks> <extension()> _ public sub forceclose(byval client tcpclient) client.client.lingerstate = new lingeroption(true, 0) 'set socket linger, 0 seconds (causes current connection send rst-packet , terminate). client.client.dispose() 'dispose underlying socket instead of graceful shutdown. client.close() 'close tcpclient. end sub
then call this:
yourclient.forceclose()
Comments
Post a Comment