473,386 Members | 1,791 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,386 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 3489
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.