vb.net - .NET TcpClient connected to a VB6 Winsock Control does not close when Winsock control is closed -


this problem bit perplexing , tried on , on google , stack overflow find answer. wrote windows service in vb.net uses tcplistener class listen connections applications. straight forward service allows multiple instances of application send udp datagrams our instruments listening on port 7000. reason service because must listen on port 7001 udp datagrams instruments, forwarding them along connected client applications using tcp.

anyway, onto issue. when client application vb.net application using tcpclient connect service, runs fine. when client application closes tcp connection, tcpclient in services cannot receive anymore data (which causes exception in turn prompts service close , remove tcpclient in question). far good.

when client vb6 application using winsock control connect service, fine until winsock control closes connection. @ point, tcpclient.connected property still reads true , attempting read stream not result in exception. service still believes connection open. what's worse callback function beginreceive function in network stream keeps getting called tcpclient suppose closed. causes process explorer show service ramp using 100% of processor (or core). upon further investigation, process explorer reports connection state close_wait.

maybe there property or method in tcpclient class can check see if connection closed or being closed. have not found far, though. here copy of code examine. maybe can see missed something. taking time read this.

' receives tcp packet clients, processes them , forwards them on requested. private sub receivetcppacketcallback(byval asyncresult system.iasyncresult)     ' retrieve asynchronous state callback.     dim asyncstate tcpasyncstate = ctype(asyncresult.asyncstate, tcpasyncstate)      ' tcp client associated state.     dim tcpclient system.net.sockets.tcpclient = asyncstate.tcpclient      try         if tcpclient.connected             dim commanddata string              ' network stream associated tcp client sent packet.             dim networkstream system.net.sockets.networkstream = tcpclient.getstream              ' sync lock receive buffer ensure no other threads touching it.             synclock m_preceivebuffer                 ' end read retrieve data locked buffer.                 dim datalength integer = networkstream.endread(asyncresult)                  ' convert bytes retrieved string using ascii encoding.                 commanddata = system.text.encoding.ascii.getstring(m_preceivebuffer, 0, datalength)             end synclock              ' process command data sent tcp client , process command if complete.             if asyncstate.processcommanddata(commanddata) processcommandinformation(asyncstate)              ' continue listening more commands client.             networkstream.beginread(m_preceivebuffer, 0, m_preceivebuffer.length, addressof receivetcppacketcallback, asyncstate)         else             ' attempt close tcp client , remove list.             tcpclient.close()             if m_ptcpclients.contains(tcpclient) m_ptcpclients.remove(tcpclient)         end if     catch ex exception         dim message new system.text.stringbuilder          ' if exception occurs, assemble message write event log.         message.append(date.now.tostring).append(","c).append("receivetcppacketcallback exception,")         message.append("error receiving command tcp client,")         message.append(ex.source).append(","c).append(ex.message)          ' write message event log.         me.eventlog.writeentry(message.tostring)          ' attempt close tcp client , remove list.         tcpclient.close()         if m_ptcpclients.contains(tcpclient) m_ptcpclients.remove(tcpclient)     end try end sub 


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -