473,508 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UDP sockets

Hi

I'm porting a client writen in C++ to python. What is the way to get a
timeout in an select for one socket?
the c++ code is:
FD_ZERO(&fds);
FD_SET(fd_sock, &fds);
tv.tv_sec=2;
tv.tv_usec=0;
n = select(fd_sock+1,&fds,NULL,NULL,&tv);
sendto(...)

Thanks

Zunbeltz

--
Remove XXX from email: zu******@wm.lc.ehu.esXXX
Jul 18 '05 #1
4 4733
On Wed, Oct 22, 2003 at 11:15:35AM +0200, Zunbeltz Izaola wrote:
Hi

I'm porting a client writen in C++ to python. What is the way to get a
timeout in an select for one socket?
the c++ code is:
FD_ZERO(&fds);
FD_SET(fd_sock, &fds);
tv.tv_sec=2;
tv.tv_usec=0;
n = select(fd_sock+1,&fds,NULL,NULL,&tv);
sendto(...)


The python code is (assuming your socket.socket object is in a variable
called 'sock'):

import select
r, w, e = select.select([sock], [], [], 2)
if r:
sock.sendto(...)

-Andrew.
Jul 18 '05 #2
On Wed, Oct 22, 2003 at 10:02:00PM +1000, Andrew Bennetts wrote:
On Wed, Oct 22, 2003 at 11:15:35AM +0200, Zunbeltz Izaola wrote:
Hi

I'm porting a client writen in C++ to python. What is the way to get a
timeout in an select for one socket?
the c++ code is:
FD_ZERO(&fds);
FD_SET(fd_sock, &fds);
tv.tv_sec=2;
tv.tv_usec=0;
n = select(fd_sock+1,&fds,NULL,NULL,&tv);
sendto(...)


The python code is (assuming your socket.socket object is in a variable
called 'sock'):

import select
r, w, e = select.select([sock], [], [], 2)
if r:
sock.sendto(...)


Oh, I hurried too much and misread your code. You seem to want to always
call sendto, regardless of the result of the select call. In that case it's
even easier:

import select
select.select([sock], [], [], 2) # Just throw away the results :)
sock.sendto(...)

Anyway, hopefully it should be clear enough how to use select in python now
:)

-Andrew.
Jul 18 '05 #3
Dnia 22 Oct 2003 11:15:35 +0200, Zunbeltz Izaola napisa³(a):
I'm porting a client writen in C++ to python. What is the way to get a
timeout in an select for one socket?


Quote from the ,,7.3 select -- Waiting for I/O completion'':

The optional timeout argument specifies a time-out as a floating point
number in seconds.

So, it'll be something like this:

n = select.select([fds], [], [], 2)[0][0]

--
[ Wojtek Walczak - gminick (at) underground.org.pl ]
[ <http://gminick.linuxsecurity.pl/> ]
[ "...rozmaite zwroty, matowe od patyny dawnosci." ]

Jul 18 '05 #4
Wojtek Walczak wrote:

Dnia 22 Oct 2003 11:15:35 +0200, Zunbeltz Izaola napisa³(a):
I'm porting a client writen in C++ to python. What is the way to get a
timeout in an select for one socket?


Quote from the ,,7.3 select -- Waiting for I/O completion'':

The optional timeout argument specifies a time-out as a floating point
number in seconds.

So, it'll be something like this:

n = select.select([fds], [], [], 2)[0][0]


At first glance, this appears unsafe. What if the fds socket
is not ready after the timeout?

You will, I think, get an IndexError raised.

-Peter
Jul 18 '05 #5

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

Similar topics

2
3880
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
1
3757
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
0
1786
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
3
10379
by: Logan McKinley | last post by:
I have a C# program that uses blocking sockets and want to allow the user to stop the server. The problem I am having is the socket blocks on...
1
20175
by: Adam Clauss | last post by:
I am (attempting) to move an existing socket application to use raw sockets. Right now, my application is essentially a port forwarder. Upon receiving a connection, it will open a connection to...
4
6297
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
4335
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
3
11567
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection...
7
6703
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor...
0
7225
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
7124
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
7385
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...
1
7046
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...
1
5053
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...
0
4707
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...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1558
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 ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.