473,799 Members | 3,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Plz help..SocketSer ver UDP server losing lots of packets

logFileName = 'log.txt'
logfile = open(logFileNam e, "a")
class MyUDPServer(Soc ketServer.UDPSe rver):
def server_bind(sel f):
self.socket.set sockopt(socket. SOL_SOCKET,
socket.SO_RCVBU F, 8388608)
self.socket.bin d(self.server_a ddress)

class LogsDumpHandler (SocketServer.D atagramRequestH andler):
def handle(self):
global logfile
# fileExists is a function which checks the existence of a file
if not fileExists(logF ileName):
logfile = open(logFileNam e, "a")
logfile.writeli nes(self.rfile. readlines())

server = MyUDPServer(("" ,PORT), LogsDumpHandler )
server.serve_fo rever()
logfile.close()

The above python code is a UDP server based on SocketServer framework
and I am losing a lot of messages, I have tried almost everything,
increasing the size of the receive buffer, changing the way i read the
messages coming into server.
The above code is supposed to work as a tracing server and it does but
is losing messages.
Is there a way I can improve this code?
Any help will be highly appreciated.

Thanks,
Sam
Nov 5 '08 #1
4 3527
On Thu, Nov 6, 2008 at 9:53 AM, <id*****@gmail. comwrote:
logFileName = 'log.txt'
logfile = open(logFileNam e, "a")
class MyUDPServer(Soc ketServer.UDPSe rver):
def server_bind(sel f):
self.socket.set sockopt(socket. SOL_SOCKET,
socket.SO_RCVBU F, 8388608)
self.socket.bin d(self.server_a ddress)

class LogsDumpHandler (SocketServer.D atagramRequestH andler):
def handle(self):
global logfile
# fileExists is a function which checks the existence of a file
if not fileExists(logF ileName):
logfile = open(logFileNam e, "a")
logfile.writeli nes(self.rfile. readlines())

server = MyUDPServer(("" ,PORT), LogsDumpHandler )
server.serve_fo rever()
logfile.close()
This is horrible code :/

Try these instead:
* UDPServer ->
http://trac.softcircuit.com.au/circu...s/udpserver.py
* UDPClient ->
http://trac.softcircuit.com.au/circu...s/udpclient.py

Example usage
----------------------

~/circuits/examples
$ ./udpserver.py
127.0.0.1, 9000:
test
127.0.0.1, 9000:
hi

~/circuits/examples
$ ./udpclient.py -b 127.0.0.1:9000 127.0.0.1:8000
test
127.0.0.1, 8000:
test
hi
127.0.0.1, 8000:
hi

You can download circuits from http://trac.softcircuit.com.au/circuits/
or get the latest development version by using Mercurial (1):
hg clone http://hg.softcircuit.com.au/projects/circuits/

cheers
James

[1] http://www.selenic.com/mercurial/wiki/

--
--
-- "Problems are solved by method"
Nov 6 '08 #2
On Nov 6, 12:46*am, "James Mills" <prolo...@short circuit.net.au>
wrote:
>
Try these instead:
** UDPServer ->http://trac.softcircuit.com.au/circu...s/udpserver.py
** UDPClient *->http://trac.softcircuit.com.au/circu...s/udpclient.py
Since there's no contact details on the Circuits site, and the guest
Trac account doesn't work (it asks the admin to verify the email),
then I'll post here: it appears that the Hello World example is wrong,
trying to add an instance of a 'hello' object that doesn't exist. I've
not tried to run the example, but it certainly confused me when trying
to work out how Circuits works.

--
Ben Sizer
Nov 6 '08 #3
I D wrote:
Hello James,
Thanks for your response.
But I cannot use a third party software, I need to use the exisiting
API's within python.
As I am new to python, I suspected that I should go by a simpler
approach and so
scrapped off the below code and wrote a very simple UDP server code as
follows:

logFileName = '/home/msat/gsc/logs/' + compNum + '/logparsertraced ump.log'
logfile = open(logFileNam e, "w")
from socket import *
host = "121.3.0.1 <http://121.3.0.1>"
port = PORT
buf = 4096
addr = (host,port)
# Create socket and bind to address
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.setsock opt(SOL_SOCKET, SO_RCVBUF, 8388608)
UDPSock.bind(ad dr)
while 1:
data,addr = UDPSock.recvfro m(buf)
if not fileExists(logF ileName):
logfile = open(logFileNam e, "a")
logfile.writeli nes(data)
It would make more sense to leave the file open outside the loop. If
it's important to have each packet individually logged you can call the
file's .flush() method after each .writelines() call. No need to
continually re-open it. I doubt this is the source of your packet loss,
however. Maybe there's a bug in fileExists (for which you could have
used os.path.exists, by the way).
# Close socket
UDPSock.close()
How do you anticipate this line will ever be executed?

Your network code looks basically OK, though a little horrible with
things like 8388608 for the socket options. See if you get any ideas from

http://holdenweb.com/docs/NetProg.pdf

though it doesn't tell you much you don't appear to know already.

regards
Steve
Even this seems to lose packets, I would really appreciate if any
pointers can be provided to improve my code.

Thanks,
Sam

On Wed, Nov 5, 2008 at 7:46 PM, James Mills
<pr******@short circuit.net.au <mailto:pr***** *@shortcircuit. net.au>wrote:

On Thu, Nov 6, 2008 at 9:53 AM, <id*****@gmail. com
<mailto:id***** @gmail.com>wrot e:
logFileName = 'log.txt'
logfile = open(logFileNam e, "a")
class MyUDPServer(Soc ketServer.UDPSe rver):
def server_bind(sel f):
self.socket.set sockopt(socket. SOL_SOCKET,
socket.SO_RCVBU F, 8388608)
self.socket.bin d(self.server_a ddress)
>
class LogsDumpHandler (SocketServer.D atagramRequestH andler):
def handle(self):
global logfile
# fileExists is a function which checks the existence of a file
if not fileExists(logF ileName):
logfile = open(logFileNam e, "a")
logfile.writeli nes(self.rfile. readlines())
>
server = MyUDPServer(("" ,PORT), LogsDumpHandler )
server.serve_fo rever()
logfile.close()

This is horrible code :/

Try these instead:
* UDPServer ->
http://trac.softcircuit.com.au/circu...s/udpserver.py
* UDPClient ->
http://trac.softcircuit.com.au/circu...s/udpclient.py

Example usage
----------------------

~/circuits/examples
$ ./udpserver.py
127.0.0.1 <http://127.0.0.1/>, 9000:
test
127.0.0.1 <http://127.0.0.1/>, 9000:
hi

~/circuits/examples
$ ./udpclient.py -b 127.0.0.1:9000 <http://127.0.0.1:9000/>
127.0.0.1:8000 <http://127.0.0.1:8000/>
test
127.0.0.1 <http://127.0.0.1/>, 8000:
test
hi
127.0.0.1 <http://127.0.0.1/>, 8000:
hi

You can download circuits from http://trac.softcircuit.com.au/circuits/
or get the latest development version by using Mercurial (1):
hg clone http://hg.softcircuit.com.au/projects/circuits/

cheers
James

[1] http://www.selenic.com/mercurial/wiki/

--
--
-- "Problems are solved by method"

------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list

--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 6 '08 #4
On Fri, Nov 7, 2008 at 1:43 AM, Ben Sizer <ky*****@gmail. comwrote:
On Nov 6, 12:46 am, "James Mills" <prolo...@short circuit.net.au>
wrote:
>>
Try these instead:
* UDPServer ->http://trac.softcircuit.com.au/circu...s/udpserver.py
* UDPClient ->http://trac.softcircuit.com.au/circu...s/udpclient.py

Since there's no contact details on the Circuits site, and the guest
Trac account doesn't work (it asks the admin to verify the email),
then I'll post here: it appears that the Hello World example is wrong,
trying to add an instance of a 'hello' object that doesn't exist. I've
not tried to run the example, but it certainly confused me when trying
to work out how Circuits works.
Ben, thanks for your feedback! I've fixed this
now! I've also added come contact details for
myself on the website and fixed the guest
account! Thanks for taking an interest.

--JamesMills

--
--
-- "Problems are solved by method"
Nov 6 '08 #5

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

Similar topics

3
4172
by: Olivier Hoarau | last post by:
Hello, I have build a client/server application with the socket module. The server mades UDP broadcasting and the client only reads UDP broadcast messages. All work fine. Now I want to use for the same thing the socketserver module, it's ok for the client, but I don't succeed in making work the server :-(( Here is the client (which works)
3
4409
by: Ergin Aytac | last post by:
I'm trying to run a script written in python and have some socket connection problems. I cutted the origin script (more than 1000 lines) so it is only the part of the connection and there is no functionalty but connecting to a server. I have no idea about programming with python so every hint is a present for me :) ------------------------------------ import SocketServer import os
12
13409
by: Paul Rubin | last post by:
Let's say you have a SocketServer with the threading mix-in and you run serve_forever on it. How can you shut it down, or rather, how can it even shut itself down? Even if you use a handle_request loop instead of serve_forever, it still seems difficult: class myserver(ThreadingMixIn, TCPServer): pass server = myserver(...) server.shutdown = False while not server.shutdown: server.handle_request()
3
13916
by: Magnus Lycka | last post by:
I have a socket server like below which I want to exit when it's out of data. If I interrupt the client, I'll get a broken pipe on the server side, and after a Ctrl-C, I can restart the server again, but if I let it run out of data, and exit via handle_error as can be seen below, I will get a socket.error: (98, 'Address already in use') when I try to restart the server. (Unless I wait a minute or so, or use another port.) I feel like I'm...
4
5270
by: google | last post by:
Dear newsgroup, I give up, I must be overseeing something terribly trivial, but I can't get a simple (Java) applet to react to incoming (python) SocketServer messages. Without boring you with the details of my code (on request available, though), here is what I do : I have a TCPServer and BaseRequestHandler .
8
5483
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
12
2121
by: rbt | last post by:
I have written a python socketServer program and I have a few questions that I hope the group can answer... here is a simple version of the server: class tr_handler(SocketServer.StreamRequestHandler): def handle(self): data = self.rfile.readline(300) data = str.strip(data)
7
2503
by: Mahesh Devjibhai Dhola | last post by:
Hi, I want to develop p2p (peer-to-peer) communication connection for chat in ..Net (Lang: c# - preferable) NOTE: if two LAN are behind their own router then also it should work as yahoo, msn messenger works. Let me explain scenario: In yahoo: - We login to yahoo server from yahoo messenger (yahoo server listens at
5
2279
by: eliben | last post by:
Hello, I have a small wxPython application. Today I was trying to add some RPC capability to it, so I implemented an instance of SimpleXMLRPCServer that runs in a separate thread when invoked and answers requests. All went fine until I realized that I have to sometimes stop the server - which is where I ran into a problem. Python threads can not be killed after they've been started. They can be kindly requested to
0
9543
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
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
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
10237
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
10029
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...
1
7567
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
6808
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();...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
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.