From c5d92fccf4e4e0a6580e6bc825d5d192a614bc19 Mon Sep 17 00:00:00 2001 From: RhysMJ Date: Thu, 28 Jun 2018 13:37:21 +0100 Subject: [PATCH 1/3] Example Piece This is only an example program for the moment which must be used in order for the TCP module to be able to connect to the other computer. This will be removed/replaced once I've created a better program/system for the server/hosting computer. --- server.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/server.py b/server.py index 94406c1..026b0ff 100644 --- a/server.py +++ b/server.py @@ -1,17 +1,24 @@ import socket -data =0 -TCP_IP = '192.168.1.8' -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) +import tcp -conn, addr = s.accept() -print ('Connection address:', addr) -while data != "stop": - data = conn.recv(BUFFER_SIZE).decode() - if not data: break - print ("received data:", data) - conn.send(data.encode()) # echo -conn.close() +def server(): + data =0 + TCP_IP = tcp.getmyip() + TCP_PORT = int(input("Enter port: ")) + BUFFER_SIZE = 20 # Normally 1024, but we want fast response + + conn = tcp.bind(TCP_IP, TCP_PORT) + + while data != "stop": + data = tcp.receive(conn, BUFFER_SIZE) + if not data: + conn = tcp.bind(TCP_IP, TCP_PORT) + else: + print ("received data:", data) + + tcp.send(data, conn) + #conn.send(data.encode()) # enumerate + + conn.close() + +server() From 18d09249bc953ca5014087390a54c3a42aef6e6e Mon Sep 17 00:00:00 2001 From: RhysMJ Date: Sun, 1 Jul 2018 17:13:38 +0100 Subject: [PATCH 2/3] Update to v0.91 Small changes for easier use when using this program with another. --- server.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/server.py b/server.py index 026b0ff..3337311 100644 --- a/server.py +++ b/server.py @@ -1,24 +1,30 @@ -import socket -import tcp +## Example use of tcp v0.91 ## +## R. M. Jones ## +## 01/07/2017 ## + +import tcp #imoprts the module that is included with this file def server(): + data =0 - TCP_IP = tcp.getmyip() - TCP_PORT = int(input("Enter port: ")) + TCP_IP = tcp.getmyip() #Gets your private ip address + TCP_PORT = int(input("Enter port: ")) #The port that will be open BUFFER_SIZE = 20 # Normally 1024, but we want fast response conn = tcp.bind(TCP_IP, TCP_PORT) - while data != "stop": - data = tcp.receive(conn, BUFFER_SIZE) - if not data: - conn = tcp.bind(TCP_IP, TCP_PORT) - else: - print ("received data:", data) - - tcp.send(data, conn) - #conn.send(data.encode()) # enumerate - - conn.close() + + data = tcp.receive(conn, BUFFER_SIZE) + if not data: + conn = tcp.bind(TCP_IP, TCP_PORT) + else: + return "received data: " + data, conn + -server() +if __name__ == "__main__": + info = 0 + while info != "stop": + info, s = server() + print(info) + tcp.send(data, s) + s.close() From 61815832de5e80ca10193f961d6bafc830d1a72b Mon Sep 17 00:00:00 2001 From: RhysMJ Date: Sun, 1 Jul 2018 17:24:30 +0100 Subject: [PATCH 3/3] Update to v0.92 Fixed major bugs and improved the efficiency of the code --- server.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server.py b/server.py index 3337311..ef229f6 100644 --- a/server.py +++ b/server.py @@ -1,14 +1,14 @@ -## Example use of tcp v0.91 ## +## Example use of tcp v0.92 ## ## R. M. Jones ## ## 01/07/2017 ## import tcp #imoprts the module that is included with this file -def server(): +def server(BUFFER_SIZE, TCP_IP=tcp.getmyip(), TCP_PORT=5050): data =0 - TCP_IP = tcp.getmyip() #Gets your private ip address - TCP_PORT = int(input("Enter port: ")) #The port that will be open + #TCP_IP = tcp.getmyip() #Gets your private ip address + #TCP_PORT = int(input("Enter port: ")) #The port that will be open BUFFER_SIZE = 20 # Normally 1024, but we want fast response conn = tcp.bind(TCP_IP, TCP_PORT) @@ -24,7 +24,7 @@ def server(): if __name__ == "__main__": info = 0 while info != "stop": - info, s = server() + info, s = server(20) print(info) - tcp.send(data, s) + tcp.send(info, s) s.close()