473,387 Members | 1,504 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,387 software developers and data experts.

Close a socket externally

drs
Hi,

I have a program which opens a socket server in a thread. I need for the
server to listen on the socket for a certain amount of time (say, ten
seconds or so) and then close it, and am wondering if there is a good way to
close the socket other than sending it a message from another thread to
close itself. That is, I am looking for a way to forcably kill a socket
from a second thread, not to send it a message to kill itself.

Thanks,

-drs
Jul 18 '05 #1
7 1796
"drs" <dr*@remove-to-send-mail-ecpsoftware.com> writes:
I have a program which opens a socket server in a thread. I need for the
server to listen on the socket for a certain amount of time (say, ten
seconds or so) and then close it, and am wondering if there is a good way to
close the socket other than sending it a message from another thread to
close itself. That is, I am looking for a way to forcably kill a socket
from a second thread, not to send it a message to kill itself.


Why the requirement to close the socket from another thread? Why
can't the thread that created the socket time out and close the
socket itself?

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 18 '05 #2
On Fri, 01 Oct 2004 22:49:28 +0000, drs wrote:
I have a program which opens a socket server in a thread. I need for the
server to listen on the socket for a certain amount of time (say, ten
seconds or so) and then close it, and am wondering if there is a good way to
close the socket other than sending it a message from another thread to
close itself. That is, I am looking for a way to forcably kill a socket
from a second thread, not to send it a message to kill itself.


Speaking generally, I would be surprised if this existed, because it would
basically be a security hole. Not quite literally, but it definitely feels
wrong.

The only way to do it that I can think of is for your program to interpose
itself and act as a proxy between the other two. Then you can close it the
*two* sockets anytime you want, for any reason. You could also separate
the proxy part from the control part by leaving a control port open on the
proxy. This has obvious issues, notably bandwidth, but short of major
kernel patching I think this is your choice.

Or, more likely, your architecture needs a bit of restructuring.

I might be wrong, but if I am, I register my surprise in advance. Also, it
is going to be very, very platform specific and you don't mention what
platform you are on.
Jul 18 '05 #3

drs wrote:
I have a program which opens a socket server in a thread. I need for the server to listen on the socket for a certain amount of time (say, ten
seconds or so) and then close it, and am wondering if there is a good way to close the socket other than sending it a message from another thread to close itself.
Yes, you can either set a timeout on the socket, or use
select.select() with a timeout. When waiting for new
connections, a socket will select as readable when a new
connection arrives.
That is, I am looking for a way to forcably kill a socket
from a second thread, not to send it a message to kill itself.


The timeout/select solution is along different lines, but if I
understood your initial statement of the problem, it's how most
sockets programmers do that kind of thing.
--
--Bryan

Jul 18 '05 #4
drs

<br***********************@yahoo.com> wrote in message
news:10**********************@k26g2000oda.googlegr oups.com...

drs wrote:
I have a program which opens a socket server in a thread. I need for

the
server to listen on the socket for a certain amount of time (say, ten
seconds or so) and then close it, and am wondering if there is a good

way to
close the socket other than sending it a message from another thread

to
close itself.


Yes, you can either set a timeout on the socket, or use
select.select() with a timeout. When waiting for new
connections, a socket will select as readable when a new
connection arrives.
That is, I am looking for a way to forcably kill a socket
from a second thread, not to send it a message to kill itself.


The timeout/select solution is along different lines, but if I
understood your initial statement of the problem, it's how most
sockets programmers do that kind of thing.


My understanding of using a timeout is that the clock starts running only
when there is no communication. I actually need a hard limit on the time a
socket is open, and while the best option might be to at least allow the
current read/write cycle to complete, I am not necessarilly adverse to just
cutting off the socket.

To explain, I am working on an agent based modeling project where agents
communicate and exchange over TCP sockets. Agents have to "buy" the right
to open sockets, so I need an enforcement mechanism to shut them down when
their time expires.

I am not familiar w/ select, so i'll go look into that.

-drs
Jul 18 '05 #5
drs wrote:
To explain, I am working on an agent based modeling project where agents
communicate and exchange over TCP sockets. Agents have to "buy" the right
to open sockets, so I need an enforcement mechanism to shut them down when
their time expires.


Would it work to limit the number of bytes that they're allowed to
send/receive, instead of time? Especially given that TCP's delivery
guarantees mean that the transfer times for a given chunk of data can't
really be predicted (because it may include any number of
retransmissions), it seems that byte-count might be a more "fair" way of
allocating communication. Of course, this depends on how much control
you have over the specifications...

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #6
The method I like is to compute a deadline for all the action on
the socket, then compute a new timeout for each blocking
operation. The following isn't tested, or even compiled, but it
shows the idea:

deadline = time.time() + hard_limit_on_the_time
while stuff_to_do:
timeout = deadline - time.time()
if timeout < 0:
break # time limit expired
(readable, _, _) = select.select([mysock], [], [], timeout)
if ! readable:
break # time limit expired
# at this point mysock has data available (barring weird
things)
--
--Bryan

Jul 18 '05 #7
I wrote:
The following isn't tested [...]


Web-posting screwed up the indentation. It should read:

deadline = time.time() + hard_limit_on_the_time
while stuff_to_do:
timeout = deadline - time.time()
if timeout < 0:
break # time limit expired
(readable, _, _) = select.select([mysock], [], [], timeout)
if ! readable:
break # time limit expired
# at this point mysock has data available (barring weird things)

---
--Bryan

Jul 18 '05 #8

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

Similar topics

3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
9
by: AA | last post by:
This is making me crazy!! Please, if some body can help me. I'm testing a ver simple socket client. In my test I just open and close a connection (in a loop) to my local IIS server (port 80)...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
4
by: Haim | last post by:
it is very strange for me that a simple event of closing socket that was in the the winsock object of vb6 , i didn't found yet in the vb.net the only way i found is to try to send something to...
14
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still...
4
by: =?Utf-8?B?Um9iS2lubmV5MQ==?= | last post by:
Hello, My boss asked me if it is possible to wrap SSL around an existing socket which is running with a C++ program (I know it is, I have done it before). I am wondering if it is possible to do...
1
by: Sparky74 | last post by:
Hi Everybody. I have been searching for many hours for an answer to this problem. I hope somebody can help me. I have a C# .NET client application that connects to a TCP/IP C++ server application...
3
by: A. W. Dunstan | last post by:
I have an application that wants to open a Socket, write data and close the socket. A persistent connection would be nice, but it's intended to operate in an environment where the network...
0
by: meLlamanJefe | last post by:
I have the following code snippet that is used in a simple multi-threaded server program I am testing. The ServerSocket class binds to a port and creates a listening socket. Then it sits and waits...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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,...
0
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...

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.