473,597 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3509
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(userIsLog edIn

//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.Pol l(1000, SelectMode.Sele ctRead)

if(con.Ns.DataA vailable

//read the incoming dat
numBytes = con.Sock.Receiv e(peekBytes,Soc ketFlags.Peek)
bytes = new byte[numBytes]
con.Ns.Read(byt es, 0, Convert.ToInt32 (bytes.Length))

//throw the event (the event determines what to do with the message
this.OnIncoming Message(this, new MessageEventArg s(bytes, con))

else //client is probably disconnecting

//test for remote disconnection
byte [] buffer = new byte[1];
if(con.Sock.Rec eive(buffer, SocketFlags.Pee k) <= 0)

//no data recieved: this IS a disconnection so throw the even
this.OnRemoteDi sconnection(thi s, new ConnectionEvent Args(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(userIsLog edIn

//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.Pol l(1000, SelectMode.Sele ctRead)

if(con.Ns.DataA vailable

//read the incoming dat
numBytes = con.Sock.Receiv e(peekBytes,Soc ketFlags.Peek)
bytes = new byte[numBytes]
con.Ns.Read(byt es, 0, Convert.ToInt32 (bytes.Length))

//throw the event (the event determines what to do with the message
this.OnIncoming Message(this, new MessageEventArg s(bytes, con))

else //client is probably disconnecting

//test for remote disconnection
byte [] buffer = new byte[1];
if(con.Sock.Rec eive(buffer, SocketFlags.Pee k) <= 0)

//no data recieved: this IS a disconnection so throw the even
this.OnRemoteDi sconnection(thi s, new ConnectionEvent Args(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
9273
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 python wrapper module for sockets, and found that the close command doesn't actually call the underlying close! this didn't seem right, so i added it, and my code now works simply and as expected. def close(self):
4
2426
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 the prompt. I have to push Ctrl-C in order to have it exit completely. The GUI is closed though. This is a piece of code from the main class that connects to the server and starts a thread that handles the connection: =======================...
2
271
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
3783
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 after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve connection, recieve data, and close socket
0
1257
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 to work. Does this cause any problems (Resource leaks etc) ? Should I also call EndRead after closing the socket even though it will it throw ? Has anyone done anything similar ? Shane
4
2758
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 client socket connection. If I close the listener socket, will the client sockets also shut down? Or do I need to manually shut these down as well?
3
5260
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 is about Python or POSIX. In the following, the first time through, everything works. On the second connection, though, the same file descriptor as the first connection may be re-used, but for some reason, trying to do os.read/close on that...
22
3064
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 created / destroyed at user request). I can check form != null to prevent incorrect access. But, the form could disappear immediately after the check, before the method is run. Or someone could click 'close' when it's in the middle of an update. ...
4
16075
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 the socket again, the bind fails because the port is still in use. When I execute the code in "debug" mode, the problem never occurs. When I execute the same code in release mode, the problem appears about 20% of the time. Here's the code:
0
1818
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 point the object maintaining the server side connection will be disposed of through the use of a delegate. The code is as follows: public Bridge(string args, int inPort) { name = args; surname = args;
0
7885
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5426
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2399
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.