Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to send/receive data over TCP
I have tow system one is system A and anothr is system B

System A - Server (pi)
System B - Client (printer)

I want to send a request to the printer
if request accepted, get the data from the printer

I found the two program but I don't understand which one used for my purpose

Here's simple code to send and receive data by TCP in Python
#!/usr/bin/env python

import socket

TCP_IP = ''
TCP_PORT = 5005
MESSAGE = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
data = s.recv(BUFFER_SIZE)

print "received data:", data
Here's simple code to serve TCP in Python:
#!/usr/bin/env python

import socket

TCP_IP = ''
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))

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

write a socket programming with python to send and recive a file over tcp
Here is a part to send a part of data based on the size of the buffer.

msg[i] = file[i].read()
while 1:
tdata[i], msg[i] = msg[i][:buf], msg[i][buf:]

if len(msg[i]) < buf:

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Send Pressure sensor data to IoT platform using WebSockets in Python barry76 3 701 Mar-12-2019, 09:48 AM
Last Post: barry76
  Send data BMP180 between client and server trought module socket smalhao 0 710 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Python socket : Error receive data quanglnh1993 1 3,478 Mar-14-2018, 11:59 AM
Last Post: avorane
  Socket won't receive data suddenly with multiple clients SquareRoot 0 1,065 Sep-06-2017, 09:09 PM
Last Post: SquareRoot

Forum Jump:

Users browsing this thread: 1 Guest(s)