# Using socket() and bind() \[basic transmission questions\]

**URL:** <https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658>\
**Category:** Python\
**Created:** [July 23, 2008, 7:29pm UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658 "2008-07-23T19:29:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikeB](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@MikeB](https://forums.digi.com/u/MikeB)\
**Post date:** [July 23, 2008, 7:29pm UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658/1 "2008-07-23T19:29:16Z")

</div>

What I’m trying to do is use the ConnectPortX gateway to set up a network with two additional endpoints. I simply want to discover the two devices and send them an 8 bit command.

I’m looking through the zb\_ex4\_disc.py and zb\_ex2\_rw.py example files and trying to make sense of their code so I can manipulate it to suit my demo application.

I understand most of ex4\_disc, in that it simply gives me a two variable array with the nodes and its respective data. The print part is helpful but the list is really what I need.

So I assume that at this point I have enough information to send out data, I have the short and long addresses for my zigbee radios. So moving on to ex2\_rw, and the stuff I don’t completely understand; sockets. I was trying to read some basic stuff about Python and sockets but it was a bit over my head with TCP/UDP and that or it wasn’t transparent/evident enough what the code was doing. Now, first the code creates a socket:  
sd = socket(‘’,‘’,‘’)

Now I would assume I have to do this for each endpoint:  
ep1= socket(‘’,‘’,‘’)  
ep2= socket(‘’,‘’,‘’)

Now comes the part that I don’t get  
sd.bind((“”,0xe8,0,0))

I haven’t really figured out what relevance ‘bind’ has, does it send the end address 0xe8 to the socket I’ve created? So that in my code it would be something like:  
ep1.bind((“”,ep1\_short\_address,0,0))?

The last part of the example code is:  
sd.sendto(data, 0, src\_addr)

Now data will obviously be my 8 bit code, the 0 is for non-blocking but the problem I have is with src\_addr. When looking at the Digi Python handbook I see that the command sendto() the tuple addr format is (addr\_string, endpoint, profile\_id, cluster\_id) and the node class itself contains: type, addr\_extend, addr\_short, addr\_parent,profile\_id,manufact\_id,label) so my question would be, can I pass just addr\_short to it or should I pass everything it wants? Something like:  
ep1.sendto(8bit,0,0x34)?

Or should I set up a ep1\_addr = [node.addr\_string,0x34,node.profile\_id,node.cluster\_id]

and call ep1.sendto(8bit,0,ep1\_addr) ?

Thanks in advance.  
Mike

---

<div class="post-metadata">

