Hi,
I want to make a UDP client server application that is conform to
RFC868 (Time protocol). Both the UDP client and UDP server are in a
test phase.
The question is: when I add "svrsocket.sendto(resultaat, (ip, port))"
in the UDP server, my application closes while running. When I leave
it away, it works fine. I really need this statement as the purpose is
that a client sends sth to the server and the server sends back the
time ticks.
Anybody an idea how I can modifu my client/server so that it works?
Regards,
Wim
Here is the server:
# UDP server example
import time
import socket
import string
import string
class Tijd:
def __init__(self, hours=0,minutes=0,seconds=0):
self.hours=hours
self.minutes=minutes
self.seconds=seconds
def aantal_seconden(self):
x = time.time()
y=(1970, 1, 1, 1, 0, 0, 0, 0, 0)
y=time.mktime(y)
resultaat=x-y
return resultaat
if __name__=="__main__":
tijd=Tijd()
resultaat=tijd.aantal_seconden()
print 'resultaat',resultaat
port=37
svrsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
svrsocket.bind(('', port))
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
print 'TOD server is at IP adress: ', ip
tijd=time.ctime()
tijd=string.split(time.ctime())
print 'The current time is', tijd[3]
print 'Listening for TOD-requests on port %s ...' %port
while 1:
data, address = svrsocket.recvfrom(256)
print 'Received a TOD-request from modem with IP-address %s'
%address[0]
print 'Sending back the time to modem with
IP-address',address[0]
print "time", resultaat
svrsocket.sendto(resultaat, (ip, port))
And here is the client:
# UDP client example
import socket
port=37
clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while 1:
data = raw_input("Type something: ")
if data:
clisocket.sendto(data, ("127.0.0.1", port))
else:
break
s.close() 2 13226
WIWA wrote: The question is: when I add "svrsocket.sendto(resultaat, (ip, port))" in the UDP server, my application closes while running. When I leave it away, it works fine. I really need this statement as the purpose is that a client sends sth to the server and the server sends back the time ticks.
It would have helped if you were more specific about : "closes while running"?
In my case (running your code) I get an exception in the server:
Traceback (most recent call last):
File "server.py", line 44, in ?
svrsocket.sendto(resultaat, (ip, port))
File "<string>", line 1, in sendto
TypeError: sendto() takes exactly 3 arguments (2 given)
this pretty much tells you what's wrong. Your sendto() call is faulty.
The first argument in your case is of type <float> while it should be
a <string>. So try
svrsocket.sendto(str(resultaat), (ip, port))
however, this triggers a loop in your code if your client is on the
same machine as the server (the server sends out a UDP packet to the
same port as it itself is listening on, on my machine, it gets back
its own UDP packet....)
HTH,
--Irmen de Jong
This should work:
Server
======
import socket
import time
# Constants.
PORT = 37
# Create and bind the socket which listens for packets.
srvsocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
svrsocket.bind(('',PORT))
while 1:
# Block waiting for packet.
data, address = svrsocket.recvfrom(256)
print "Client sent:", data
print "Client at:", address
# Got a packet, reply to address packet came from.
srvsocket.sendto(str(time.time()),address)
Client
======
import socket
# Constants.
PORT = 37
# Create new socket, let it bind to an OS-chosen port.
clisocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while 1:
data = raw_input("Enter what the server receives.")
if data:
# Send data to server, irrelevant what.
clisocket.sendto(data,("localhost",PORT))
# Block waiting for reply.
data, address = clisocket.recvfrom(256)
print "Server sent time:", data
else:
break
Look at the above code. There were several errors in your original
implementation, e.g. that time never got updated while the server ran,
and several other things. I also don't see the need for a Tijd class.
HTH!
Heiko.
PS: The above code is untested, if there's something wrong, my bad... :)
PPS: Read the UNIX TCP/IP Network Programming FAQ if you're into socket
programming! Google for it, I currently don't know the URL... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris |
last post by:
<apologies for cross-posting>
Hi All,
I am based in the UK and have been doing some private work for a
client which involved setting up a database and scripts to search it
and display results...
|
by: Dustin |
last post by:
I've been trying to create a "byte streaming server", it's basically a
chat server, but I'd like to use it for transfering large amounts of
data between two connected clients. Sounds simple...
|
by: Rhino |
last post by:
I am trying to verify that I correctly understand something I saw in the DB2
Information Center.
I am running DB2 Personal Edition V8.2.1 on Windows. I came across the
following in the Info...
|
by: Paul H |
last post by:
How do you folks get a reliable and complete brief of what is required
before development starts?
I am forever going back to a client once a project has started saying "Hang
on, now that I've...
|
by: Harry Smith |
last post by:
While reading the documentation on IsStartupScriptRegistered, there is a
reference to "client startup script" as "Determines if the client startup
script is registered with the Page object."
What...
|
by: Nalaka |
last post by:
Hi,
I created a sinple web service that returns a dataSet.
Then I created a client program that uses this web service (that returns the
Dataset).
My question is, how did the client figure...
|
by: pshindle |
last post by:
We have several machines currently running the DB2 V7 Run-time Client
that we would like to actually be running the App Dev Client. To
'upgrade' (within the same version) this client software can...
|
by: rjha94 |
last post by:
Hi
I just installed the runtime client on my windows machine. when i go
inside the SQLLIB\bin folder i can see db2.exe. is it possible to use
this db2 bundled with runtime client for command line...
|
by: MMcCarthy |
last post by:
http://bytes.com/images/howtos/projectscope_blocks.jpgAs a freelance IT consultant for over 10 years, I’ve come to appreciate well defined project scopes. A project scope is a common understanding...
|
by: Bill Davy |
last post by:
I am trying to edit Contacts in Outlook. This is so I can transfer numbers
from my address book which is an Excel spreadsheet to my mobile phone. I
came across the following snippet of code which...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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: 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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |