-4

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.

3
  • 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. Commented Jun 25, 2014 at 11:44
  • u first store the array in a file and send it over tcp Commented Jun 25, 2014 at 11:48
  • stackoverflow.com/questions/19412029/… check this to send file Commented Jun 25, 2014 at 11:57

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.