Browse Source

seems to hang after creating a tsock

tarfeef101 6 years ago
parent
commit
a4d6edf669
4 changed files with 137 additions and 139 deletions
  1. 39 39
      a1/client.py
  2. 24 24
      a1/client.sh
  3. 51 53
      a1/server.py
  4. 23 23
      a1/server.sh

+ 39 - 39
a1/client.py

@@ -1,39 +1,39 @@
-import socket
-import sys
-
-# save values needed to talk to server
-saddr = sys.argv[1]
-uport = sys.argv[2]
-rcode = sys.argv[3]
-msg = sys.argv[4]
-
-# start udp socket
-sock = socket(AF_INET, SOCK_DGRAM)
-
-# send msg to server to check rcode
-sock.sendto(rcode.encode(),(saddr, uport))
-
-# get reply from server, save the port, then check it
-confirmation, newaddr = sock.recvfrom(1024)
-rport = confirmation.decode()
-sock.sendto(rport.encode(),(newaddr, uport))
-confirm, neweraddr = sock.recvfrom(1024)
-
-if (confirm.decode() == "correct"):
-    # got right port, close UDP connection
-    sock.close
-    
-    # make TCP connection, send msg
-    tsock = socket(AF_INET, SOCK_STREAM)
-    tsock.connect(saddr, rport)
-    tsock.send(msg.encode())
-    
-    # get reply, print it, exit
-    reply = tsock.recv(1024).decode()
-    print("CLIENT_RCV_MSG='", reply, "'", sep="")
-    tsock.close()
-    sys.exit(0)
-
-else:
-    print("Did not confirm the port, giving up.")
-    sys.exit(1)
+import socket
+import sys
+
+# save values needed to talk to server
+saddr = sys.argv[1]
+uport = int(sys.argv[2])
+rcode = sys.argv[3]
+msg = sys.argv[4]
+
+# start udp socket
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+
+# send msg to server to check rcode
+sock.sendto(rcode.encode(),(saddr, uport))
+
+# get reply from server, save the port, then check it
+confirmation, newaddr = sock.recvfrom(1024)
+rport = confirmation.decode()
+sock.sendto(rport.encode(), newaddr)
+confirm, neweraddr = sock.recvfrom(1024)
+
+if (confirm.decode() == "correct"):
+    # got right port, close UDP connection
+    sock.close
+    
+    # make TCP connection, send msg
+    tsock = socket(socket.AF_INET, socket.SOCK_STREAM)
+    tsock.connect(saddr, rport)
+    tsock.send(msg.encode())
+    
+    # get reply, print it, exit
+    reply = tsock.recv(1024).decode()
+    print("CLIENT_RCV_MSG='", reply, "'", sep="")
+    tsock.close()
+    sys.exit(0)
+
+else:
+    print("Did not confirm the port, giving up.")
+    sys.exit(1)

+ 24 - 24
a1/client.sh

@@ -1,25 +1,25 @@
-#!/bin/bash
-
-#Run script for client distributed as part of
-#Assignment 1
-#Computer Networks (CS 456)
-#Number of parameters: 4
-#Parameter:
-#    $1: <server_address>
-#    $2: <n_port>
-#    $3: <req_code>
-#    $4: message
-
-#Uncomment exactly one of the following commands depending on your implementation
-
-#For C/C++ implementation
-#./client $1 $2 $3 "$4"
-
-#For Java implementation
-#java client $1 $2 $3 "$4"
-
-#For Python implementation
-python3 client.py $1 $2 $3 "$4"
-
-#For Ruby implementation
+#!/bin/bash
+
+#Run script for client distributed as part of
+#Assignment 1
+#Computer Networks (CS 456)
+#Number of parameters: 4
+#Parameter:
+#    $1: <server_address>
+#    $2: <n_port>
+#    $3: <req_code>
+#    $4: message
+
+#Uncomment exactly one of the following commands depending on your implementation
+
+#For C/C++ implementation
+#./client $1 $2 $3 "$4"
+
+#For Java implementation
+#java client $1 $2 $3 "$4"
+
+#For Python implementation
+python3 client.py $1 $2 $3 "$4"
+
+#For Ruby implementation
 #ruby client.rb $1 $2 $3 "$4"

+ 51 - 53
a1/server.py

