473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Closing event for socket

Hi folks

is it possible to let VB.NET call a specific handler, when a socket gets closed (either as a server, or as a client)

Thank
Helmut
Nov 22 '05 #1
4 3483
I think you're asking how to detect when a socket is closed remoteley. I banged my head against that one for quite a while. It's documented very poorly. If that's what you're looking for, this should help.

This snippet is in C# but you should be able to convert it. There's a lot left out so e-mail me if you have any questions

/// code above here ommitte

//continuously poll for incoming messages on this connectio
while(userIsLogedIn

//wait for the connection's mutex to come fre
con.Mtx.WaitOne()
Debug.WriteLine(" Locked " + con.Name, "CommThread")

//poll the socket for incoming data (1 second timeout
if(con.Sock.Poll(1000, SelectMode.SelectRead)

if(con.Ns.DataAvailable

//read the incoming dat
numBytes = con.Sock.Receive(peekBytes,SocketFlags.Peek)
bytes = new byte[numBytes]
con.Ns.Read(bytes, 0, Convert.ToInt32(bytes.Length))

//throw the event (the event determines what to do with the message
this.OnIncomingMessage(this, new MessageEventArgs(bytes, con))

else //client is probably disconnecting

//test for remote disconnection
byte [] buffer = new byte[1];
if(con.Sock.Receive(buffer, SocketFlags.Peek) <= 0)

//no data recieved: this IS a disconnection so throw the even
this.OnRemoteDisconnection(this, new ConnectionEventArgs(con))

//break out of the loop to cause a full local disconnectio
break

els

noData = true
/// code below here ommitted
Nov 22 '05 #2
I think you're asking how to detect when a socket is closed remoteley. I banged my head against that one for quite a while. It's documented very poorly. If that's what you're looking for, this should help.

This snippet is in C# but you should be able to convert it. There's a lot left out so e-mail me if you have any questions

/// code above here ommitte

//continuously poll for incoming messages on this connectio
while(userIsLogedIn

//wait for the connection's mutex to come fre
con.Mtx.WaitOne()
Debug.WriteLine(" Locked " + con.Name, "CommThread")

//poll the socket for incoming data (1 second timeout
if(con.Sock.Poll(1000, SelectMode.SelectRead)

if(con.Ns.DataAvailable

//read the incoming dat
numBytes = con.Sock.Receive(peekBytes,SocketFlags.Peek)
bytes = new byte[numBytes]
con.Ns.Read(bytes, 0, Convert.ToInt32(bytes.Length))

//throw the event (the event determines what to do with the message
this.OnIncomingMessage(this, new MessageEventArgs(bytes, con))

else //client is probably disconnecting

//test for remote disconnection
byte [] buffer = new byte[1];
if(con.Sock.Receive(buffer, SocketFlags.Peek) <= 0)

//no data recieved: this IS a disconnection so throw the even
this.OnRemoteDisconnection(this, new ConnectionEventArgs(con))

//break out of the loop to cause a full local disconnectio
break

els

noData = true
/// code below here ommitted
Nov 22 '05 #3
If you are using synchronous mode then there are no "perfect" solution,
because there are no separate thread where your handler can be called. If
you are using asynchronous sockets (that you probably should) as soon as
socket will be closed "read handler" will be called and EndReceive will
throw exception when you will call it.

Nov 22 '05 #4
If you are using synchronous mode then there are no "perfect" solution,
because there are no separate thread where your handler can be called. If
you are using asynchronous sockets (that you probably should) as soon as
socket will be closed "read handler" will be called and EndReceive will
throw exception when you will call it.

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
4
by: flupke | last post by:
Hi, I have a gui (made in wxPython) that enables a user to connect to a server and issue some commands. The problem occurs when i try to disconnect the client. It exits but it doesn't return to...
2
by: Helmut Gardner | last post by:
Hi folks is it possible to let VB.NET call a specific handler, when a socket gets closed (either as a server, or as a client) Thank Helmut
1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
0
by: Shane | last post by:
I want to use BeginRead on a socket stream. I want to abort the read if no data arrives within a certain time interval. I know i can't cancell the BeginRead directly but closing the socket seems...
4
by: Funke | last post by:
Assume that in C#, I create a server socket (listener) and code to start new threads with each connection using BeginAccept(). After some time, I have three threads running, each with their own...
3
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue...
22
by: Zytan | last post by:
I have public methods in a form. The main form calls them, to update that form's display. This form is like a real-time view of data that is changing. But, the form may not exist (it is...
4
by: O.B. | last post by:
I have a socket configured as TCP and running as a listener. When I close socket, it doesn't always free up the port immediately. Even when no connections have been made to it. So when I open...
0
by: FragMagnet | last post by:
Hi all, I've run into a bit of a snag while dealing with asynchronous sockets. What I'm trying to do is set a timeout for which the server will listen for a connection from a client, at which...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.