Browse Source

coded on jupyterhub so most things coming in at once. need a readme, and also to fix the billion bugs i'm sure i have

Tareef 6 years ago
commit
6406dd8818
4 changed files with 141 additions and 0 deletions
  1. 39 0
      a1/client.py
  2. 25 0
      a1/client.sh
  3. 53 0
      a1/server.py
  4. 24 0
      a1/server.sh

+ 39 - 0
a1/client.py

@@ -0,0 +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)

+ 25 - 0
a1/client.sh

@@ -0,0 +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
+#ruby client.rb $1 $2 $3 "$4"

+ 53 - 0
a1/server.py

@@ -0,0 +1,53 @@
+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()

+ 24 - 0
a1/server.sh

@@ -0,0 +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
+#ruby server.rb $1