How to open and close python socket -


i have c++ program reads sensor data gpio rpi. want pass data computer on ethernet (cannot wi-fi).

i calling python script cpp post 1 data byte socket.

i want close socket , end py script, next read in cpp, call same python script again reopen same socket , post next data. how make script modular?

additionally (optional)

  • (1) how do ubuntu sockets in cpp?
  • (2) how pass cpp variable transmit python script data in cpp program?

the below script transmit , receive work great in continuous mode, i'd more async method this. if attempt close socket , reopen it, errors stating open , like.

i can send data on ethernet using

#!/usr/bin/python import socket host='169.254.72.2' port=2225 s=socket.socket(socket.af_inet, socket.sock_stream) s.bind((host, port)) s.listen(1) conn, addr=s.accept() s.setsockopt(socket.ipproto_tcp, socket.tcp_nodelay, 1) print 'connected by', addr while true:         conn.send('5') s.close() 

my receive python script (client) on pc this:

import socket host='169.254.72.2' port=2225 s=socket.socket(socket.af_inet, socket.sock_stream) s.connect((host, port)) while 1:     a=time.clock()     data=s.recv(1)     print data s.close() 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -