Bläddra i källkod

works once! need ot make it work with serial clients though

tarfeef101 6 år sedan
förälder
incheckning
cfbde0c7c1
2 ändrade filer med 11 tillägg och 2 borttagningar
  1. 2 2
      a1/client.py
  2. 9 0
      a1/server.py

+ 2 - 2
a1/client.py

@@ -24,8 +24,8 @@ if (confirm.decode() == "correct"):
     sock.close
     
     # make TCP connection, send msg
-    tsock = socket(socket.AF_INET, socket.SOCK_STREAM)
-    tsock.connect(saddr, rport)
+    tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    tsock.connect((saddr, int(rport)))
     tsock.send(msg.encode())
     
     # get reply, print it, exit

+ 9 - 0
a1/server.py

@@ -30,6 +30,15 @@ while True:
         print("SERVER_TCP_PORT=", rport, sep="")
         sock.sendto(str(rport).encode(), clientAddress) # send new socket port to client
         newConn = False
+
+        # get confirmation of port, send OK
+        confirm, newaddr = sock.recvfrom(1024)
+        if (confirm.decode() == str(rport)):
+          sock.sendto("correct".encode(), newaddr)
+        else:
+          print("Received wrong port number, fail")
+          exit(1)
+
         tsock.listen(1)
         
         # now put logic for TCP socket behaviour