**Author:** ![jevered\_at\_esquaredi](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@jevered\_at\_esquaredi](https://forums.digi.com/u/jevered_at_esquaredi)\
**Post date:** [September 4, 2008, 4:49pm UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658/2 "2008-09-04T16:49:02Z")

</div>

I don’t know if you have figured out your issue yet (I hope so), but I am in about the same place and thought I would share what works for me. I have the endpoint configured with a loop back connector, so that what I send to it gets echoed back. I have also connected a terminal at the endpoint and verified that “Hello, World!” is really being sent out of the remote serial port.

I loaded the embedded\_kit\_gateway.zip library and zigbee.py on my ConnectPort X2 and then wrote my own test.py file and downloaded it as well. I then telnetted to the ConnectPort and typed in “py Test.py” to execute my test. The result was a display of all of my Zigbee nodes, followed by “Hello, World!” followed by “All Done Now”.

Test.py is as follows:

import sys

sys.path.append(“WEB/python/embedded\_kit\_gateway.zip”)

import zigbee

from socket import \*

try:

```
#
# Perform a node discovery and print out
# the list of discovered nodes to stdio.
#

node_list = zigbee.getnodelist()

# Print the table:
print "%12s %12s %8s %24s" % \
("Label", "Type", "Short", "Extended")
print "%12s %12s %8s %24s" % \
("-" * 12, "-" * 12, "-" * 8, "-" * 24)
for node in node_list:
	print "%12s %12s %8s %12s" % \
	(node.label, node.type, \
	node.addr_short, node.addr_extended)

# Use a discovered addresses as endpoint:
DESTINATION=("00:13:a2:00:40:52:a1:c2!", \
0xe8, 0xc105, 0x11)

# Create the socket, proprietary transport:
sd = socket(AF_ZIGBEE, SOCK_DGRAM, ZBS_PROT_TRANSPORT)

# Bind to endpoint 0xe8 (232):
sd.bind(("", 0xe8, 0, 0))

# Send "Hello, World!" to the endpoint,
# using profile_id and cluster_id specified in
# DESTINATION:
sd.sendto("Hello, World!", 0, DESTINATION)	    
response,src_addr=sd.recvfrom(72)
print response
print "All Done Now"

```

except Exception, e:  
print e

For the most part, this code was pulled from the Digi Python Programming Guide, so I can take no credit for it, but it was not clear to me that I needed embedded\_kit\_gateway.zip or that I needed to extend the path to it to make things work.

I am now trying to figure out how to\_socket\_addr() is used so that I do not need to hard code the DESTINATION.

If anyone has an example of the usage of to\_socket\_addr() I would be very thankful.

Jay

---

<div class="post-metadata">

**Author:** ![matalalento](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@matalalento](https://forums.digi.com/u/matalalento)\
**Post date:** [June 2, 2009, 10:19am UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658/3 "2009-06-02T10:19:06Z")

</div>

Hello World!

Here is another new xbee user having problems with bind() and basic transmissions. I have tried simply telnetting the gateway and driving simple python code which attempts to send message to a xbee board with a loopback tester connected to it. The code is as follows:

# include the sockets module into the namespace:

import zigbee  
from socket import \*  
print 0  
DESTINATION = (“00:13:a2:00:40:34:1a:88!”,   
0xe8, 0xc105, 0x11)  
print 1

# Create the socket, datagram mode, proprietary transport:

sd = socket(AF\_ZIGBEE, SOCK\_DGRAM, ZBS\_PROT\_TRANSPORT)  
print 2

# Bind to endpoint 0xe8 (232):

sd.bind((“”, 0xe8, 0, 0))  
print 3  
sd.sendto(“Hello”, 0, DESTINATION)  
print 4

# Block until a single frame is received, up to 72 bytes:

response, src\_addr = sd.recvfrom(72)  
print 5  
print response

* * *

Telnet says:

## 0 1 2 Traceback : File “”, line 21, in ? File “”, line 1, in bind socket.error: \<22, ‘Invalid argument’\>

(line 21 is “sd.bind((”“, 0xe8, 0, 0))” )

* * *

If I delete the line 21 Telnet says:

## 0 1 2 3 Traceback : File “”, line 24, in ? socket.error: \<9, ‘Bad file number’\>

(line 24 is “sd.sendto(“Hello”, 0, DESTINATION)” )

Since the code is very close to the example one, I have no idea what could be wrong. Does anyone have hints for me what I am doing wrong? The gateway sees the 00:13:a2:00:40:34:1a:88! as a router in the Xbee network.

For 00:13:a2:00:40:34:1a:88! (radio module in Rs232 interface board) in the X-CTU there is  
Modem: XB24-B  
Function Set: Zigbee Router/END DEVICE AT  
Version: 1220

Gateway is  
X8 Gateway  
Firmware: 0x1142

I have uploaded python.zip to the gateway. I also did go trough the link posted above but I didnt find help there.

I appreciate any help or hints, Thanks!

-Matalalento-

---

<div class="post-metadata">

**Author:** ![Benno75](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@Benno75](https://forums.digi.com/u/Benno75)\
**Post date:** [October 11, 2008, 2:53pm UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658/4 "2008-10-11T14:53:12Z")

</div>

here’s an example of to\_socket\_addr. It gets the node list and sends a message to all the routers.

sd = socket(AF\_ZIGBEE, SOCK\_DGRAM, ZBS\_PROT\_TRANSPORT)

# Bind to endpoint 0xe8 (232):

sd.bind((“”, 0xe8, 0, 0))

# Perform a node discovery:

node\_list = zigbee.getnodelist()

for node in node\_list:  
if node.type == “router”:  
print “Yay we found a router!!”  
dest = node.to\_socket\_addr()  
print “dest is:”  
print dest  
print “sending data to router.”  
x = sd.sendto(“Hello, router!”, 0, dest)  
print “bytes sent:”  
print x

---

<div class="post-metadata">

**Author:** ![kafka](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kafka](https://forums.digi.com/u/kafka)\
**Post date:** [May 7, 2009, 8:06am UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658/5 "2009-05-07T08:06:45Z")

</div>

I’ve got ConnectPort and XbeePro USB module. After executing aboce script I was hoping to get “Hello world” in X-CTU terminal, but I didn’t get anything.

Q: How to do that and how to receice response?

---

<div class="post-metadata">

**Author:** ![mkotasek](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mkotasek](https://forums.digi.com/u/mkotasek)\
**Post date:** [May 7, 2009, 2:09pm UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658/6 "2009-05-07T14:09:42Z")

</div>

Hi Kafka,

You may want to start at some of the basic XBee network on the Wiki:

[http://www.digi.com/wiki/developer/index.php/ZigBee\_Extensions\_to\_the\_Python\_socket\_API](http://www.digi.com/wiki/developer/index.php/ZigBee_Extensions_to_the_Python_socket_API)

They should help guide you through the send and receive functionality.

---

<div class="post-metadata">

**Author:** ![52637p](https://avatars.discourse-cdn.com/v4/letter/5/c6cbf5/32.png) [@52637p](https://forums.digi.com/u/52637p)\
**Post date:** [June 2, 2009, 11:28am UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658/7 "2009-06-02T11:28:43Z")

</div>

Start by confirming that no other Python scripts are running in the system, potentially bound to the same endpoint. The units ship from the factory with a sample script installed. From the Digi CLI, you can execute the “set python” command to determine whether any scripts are enabled to auto-start, and to disable them if they are. A reboot is recommended after changing the “set python” configuration.

---

<div class="post-metadata">

**Author:** ![matalalento](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@matalalento](https://forums.digi.com/u/matalalento)\
**Post date:** [June 3, 2009, 4:24am UTC](https://forums.digi.com/t/using-socket-and-bind-basic-transmission-questions/2658/8 "2009-06-03T04:24:55Z")

</div>

Hey again and thank you for the quick response! There was zb\_tcp\_serial.py running on gateway by default. I turned it off and now this problem got solved. Thanks again!

-Matalalento-
