473,729 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UDP socket, need help setting sending port

I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux
with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also
switch to Linux for development if necessary.

I am writing some python to replace proprietary software that talks to a
timeclock via UDP.

The timeclock extracts the sending port from the UDP header and uses that
for all response messages.

I cannot find out how to set the sending port in the header. Windows XP
appears to set an arbitrary port. I've been using ethereal to analyze
network traffic and it seems that if I can set the sending port, I should be
OK.

I have been googling various combinations of "python udp ..." for the last
two hours and have not found anything that addresses how to set the sending
port. I'm guessing that this may be in setsockopt but don't see any
parameters that "click".

Any help would be greatly appreciated.

Fred Sells
fred at adventistcare dotttttt org
---------------------------------------------------------------------------
The information contained in this message may be privileged and / or
confidential and protected from disclosure. If the reader of this message is
not the intended recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited. If you
have received this communication in error, please notify the sender
immediately by replying to this message and deleting the material from any
computer.
---------------------------------------------------------------------------
Dec 20 '05 #1
3 11341
Fred,

It is quite possible I've misunderstood the problem :-) but have
you tried anything like

import socket
tc_local_port = 9999
tc_remote_port = 9999
outgoing_if = "172.16.1.2 " # say
remote_tc_host = "172.16.1.3 " # say
# udp is the default for DGRAM
tc_sock = socket(socket.A F_INET, socket.SOCK_DGR AM)
tc_sock.bind((o utgoing_if, tc_local_port))
tc_sock.connect ((remote_tc_hos t, tc_remote_port) )

If you send data with tc_sock, it should have a source port of
tc_local_port.
So, to set the source port, call bind. You should do that before
calling connect, since
calling connect on an unbound socket has the side effect of
assigning an ephemeral port to the socket.
Hope that was of some help to you.

All the best,

Keir.

Dec 20 '05 #2
A few trivial corrections, to my own post :-(
tc_sock = socket(socket.. .
should be
tc_sock = socket.socket(s ocket...
of course

and, (while I'm here) when I stated that calling connect on an
unbound socket caused
a ephemeral port to be assigned, I should have written "calling connect
on an unbound _UDP_ socket, etc. "

Cheers,

Keir.

Dec 20 '05 #3
Sells, Fred wrote:
I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux
with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also
switch to Linux for development if necessary.

I am writing some python to replace proprietary software that talks to a
timeclock via UDP.

The timeclock extracts the sending port from the UDP header and uses that
for all response messages.

I cannot find out how to set the sending port in the header. Windows XP
appears to set an arbitrary port. I've been using ethereal to analyze
network traffic and it seems that if I can set the sending port, I should be
OK.

I have been googling various combinations of "python udp ..." for the last
two hours and have not found anything that addresses how to set the sending
port. I'm guessing that this may be in setsockopt but don't see any
parameters that "click".

Try binding the address ('',0) which means any address, any port
on this box. Then get the bound address with getsockname(). Below
is a copy of an interactive try-out...
import socket
s = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
s.bind(('',0))
s <socket._socket object object at 0xb7d9ee0c> s.getsockname() ('0.0.0.0', 32775)


So in this case, port 32775 was chosen to bind to.

Steve
Dec 22 '05 #4

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

Similar topics

1
16613
by: Chuck E. Cheese | last post by:
I need a php page to connect to a python created socket and sent and receive data. below is the python code which opens a socket on the localhost @ port 21567: #!/usr/bin/python2 from socket import * from time import time,ctime HOST ='127.0.0.1' PORT = 21567
3
4636
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data and then fires a datareceived event. Within the waitingloop there is a timeout function, but I want the the 'last-time-socket-used' variable set when the socket is finished sending. When I send by System.Net.Sockets.Socket.Send(buffer()) (<--this...
5
3683
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic. My programming environment is Windows XP, Visual Studio .NET 2003 and C#. So here it...
1
3396
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This class(server) can be started or stopped by calls to the appropriate functions. The server class has...
2
702
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no more data to expect). Sometimes, Socket.Available returns 0 but the other end of the connection did not close it ! 2) This class will be used by many other classes so I have to use the
13
2645
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my problem is executing the transactions against the remote server and returning the response to the remoting client. i can open the socket fine and, if i am executing one transaction at a time, everything works great. it's when my proxy server...
9
5555
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame. But the "app.MainLoop()" in wxpython looks like conflicting with the "while 1:" in socket server. After I commented the "app.MainLoop()", everything is working except two things: 1. if I click anywhere on the screen with the mouse, the image is...
6
4655
by: White Spirit | last post by:
I have the following code to send a packet to a remote socket and receive a response in return: System.Net.Sockets.Socket locSocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); System.Net.Sockets.Socket remSocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); locSocket.Bind (new IPEndPoint (locIP, 2126)); /*
2
3673
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds smoothly without any errors but,sometimes I get the following error. "An existing connection was forcibly closed by the remote host." Why is this happening when i am not closing the client program? Could someone guide me in this issue
0
8761
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,...
1
9200
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
9142
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
6722
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
6016
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
4525
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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 we have to send another system
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.