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

sockets -- basic udp client

My question pertains to this example:

#!/usr/bin/env python

import socket, sys, time

host = sys.argv[1]
textport = sys.argv[2]

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
port = int(textport)
except ValueError:
# That didn't work. Look it up instread.
port = socket.getservbyname(textport, 'udp')

s.connect((host, port))
print "Enter data to transmit: "
data = sys.stdin.readline().strip()
s.sendall(data)
s.shutdown(1)
print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
while 1:
buf = s.recv(2048)
if not len(buf):
break
print "Received: %s" % buf
As far as I can tell, the if statement:

if not len(buf):
break

does nothing. Either recv() is going to read some data or it's going
to block. My understanding is that udp sockets do not have a
connection, so the server can't close the connection--hich would cause
a blank string to be sent to the client.

So, as far as I can tell, the only way that code would make sense is
if the server were programmed to send a blank string to the client
after it sent data to the client. Is that correct?
Feb 15 '08 #1
6 4886
En Fri, 15 Feb 2008 20:24:19 -0200, 7stud <bb**********@yahoo.com>
escribió:
My question pertains to this example:

#!/usr/bin/env python

import socket, sys, time

host = sys.argv[1]
textport = sys.argv[2]

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
port = int(textport)
except ValueError:
# That didn't work. Look it up instread.
port = socket.getservbyname(textport, 'udp')

s.connect((host, port))
print "Enter data to transmit: "
data = sys.stdin.readline().strip()
s.sendall(data)
s.shutdown(1)
print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
while 1:
buf = s.recv(2048)
if not len(buf):
break
print "Received: %s" % buf
As far as I can tell, the if statement:

if not len(buf):
break

does nothing. Either recv() is going to read some data or it's going
to block. My understanding is that udp sockets do not have a
connection, so the server can't close the connection--hich would cause
a blank string to be sent to the client.

So, as far as I can tell, the only way that code would make sense is
if the server were programmed to send a blank string to the client
after it sent data to the client. Is that correct?
That example is plain wrong; looks like some TCP code but with SOCK_STREAM
blindy replaced with SOCK_DGRAM. connect, sendall and recv are not used
for UDP; sendto and recvfrom are used instead. There are some examples in
the Demo python directory.

--
Gabriel Genellina

Feb 16 '08 #2
On Feb 15, 6:48*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 15 Feb 2008 20:24:19 -0200, 7stud <bbxx789_0...@yahoo.com*
escribió:
My question pertains to this example:
#!/usr/bin/env python
import socket, sys, time
host = sys.argv[1]
textport = sys.argv[2]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
* * port = int(textport)
except ValueError:
* * # That didn't work. *Look it up instread.
* * port = socket.getservbyname(textport, 'udp')
s.connect((host, port))
print "Enter data to transmit: "
data = sys.stdin.readline().strip()
s.sendall(data)
s.shutdown(1)
print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
while 1:
* * buf = s.recv(2048)
* * if not len(buf):
* * * * break
* * print "Received: %s" % buf
As far as I can tell, the if statement:
if not len(buf):
* *break
does nothing. *Either recv() is going to read some data or it's going
to block. * My understanding is that udp sockets do not have a
connection, so the server can't close the connection--hich would cause
a blank string to be sent to the client.
So, as far as I can tell, the only way that code would make sense is
if the server were programmed to send a blank string to the client
after it sent data to the client. *Is that correct?

That example is plain wrong; looks like some TCP code but with SOCK_STREAM*
blindy replaced with SOCK_DGRAM. connect, sendall and recv are not used *
for UDP; sendto and recvfrom are used instead. There are some examples in *
the Demo python directory.

--
Gabriel Genellina
On Feb 15, 6:48*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 15 Feb 2008 20:24:19 -0200, 7stud <bbxx789_0...@yahoo.com*
escribió:
My question pertains to this example:
#!/usr/bin/env python
import socket, sys, time
host = sys.argv[1]
textport = sys.argv[2]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
* * port = int(textport)
except ValueError:
* * # That didn't work. *Look it up instread.
* * port = socket.getservbyname(textport, 'udp')
s.connect((host, port))
print "Enter data to transmit: "
data = sys.stdin.readline().strip()
s.sendall(data)
s.shutdown(1)
print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
while 1:
* * buf = s.recv(2048)
* * if not len(buf):
* * * * break
* * print "Received: %s" % buf
As far as I can tell, the if statement:
if not len(buf):
* *break
does nothing. *Either recv() is going to read some data or it's going
to block. * My understanding is that udp sockets do not have a
connection, so the server can't close the connection--which would cause
a blank string to be sent to the client.
So, as far as I can tell, the only way that code would make sense is
if the server were programmed to send a blank string to the client
after it sent data to the client. *Is that correct?

That example is plain wrong; looks like some TCP code but with SOCK_STREAM*
blindy replaced with SOCK_DGRAM. connect, sendall and recv are not used *
for UDP; sendto and recvfrom are used instead. There are some examples in *
the Demo python directory.

Yes, I agree it's a poor example--it's from 'Foundations of Python
Network Programming'--but it does 'work'. It also doesn't appear to
be a tcp client that was converted too directly into a udp client
because the previously presented tcp examples are different.

Here is the example above converted to a more straightforward udp
client that isolates the part I am asking about:

import socket, sys

host = 'localhost' #sys.argv[1]
port = 3300
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = 'hello world'
num_sent = 0
while num_sent < len(data):
num_sent += s.sendto(data, (host, port))
print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
while 1:
buf = s.recv(2048)

#Will the following if statement do anything?
if not len(buf):
break

print "Received from server: %s" % buf

Another question I have pertains to the docs here:

getservbyname(servicename[, protocolname])
Translate an Internet service name and protocol name to a port number
for that service. The optional protocol name, if given, should be
'tcp' or 'udp', otherwise any protocol will match.
What does a 'servicename' look like?
Feb 16 '08 #3
On Feb 16, 6:32*am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
That example is plain wrong; looks like some TCP code but with *
SOCK_STREAM *
blindy replaced with SOCK_DGRAM. connect, sendall and recv are not used*
for UDP; sendto and recvfrom are used instead. There are some examples *
in *
the Demo python directory.
Yes, I agree it's a poor example--it's from 'Foundations of Python
Network Programming'--but it does 'work'. *It also doesn't appear to
be a tcp client that was converted too directly into a udp client
because the previously presented tcp examples are different.

Ok, you *can* use those functions with datagrams too, but it's confusing *
(at least for the very first UDP example!)
Yes, I agree. The book is well written, but the introductory examples
and a lack of explanation of the finer details is a disaster.

Another question I have pertains to the docs here:
getservbyname(servicename[, protocolname])
Translate an Internet service name and protocol name to a port number
for that service. The optional protocol name, if given, should be
'tcp' or 'udp', otherwise any protocol will match.
What does a 'servicename' look like?

pyimport socket
pysocket.getservbyname("http")
80
pysocket.getservbyname("smtp")
25

On Linux the mapping ports<->services is in /etc/services; on Windows see *
%windir%\system32\drivers\etc\services

--
Gabriel Genellina
Thanks.
Feb 16 '08 #4
If you don't care about the address of the sender, e.g. you are not
going to send anything back, is there an advantage to using recv()?
Or, as a matter of course should you always use recvfrom() with udp
sockets?
I don't know of a reason why you couldn't use recvfrom() all the time,
and that is what I do. However, I also don't see a reason why you
should
not use recv() if you don't care who the message comes from.
Historically,
though, the ultimate authority on this kind of stuff is

Richard Stevens and his Unix and TCP/IP books

I recommend these books if you want to get into network programming.

Cheers,
Bob

Feb 17 '08 #5
"rl****@gmail.com" <rl****@gmail.comwrites:
Historically, though, the ultimate authority on this kind of stuff is
Richard Stevens and his Unix and TCP/IP books

I recommend these books if you want to get into network programming.
I keep wanting to get that book, but it gets older and older. Have
things really not changed since it was written?
Feb 17 '08 #6
Paul Rubin wrote:
"rl****@gmail.com" <rl****@gmail.comwrites:
>Historically, though, the ultimate authority on this kind of stuff is
Richard Stevens and his Unix and TCP/IP books

I recommend these books if you want to get into network programming.

I keep wanting to get that book, but it gets older and older. Have
things really not changed since it was written?
TCP is much like a funicular railway: it's been around long enough that
the basic engineering principles are known and the basic bugs have been
ironed out. Stevens is still an excellent reference.

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

Feb 17 '08 #7

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

Similar topics

4
by: Mike Dole | last post by:
I'm working on a client - server application based on the 'How to Sockets Server and How to Sockets Client' code from the Visual Basic ..NET Resource Kit. Since I want to be able to send 'big...
6
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
1
by: Chad | last post by:
I have developed a Sockets application using .NET Sockets. The only problem I have with my application is that it will sometimes lose part of the TCP/IP Message. The message I am sending is 963...
3
by: Terry Olsen | last post by:
I'm wondering if a winsock class has been written that functions similar to the winsock.ocx from VB6.
3
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...
2
by: Dave Dean | last post by:
Hi all, I'm just starting out in sockets/network programming, and I have a very basic question...what are the 'security' implications of opening up a socket? For example, suppose I've written a...
2
by: =?Utf-8?B?U2Vhbk1hYw==?= | last post by:
I am familiar with how to use winsock in vb6 to create a network app. I'm trying to find a way to take the current vb6 app and create a windows service using system.net.sockets. How do you create...
0
by: nr100582 | last post by:
Hi, I'm building an application for versioning purpose (in C# .Net 2.0). Though this versioning system is different from our regular software versioning applications like WinCvs, VSS etc (as my...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.