Moreover, I would like to know about function Comms:Networking. what is difference between Network Communications and TCP/IP (Raspberry Pi). That I confuse, can I use TCP/IP (Raspberry Pi) communicate between Pi and PC? or just use only Pi to Pi. Addition, can I use Network Communication function over PI. Please, explain to me.
Thank.
In below is python code that I search form internet.
PC_Client.py
Code: Select all
import socket
import sys
while(1):
HOST, PORT = "192.168.2.45", 5052
data ="Test connection from Radio 3"
data=input()
#Create socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
#connect to server and send data
sock.connect((HOST,PORT))
sock.sendall(bytes(data+"\n","utf-8"))
#receive data from the server and shut down
received = str(sock.recv(1024),"utf-8")
print("Sent: {}".format(data))
print("Received:{}".format(received))
Code: Select all
import socketserver
import sys
class MyTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
#print(self.data)
#print(str(self.data),"utf-8")
print("{}".format(self.data))
# just send back the same data, but upper-cased
#self.request.sendall(self.data.upper())
self.request.sendall(bytes("Accept from server!!","utf-8"))
####Send to another instrumentd
if __name__ == "__main__":
HOST, PORT = "192.168.2.45", 5052
# Create the server, binding to localhost on port 9999
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()