Hi, I need that my server will be in waiting mode while first packet arrived, when it happens it shoud start to receive packets until the socket.settimeout(1) function kill waiting. my code seems like that, and is working properly until settimeout() return :
Traceback (most recent call last):
File "C:\Documents and Settings\Shatt\Desktop\SERVERIS1.txt", line 23, in <module>
paketas= s.recv( 2500 )
timeout: timed out
the main problem is that i need that my program execute other commands after while cycle, maybe any suggestions how to make it?

s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
    s.bind( ( HOST, PORT ) )
    if s.recv (2500):
          while 1: 
               f = open(.....)
              paketas= s.recv( 2500 )
              s.settimeout(1)
              t_pak=datetime.datetime.now()
              a=t_pak.strftime('%Y-%m-%d %H:%M:%S.%f')
              f.write(....)
              f.close()

in short, i need that my server will wait until it get first packet, after this it shoud wait and receive all packets, and when server don't get packets for abaout 2sec, it should leave waiting mode, and other part of program should be executable.THANKS Very appreciate all suggestions.

Recommended Answers

All 4 Replies

Rather than having to wait for a socket to time out to allow the rest of your program to execute, you could always use threading. Then you wouldn't have to worry about a timeout or anything, you could just have one thread receive packets while the other one does whatever other action you want to do. Post back if you have any questions about how to do that.

However, if you do not want to use threading, the reason you are having that error is because there is no data waiting to be read from the socket. A much easier way to check if there is data on a socket is using the select module. That way, rather than using a socket timeout, you could do something like this:

### THIS IS PSEUDOCODE
if socket has data:
    receivePacket()
else:
    doSomethingElse()

The problem is that I should to receive a lot of packets and I don't know when the last will come. when I receive the first packet the other come very quikle ~30/sec.So I need that my server receive all of them, and this i'm doing with packet,address=socket.recvfrom(1500), but how to do when there are no packets for 2sec that server stop waiting and go to other part of program. i think that it should be something like non blocking socket, because now it's blocking and if the server doesn't receive packet it is waiting and can't do nothing, how to make that if there are no packets for 2sec the server will stop waiting and program do other things? THANKS

I think you should follow a standard pattern, like the code in the socket module example http://docs.python.org/py3k/library/socket.html#example. You could write something like

# python 2.6, untested code
s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind( ( HOST, PORT ) )
s.listen(1)
while True:
    conn, addr = s.accept() # conn is a new socket
    conn.settimeout(1)
    f = open(....)
    try:
        while True:
            paketas= s.recv( 2500 )
            if not paketas:
                break
            t_pak=datetime.datetime.now()
            a=t_pak.strftime('%Y-%m-%d %H:%M:%S.%f')
            f.write(....)
    except socket.timeout:
        pass
    finally:
        conn.close()
        f.close()
s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
s.bind((HOST,PORT))
if s.recvfrom(1500):
f.open(....)
     while True:
           packet,address=s.recvfrom(1500)
           f.write(...)
           f.close()
s.close()

this code is working properly for me, the server get first packet and accepting all other because when the first packet arrives starts the burst of packets. how can I change this code that if there are no packets the server waiting mode will finish and socket will close. because now if the socket doesn't get any packets it is waiting and standing by packet,address=s.recvfrom(1500) this command block the job and is standing, how to make that it won't wait and close the socket? thanks.appreciate your advices:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.