I don't have an example to show. I referred some online site, but didn't find anything that met my requirement. Please anyone give me an example to use.
-
Unfortunately, Stack Overflow is not a code writing service. You have to try something. Help center has more information about the site policy and guidelines.Lev Levitsky– Lev Levitsky2014-06-25 11:44:53 +00:00Commented Jun 25, 2014 at 11:44
-
u first store the array in a file and send it over tcpsundar nataraj– sundar nataraj2014-06-25 11:48:50 +00:00Commented Jun 25, 2014 at 11:48
-
stackoverflow.com/questions/19412029/… check this to send filesundar nataraj– sundar nataraj2014-06-25 11:57:36 +00:00Commented Jun 25, 2014 at 11:57
Add a comment
|
2 Answers
There are multiple options. I would try:
- instead of large array, try to get a generator or iterator which would provide items one by one. This could save you memory in sending process (but is is not necessary, if you can live with whole array in memory)
- do not use plain sockets, use ZeroMQ (which runs on top of it).
- define a protocol for sending the array. Assuming the array is flat, it can go with following types of messages:
- startarray
- itemdata
- endarray
- loop over items to send and send them as one by one, before sending "startarray" message, then one item by one, finally closing by "endarray" message
- for transfer over TCP, you need to pick some serializatio format. I would start with JSON.
- sender could use ZMQ socket of type PUSH, receiver would be using PULL.
Comments
You can find a minimum working example of how to achieve that in the python wiki.
For completeness, I have copied the code from the above mentioned site. You create a server:
#!/usr/bin/env python
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connection address:', addr
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
print "received data:", data
conn.send(data) # echo
conn.close()
And a client:
#!/usr/bin/env python
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()
print "received data:", data