From dc60475fc9d081f08c2a43a23dceec1ee25f149d Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Sat, 26 Sep 2020 12:41:40 +0100 Subject: [PATCH] basic client/server socket example --- src/soc/debug/test/jtagremote.py | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/soc/debug/test/jtagremote.py diff --git a/src/soc/debug/test/jtagremote.py b/src/soc/debug/test/jtagremote.py new file mode 100644 index 00000000..365c4a45 --- /dev/null +++ b/src/soc/debug/test/jtagremote.py @@ -0,0 +1,60 @@ +#The server code +import socket +from socket import close, AF_INET, SOCK_STREAM +import sys +import select +import time + + +def server(): + HOST = '' + PORT = 9999 + s = socket.socket(AF_INET, SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind((HOST, PORT)) + s.listen(1) #only needs to receive one connection (the client) + return s + +def get_connection(s): + r, w, e = select.select( [s], [], [], 0) + for sock in r: + #incoming message from remote server + if sock == s: + conn, addr = s.accept() #accepts the connection + print("Connected by: ", addr) #prints the connection + return conn + return None + +def get_data(s): + r, w, e = select.select( [s], [], []) + + for sock in r: + #incoming message from remote server + if sock == s: + return sock.recv(1024) + return None + + +def client(): + HOST = 'localhost' + PORT = 9999 + s = socket.socket(AF_INET, SOCK_STREAM) + s.connect((HOST, PORT)) + return s + +if __name__ == '__main__': + + s = server() + c = client() + sc = get_connection(s) + + c.send(str.encode("hello")) + while True: + resp = get_data(sc) + if resp is not None: + print ("response", resp) + break + s.close() + sc.close() + c.close() + -- 2.30.2