PNRT
asked on
Cannot close socket connection in vb.net
Hi, using VB.net/VS2005/.net2. Created server using TCP listener, Asyncronous connection from client. Client connects and disconnects from server correctly. However, if client losses connection, server does not close connection automatically. Using the code below doesnt close the connection. Looking for a way to either close the connection or the thread automatically, or perhaps by time (nothing heard after so many seconds or something) and how to access and control that particular connection by code
If bytesRead < 1 Then
_client.Close()
other stuff
Exit Sub
Else
Do stuff with the bytes
End If
ASKER
Thanks for this. However, my problem seems to be refering back to a particular connection to close it, when I have say 50 connections open and three or four that have dropped. If I cant get it to close automatically, then I would like to be able to at least address and control a particular connection
You will have an array or a user-defined class with references to each of your 50 sockets, right? I assume that you would loop through your 50 connections to find the ones which have dropped, or which have gone idle and you want to drop. Use the code above to kill the socket connection.
(Perhaps I am not understanding?)
Cheers.
(Perhaps I am not understanding?)
Cheers.
ASKER
Thanks Andy, yes you're understanding correctly, a hashtable in fact. I'm just trying to get my head around the timer aspect. I'll give it a try and get back as soon as.
ASKER
Just incidently, any idea why the code is not closing the connection once there are no further bytes to be read?
The socket is waiting for the client to send more bytes. There are different situations where you may want to use a socket.
I used these sockets to write a telnet application whereby users would type commands and have them executed by the server. In that situation I wanted the connection to remain open, accepting commands whenever the user typed them, until the user typed the "quit" command or they had fallen idle for 5 minutes.
In other situations, you may have an automated client (1) connect, (2) send X bytes, and then (3) be done. I am guessing that perhaps this is the type of application you are writing. In your case you could execute the code I provided to then close the connection after receiving whatever the client has sent.
Cheers.
I used these sockets to write a telnet application whereby users would type commands and have them executed by the server. In that situation I wanted the connection to remain open, accepting commands whenever the user typed them, until the user typed the "quit" command or they had fallen idle for 5 minutes.
In other situations, you may have an automated client (1) connect, (2) send X bytes, and then (3) be done. I am guessing that perhaps this is the type of application you are writing. In your case you could execute the code I provided to then close the connection after receiving whatever the client has sent.
Cheers.
ASKER
Thanks yes, Im writing a server that will stay connected and send automated replies back to the client.
I think I've now done a complete 360 and got back where I started. Still cant address the particular socket that needs closing.
When a new connection occurs, I create three hashtables holding various data. One holds the IP/Port number. This is the hashtable IPPorts. If ObjEntry is the loop, I have this.
Dim oldIP As Net.Sockets.TcpClient
oldIP = Client .IPPorts(objEntry.Key)
To close or disconnect the oldIP
I then get -
"Option Strict On disallows implicit conversions from 'Object' to 'System.Net.Sockets.TcpCli ent'.
Intellisanse then suggests the following (obviously to change the type)
oldIP = CType(Client.IPPorts(objEn try.Key), Net.Sockets.TcpClient)
This compiles OK but then throws up the exception - 'System.InvalidCastExcepti on: Unable to cast object of type system.string to System.Net,Sockets.TcpClie nt.
I've also tried an array but with the saem results
I dont seem to be able to ge the conversion between string and System.Net.Sockets.TcpClie nt.
So Im back where I started, not being able to address a particular socket in order to close/disconnect it
as I mentioned in the first question.
Any ideas?
I think I've now done a complete 360 and got back where I started. Still cant address the particular socket that needs closing.
When a new connection occurs, I create three hashtables holding various data. One holds the IP/Port number. This is the hashtable IPPorts. If ObjEntry is the loop, I have this.
Dim oldIP As Net.Sockets.TcpClient
oldIP = Client .IPPorts(objEntry.Key)
To close or disconnect the oldIP
I then get -
"Option Strict On disallows implicit conversions from 'Object' to 'System.Net.Sockets.TcpCli
Intellisanse then suggests the following (obviously to change the type)
oldIP = CType(Client.IPPorts(objEn
This compiles OK but then throws up the exception - 'System.InvalidCastExcepti
I've also tried an array but with the saem results
I dont seem to be able to ge the conversion between string and System.Net.Sockets.TcpClie
So Im back where I started, not being able to address a particular socket in order to close/disconnect it
as I mentioned in the first question.
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the reply Andy,not exactly what I was after as I would have to rewrite the method I have, but gives me the idea. I'll ask a seperate question regarding the type conversion
In my application, I used a variable "idle" for each socket. Whenever data came from that socket, I reset idle to zero, and a timer popped every second to increment the idle counter. When idle reached 10 minutes (or whatever), I had it trigger the following code which seemed to do the job.
Cheers
Open in new window