@@ -1,53 +1,51 @@
-import socket
-import sys
-
-# save request code, make var for tcp socket port
-requestcode = sys.argv[1]
-rport = 0
-
-# start udp socket, declare tcp socket var
-sock = socket(AF_INET, SOCK_DGRAM)
-tsock = None
-
-# get udp socket port from system, save in a var
-sock.bind('', 0)
-nport = sock.getsockname()[1]
-
-# make flag to see if we are waiting for a confirmation or a new connection
-newConn = true
-
-# listen for msgs
-while True:
-    message, clientAddress = serverSocket.recvfrom(1024)
-    message = message.decode()
-    
-    # match, create tcp socket
-    if (message == requestcode && newConn):
-        tsock = (socket.AF_INET, socket.SOCK_STREAM)
-        tsock.bind('', 0)
-        rport = tsock.getsockname()[1]
-        print("SERVER_TCP_PORT=", rport, sep="")
-        sock.sendto(rport.encode(), clientAddress) # send new socket port to client
-        newConn = false
-        
-        # now put logic for TCP socket behaviour
-        while True:
-            tempsock, addr = serverSocket.accept()
-            msg = tempsock.recv(1024).decode()
-            print("SERVER_RCV_MSG='", msg, "'", sep="")
-            reply = msg[::-1]
-            tempsock.send(reply.encode())
-            tempsock.close()
-    
-    # match, send a confirmation msg
-    else if (newConn == false  && message == rport):
-        sock.sendto("correct".encode(), clientAddress)
-    
-    # client is trying to verify the code, but sucks at life
-    else if (newConn == false):
-        # do nothing
-    
-    # client f*ed up, reset all the things
-    else:
-        newConn = true
-        tsock.close()
+import socket
+import sys
+
+# save request code, make var for tcp socket port
+requestcode = sys.argv[1]
+rport = 0
+
+# start udp socket, declare tcp socket var
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+tsock = None
+
+# get udp socket port from system, save in a var
+sock.bind(('', 0))
+nport = sock.getsockname()[1]
+print("SERVER_PORT=", nport, sep="")
+
+# make flag to see if we are waiting for a confirmation or a new connection
+newConn = True
+
+# listen for msgs
+while True:
+    message, clientAddress = sock.recvfrom(1024)
+    message = message.decode()
+    
+    # match, create tcp socket
+    if (message == requestcode and newConn):
+        tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        tsock.bind(('', 0))
+        rport = tsock.getsockname()[1]
+        print("SERVER_TCP_PORT=", rport, sep="")
+        sock.sendto(str(rport).encode(), clientAddress) # send new socket port to client
+        newConn = False
+        tsock.listen(1)
+        
+        # now put logic for TCP socket behaviour
+        while True:
+            tempsock, addr = tsock.accept()
+            msg = tempsock.recv(1024).decode()
+            print("SERVER_RCV_MSG='", msg, "'", sep="")
+            reply = msg[::-1]
+            tempsock.send(reply.encode())
+            tempsock.close()
+    
+    # match, send a confirmation msg
+    elif (newConn == False  and message == rport):
+        sock.sendto("correct".encode(), clientAddress)
+    
+    # client screwed up something other than sending the wrong requestcode
+    elif (newConn != False):
+        newConn = True
+        tsock.close()

+ 23 - 23
a1/server.sh

@@ -1,24 +1,24 @@
-#!/bin/bash
-
-#Run script for the server distributed as a part of
-#Assignment 1
-#Computer Networks (CS 456)
-#
-#Number of parameters: 1
-#Parameter:
-#    $1: <req_code>
-#
-
-#Uncomment exactly one of the following commands depending on implementation
-
-#For C/C++ implementation
-#./server $1
-
-#For Java implementation
-#java server $1
-
-#For Python implementation
-python3 server.py $1
-
-#For Ruby implementation
+#!/bin/bash
+
+#Run script for the server distributed as a part of
+#Assignment 1
+#Computer Networks (CS 456)
+#
+#Number of parameters: 1
+#Parameter:
+#    $1: <req_code>
+#
+
+#Uncomment exactly one of the following commands depending on implementation
+
+#For C/C++ implementation
+#./server $1
+
+#For Java implementation
+#java server $1
+
+#For Python implementation
+python3 server.py $1
+
+#For Ruby implementation
 #ruby server.rb $1