Example #1
0
def main():
    # parse all the arguments to the client
    parser = argparse.ArgumentParser(description='CS 352 Socket Server')
    parser.add_argument('-f',
                        '--filename',
                        help='Filename to Receiver',
                        required=False)
    parser.add_argument('-p',
                        '--port',
                        help='CS 352 Socket Port (optional for part 1)',
                        required=False)
    parser.add_argument('-u',
                        '--udpportRx',
                        help='UDP port to use for receiving',
                        required=True)
    parser.add_argument('-v',
                        '--udpportTx',
                        help='UDP port to use for sending',
                        required=False)

    args = vars(parser.parse_args())

    # open the file for writing
    filename = args['filename']
    udpportRx = args['udpportRx']

    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''

    # the port is not used in part 1 assignment, except as a placeholder
    if (args['port']):
        port = args['port']
    else:
        port = 1111

    if (filename):
        try:
            fd = open(filename, "wb")
            usefile = True
        except:
            print("error opening file: %s" % (filename))
            exit(-1)
    else:
        pass

        # This is where we set the transmit and receive
    # ports the server uses for the underlying UDP
    # sockets. If we are running the client and
    # server on different machines, these ports
    # need to be different, otherwise we can
    # use the same ports
    if (udpportTx):
        sock352.init(udpportTx, udpportRx)
    else:
        sock352.init(udpportRx, udpportRx)

    s = sock352.socket()

    # set the fragment size we will read on
    # FRAGMENTSIZE = 4096

    # binding the host to empty allows reception on
    # all network interfaces
    s.bind(('', port))
    s.listen(5)

    # when accept returns, the client is connected
    (s2, address) = s.accept()

    # this receives the size of the file
    # as a 4 byte integer in network byte order (big endian)
    longPacker = struct.Struct("!L")
    long = s2.recv(4)
    fn = longPacker.unpack(long)
    filelen = fn[0]
    #print("Filelen: ", filelen)

    # # the MD5 computes a unique hash for all the data
    # mdhash = md5.new()

    # bytes_to_receive = filelen
    start_stamp = time.clock()

    # main loop to receive the data from the client
    # while (bytes_to_receive > 0):
    #     if (bytes_to_receive >= FRAGMENTSIZE):
    #         fragment = s2.recv(FRAGMENTSIZE)
    #     else:
    #         fragment = s2.recv(bytes_to_receive)

    #     mdhash.update(fragment)
    #     bytes_to_receive = bytes_to_receive - len(fragment)
    #     fd.write(fragment)
    file = s2.recv(filelen)
    # mdhash.update(file)
    fd.write(file)

    end_stamp = time.clock()
    lapsed_seconds = end_stamp - start_stamp

    # finish computing the MD5 hash
    # local_digest = mdhash.digest()

    # receive the size of the remote hash
    # dl = longPacker.unpack(s2.recv(4))
    # digestlen = dl[0]
    # remote_digest = s2.recv(digestlen)

    # # check is the size matches
    # if (len(remote_digest) != digestlen):
    #     raise RuntimeError("socket error")

    # # compare the two digests, byte for byte
    # for i, server_byte in enumerate(local_digest):
    #     client_byte = remote_digest[i]
    #     if (client_byte != server_byte):
    #         print( "digest failed at byte %d %c %c " % (i,client_byte,server_byte))

    if (lapsed_seconds > 0.0):
        print("server1: received %d bytes in %0.6f seconds, %0.6f MB/s " %
              (filelen, lapsed_seconds,
               (filelen / lapsed_seconds) / (1024 * 1024)))
    else:
        print("server1: received %d bytes in %d seconds, inf MB/s " %
              (filelen, lapsed_seconds))
    fd.close()
    s2.close()
Example #2
0
def main():
    # parse all the arguments to the client
    parser = argparse.ArgumentParser(description='CS 352 Socket Client')
    parser.add_argument('-f',
                        '--filename',
                        help='File to Send',
                        required=False)
    parser.add_argument('-d',
                        '--destination',
                        help='Destination IP Host',
                        required=True)
    parser.add_argument('-p',
                        '--port',
                        help='remote sock352 port',
                        required=False)
    parser.add_argument('-u',
                        '--udpportRx',
                        help='UDP port to use for receiving',
                        required=True)
    parser.add_argument('-v',
                        '--udpportTx',
                        help='UDP port to use for sending',
                        required=False)

    # get the arguments into local variables
    args = vars(parser.parse_args())
    filename = args['filename']
    destination = args['destination']
    udpportRx = args['udpportRx']

    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''

    # the port is not used in part 1 assignment, except as a placeholder
    if (args['port']):
        port = args['port']
    else:
        port = 1111

    # open the file for reading
    if (filename):
        try:
            filesize = os.path.getsize(filename)
            fd = open(filename, "rb")
            usefile = True
        except:
            print("error opening file: %s" % (filename))
            exit(-1)
    else:
        pass

    # This is where we set the transmit and receive
    # ports the client uses for the underlying UDP
    # sockets. If we are running the client and
    # server on the same machine, these ports
    # need to be different. If they are running on
    # different machines, we can re-use the same
    # ports.
    if (udpportTx):
        sock352.init(udpportTx, udpportRx)
    else:
        sock352.init(udpportRx, udpportRx)

    s = sock352.socket()
    s.connect((destination, port))

    longPacker = struct.Struct("!L")
    fileLenPacked = longPacker.pack(filesize)
    s.send(fileLenPacked)

    bytes_to_send = filesize

    start_stamp = time.clock()

    file_contents = fd.read()

    totalsent = 0
    # make sure we sent the whole fragment

    totalsent = s.send(file_contents)
    if (totalsent == 0):
        raise RuntimeError("socket broken")
    end_stamp = time.clock()
    lapsed_seconds = end_stamp - start_stamp

    if (lapsed_seconds > 0.0):
        print("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " %
              (filesize, lapsed_seconds,
               (filesize / lapsed_seconds) / (1024 * 1024)))
    else:
        print("client1: sent %d bytes in %d seconds, inf MB/s " %
              (filesize, lapsed_seconds))

    fd.close()
    s.close()
Example #3
0
def main():

    # parse all the arguments to the client
    parser = argparse.ArgumentParser(description='CS 352 Socket Server')
    parser.add_argument('-f',
                        '--filename',
                        help='Filename to Receiver',
                        required=False)
    parser.add_argument('-p',
                        '--port',
                        help='CS 352 Socket Port (optional for part 1)',
                        required=False)
    parser.add_argument('-u',
                        '--udpportRx',
                        help='UDP port to use for receiving',
                        required=True)
    parser.add_argument('-v',
                        '--udpportTx',
                        help='UDP port to use for sending',
                        required=False)

    args = vars(parser.parse_args())

    # open the file for writing
    filename = args['filename']
    udpportRx = args['udpportRx']

    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''

    # the port is not used in part 1 assignment, except as a placeholder
    if (args['port']):
        port = args['port']
    else:
        port = 1111

    if (filename):
        try:
            fd = open(filename, "wb")
            usefile = True
        except:
            print("error opening file: %s" % (filename))
            exit(-1)
    else:
        pass

    # This is where we set the transmit and receive
    # ports the server uses for the underlying UDP
    # sockets. If we are running the client and
    # server on different machines, these ports
    # need to be different, otherwise we can
    # use the same ports
    if (udpportTx):
        sock352.init(udpportTx, udpportRx)
    else:
        sock352.init(udpportRx, udpportRx)

    s = sock352.socket()

    # binding the host to empty allows reception on
    # all network interfaces
    s.bind(('', port))
    s.listen(5)

    # when accept returns, the client is connected
    (s2, address) = s.accept()

    # this receives the size of the file
    # as a 4 byte integer in network byte order (big endian)
    longPacker = struct.Struct("!L")
    long = s2.recv(4)
    fn = longPacker.unpack(long)
    filelen = fn[0]

    start_stamp = time.clock()

    file = s2.recv(filelen)

    fd.write(file)

    end_stamp = time.clock()
    lapsed_seconds = end_stamp - start_stamp

    if (lapsed_seconds > 0.0):
        print("server1: received %d bytes in %0.6f seconds, %0.6f MB/s " %
              (filelen, lapsed_seconds,
               (filelen / lapsed_seconds) / (1024 * 1024)))
    else:
        print("server1: received %d bytes in %d seconds, inf MB/s " %
              (filelen, lapsed_seconds))
    fd.close()
    s2.close()
import sock352
import time

sock352.init(0, 0)

s = sock352.socket()

s.connect(('127.0.0.1', 12467))

print(time.time())
Example #5
0
def main():
    # parse all the arguments to the client
    global fd
    parser = argparse.ArgumentParser(description='CS 352 Socket Server')
    parser.add_argument('-f',
                        '--filename',
                        help='Filename to Receiver',
                        required=False)
    parser.add_argument('-p',
                        '--port',
                        help='CS 352 Socket Port (optional for part 1)',
                        required=False)
    parser.add_argument('-u',
                        '--udpportRx',
                        help='UDP port to use for receiving',
                        required=True)
    parser.add_argument('-v',
                        '--udpportTx',
                        help='UDP port to use for sending',
                        required=False)

    args = vars(parser.parse_args())

    # open the file for writing
    filename = args['filename']
    udpportRx = args['udpportRx']

    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''

    # the port is not used in part 1 assignment, except as a placeholder
    if (args['port']):
        port = args['port']
    else:
        port = 1111

    if (filename):
        try:
            fd = open(filename, "wb")
            usefile = True
        except:
            print("error opening file: %s" % (filename))
            exit(-1)
    else:
        pass

        # This is where we set the transmit and receive
    # ports the server uses for the underlying UDP
    # sockets. If we are running the client and
    # server on different machines, these ports
    # need to be different, otherwise we can
    # use the same ports
    if (udpportTx):
        sock352.init(udpportTx, udpportRx)
    else:
        sock352.init(udpportRx, udpportRx)

    s = sock352.socket()

    # set the maximum fragment size we will read on
    MAXFRAGMENTSIZE = 16384

    # binding the host to empty allows reception on
    # all network interfaces
    s.bind(('', port))
    s.listen(5)

    # when accept returns, the client is connected
    (s2, address) = s.accept()

    # this receives the size of the file
    # as a 4 byte integer in network byte order (big endian)
    longPacker = struct.Struct("!L")
    long = s2.recv(4)
    fn = longPacker.unpack(long)
    filelen = fn[0]

    # the MD5 computes a unique hash for all the data

    bytes_to_receive = filelen
    start_stamp = time.perf_counter()

    random.seed(a=352)
    # main loop to receive the data from the client
    while bytes_to_receive > 0:
        size = random.randrange(1, MAXFRAGMENTSIZE)
        if bytes_to_receive >= size:
            # pick a random size to receive
            fragment = s2.recv(size)
        else:
            fragment = s2.recv(bytes_to_receive)

        bytes_to_receive = bytes_to_receive - len(fragment)
        fd.write(fragment)

    end_stamp = time.perf_counter()
    lapsed_seconds = end_stamp - start_stamp

    if lapsed_seconds > 0.0:
        print("server1: received %d bytes in %0.6f seconds, %0.6f MB/s " %
              (filelen, lapsed_seconds,
               (filelen / lapsed_seconds) / (1024 * 1024)))
    else:
        print("server1: received %d bytes in %d seconds, inf MB/s " %
              (filelen, lapsed_seconds))
    fd.close()
    s2.close()
Example #6
0
def main():
    # parse all the arguments to the client
    parser = argparse.ArgumentParser(description='CS 352 Socket Client')
    parser.add_argument('-f',
                        '--filename',
                        help='File to Send',
                        required=False)
    parser.add_argument('-d',
                        '--destination',
                        help='Destination IP Host',
                        required=True)
    parser.add_argument('-p',
                        '--port',
                        help='remote sock352 port',
                        required=False)
    parser.add_argument('-u',
                        '--udpportRx',
                        help='UDP port to use for receiving',
                        required=True)
    parser.add_argument('-v',
                        '--udpportTx',
                        help='UDP port to use for sending',
                        required=False)

    # get the arguments into local variables
    args = vars(parser.parse_args())
    filename = args['filename']
    destination = args['destination']
    udpportRx = args['udpportRx']

    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''

    # the port is not used in part 1 assignment, except as a placeholder
    if (args['port']):
        port = args['port']
    else:
        port = 1300

    # open the file for reading
    if (filename):
        try:
            filesize = os.path.getsize(filename)
            fd = open(filename, "rb")
            usefile = True
        except:
            print("error opening file: %s" % (filename))
            exit(-1)
    else:
        pass

    # This is where we set the transmit and receive
    # ports the client uses for the underlying UDP
    # sockets. If we are running the client and
    # server on the same machine, these ports
    # need to be different. If they are running on
    # different machines, we can re-use the same
    # ports.
    if (udpportTx):
        sock352.init(udpportTx, udpportRx)
    else:
        sock352.init(udpportRx, udpportRx)

    # create a socket and connect to the remote server
    s = sock352.socket()
    s.connect((destination, port))

    # send the size of the file as a 4 byte integer
    # to the server, so it knows how much to read
    FRAGMENTSIZE = 8192
    longPacker = struct.Struct("!L")
    fileLenPacked = longPacker.pack(filesize)
    s.send(fileLenPacked)

    # use the MD5 hash algorithm to validate all the data is correct
    mdhash = md5.new()

    # loop for the size of the file, sending the fragments
    bytes_to_send = filesize

    start_stamp = time.clock()
    while (bytes_to_send > 0):
        fragment = fd.read(FRAGMENTSIZE)
        mdhash.update(fragment)
        totalsent = 0
        # make sure we sent the whole fragment
        while (totalsent < len(fragment)):
            sent = s.send(fragment[totalsent:])
            if (sent == 0):
                raise RuntimeError("socket broken")
            totalsent = totalsent + sent
        bytes_to_send = bytes_to_send - len(fragment)

    end_stamp = time.clock()
    lapsed_seconds = end_stamp - start_stamp

    # this part send the lenght of the digest, then the
    # digest. It will be check on the server

    digest = mdhash.digest()
    # send the length of the digest
    long = len(digest)
    digestLenPacked = longPacker.pack(long)
    sent = s.send(digestLenPacked)
    if (sent != 4):
        raise RuntimeError("socket broken")

    # send the digest
    sent = s.send(digest)
    if (sent != len(digest)):
        raise RuntimeError("socket broken")

    if (lapsed_seconds > 0.0):
        print("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " %
              (filesize, lapsed_seconds,
               (filesize / lapsed_seconds) / (1024 * 1024)))
    else:
        print("client1: sent %d bytes in %d seconds, inf MB/s " %
              (filesize, lapsed_seconds))

    fd.close()
    s.close()
Example #7
0
import sock352

sock352.init(38912, 38911)

socket = sock352.socket()

# Server will drop 20% of the packets it sends.
socket.dropPercentage = 0

print "Binding..."
socket.bind(('', 1010))
print "Listening..."
socket.listen(5)
print "Accepting..."
socket.accept()

print "Receiving..."
data = socket.recv(488890)

print "Sending: " + data[:20] + "..." + data[-20:]
ret = socket.send(data)

print "Sent."
print "Closing socket..."

socket.close()

print "Closed socket."
#fuser 8888/udp     <-- finds udp port 8888 being used
#fuser -k 8888/udp  <-- kills process running on port 8888

import sock352
import struct

sock352.init(9993, 8883)

(publicKeys, privateKeys) = sock352.readKeyChain('keychain.txt')

sock = sock352.socket()

sock.bind(('', 1111))
sock.listen(1)

(s, address) = sock.accept(sock352.ENCRYPT)

longPacker = struct.Struct("!L")
long = s.recv(4)

fn = longPacker.unpack(long)
size = fn[0]

message = s.recv(size)

print("Received: " + message)

s.close()
import sock352

sock352.init(8888, 9999)

s = sock352.socket()

s.connect(('127.0.0.1', 1111))

b = s.send(
    "Style never met and those among great. At no or september sportsmen he perfectly happiness attending. Depending listening delivered off new she procuring satisfied sex existence. Person plenty answer to exeter it if. Law use assistance especially resolution cultivated did out sentiments unsatiable. Way necessary had intention happiness but september delighted his curiosity. Furniture furnished or on strangers neglected remainder engrossed."
)

print("Sent " + str(b) + " bytes")

s.close()
Example #10
0
#fuser 8888/udp     <-- finds udp port 8888 being used
#fuser -k 8888/udp  <-- kills process running on port 8888

import sock352

sock352.init(9999, 8888)

sock = sock352.socket()

sock.bind(('', 1111))
sock.listen(1)

(s, address) = sock.accept()
message = s.recv(443)

print("Received: " + message)

s.close()
Example #11
0
def main():
    # parse all the arguments to the client 
    parser = argparse.ArgumentParser(description='CS 352 Socket Client')
    parser.add_argument('-f','--filename', help='File to Send', required=False)
    parser.add_argument('-d','--destination', help='Destination IP Host', required=True)
    parser.add_argument('-p','--port', help='remote sock352 port', required=False)
    parser.add_argument('-u','--udpportRx', help='UDP port to use for receiving', required=True)
    parser.add_argument('-v','--udpportTx', help='UDP port to use for sending', required=False)
    parser.add_argument('-k','--keyfile', help='keyfile', required=True)

    # get the arguments into local variables 
    args = vars(parser.parse_args())
    filename = args['filename']
    destination = args['destination']
    udpportRx = args['udpportRx']
    keyfilename = args['keyfile']
    
    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''
        
    # the port is not used in part 2 assignment, except as a placeholder
    if (args['port']): 
        port = args['port']
    else:
        port = 5555 

    # open the file to send to the server for reading
    if (filename):
        try: 
            filesize = os.path.getsize(filename)
            fd = open(filename, "rb")
            usefile = True
        except:
            print ( "error opening file: %s" % (filename))
            exit(-1)
    else:
        pass 

    # This is where we set the transmit and receive
    # ports the client uses for the underlying UDP
    # sockets. If we are running the client and
    # server on the same machine, these ports
    # need to be different. If they are running on
    # different machines, we can re-use the same
    # ports. 
    if (udpportTx):
        sock352.init(udpportTx,udpportRx)
    else:
        sock352.init(udpportRx,udpportRx)

    # load a keychain from a file
    keysInHex = sock352.readKeyChain(keyfilename)
    
    # create a socket and connect to the remote server
    s = sock352.socket()
    s.connect((destination,port),sock352.ENCRYPT)
    # Comment above and uncomment below to toggle encryption
    #s.connect((destination,port))
    #mesure the start stamp
    start_stamp = time.clock() 
	#load the whole file into memory
    whole_file = fd.read()
    #mesure its length
    filesize = len(whole_file)
    longPacker = struct.Struct("!L")
    fileLenPacked = longPacker.pack(filesize);
    s.send(fileLenPacked)
	
    sent = s.send(whole_file)
    if (sent != filesize):
        raise RuntimeError("socket broken")

    end_stamp = time.clock() 
    lapsed_seconds = end_stamp - start_stamp
    
    if (lapsed_seconds > 0.0):
        print ("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " % (filesize, lapsed_seconds, (filesize/lapsed_seconds)/(1024*1024)))
    else:
        print ("client1: sent %d bytes in %d seconds, inf MB/s " % (filesize, lapsed_seconds))        

    fd.close()
    s.close()
Example #12
0
def main():
    # parse all the arguments to the client
    parser = argparse.ArgumentParser(description='CS 352 Socket Client')
    parser.add_argument('-f',
                        '--filename',
                        help='File to Send',
                        required=False)
    parser.add_argument('-d',
                        '--destination',
                        help='Destination IP Host',
                        required=True)
    parser.add_argument('-p',
                        '--port',
                        help='remote sock352 port',
                        required=False)
    parser.add_argument('-u',
                        '--udpportRx',
                        help='UDP port to use for receiving',
                        required=True)
    parser.add_argument('-v',
                        '--udpportTx',
                        help='UDP port to use for sending',
                        required=False)

    # get the arguments into local variables
    args = vars(parser.parse_args())
    filename = args['filename']
    destination = args['destination']
    udpportRx = args['udpportRx']

    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''

    # the port is not used in part 1 assignment, except as a placeholder
    if (args['port']):
        port = args['port']
    else:
        port = 5555

    # open the file to send to the server for reading
    if (filename):
        try:
            filesize = os.path.getsize(filename)
            fd = open(filename, "rb")
            usefile = True
        except:
            print("error opening file: %s" % (filename))
            exit(-1)
    else:
        pass

    # This is where we set the transmit and receive
    # ports the client uses for the underlying UDP
    # sockets. If we are running the client and
    # server on the same machine, these ports
    # need to be different. If they are running on
    # different machines, we can re-use the same
    # ports.
    if (udpportTx):
        sock352.init(udpportTx, udpportRx)
    else:
        sock352.init(udpportRx, udpportRx)

    # create a socket and connect to the remote server
    s = sock352.socket()
    s.connect((destination, port))

    BUFFERSIZE = 268288
    longPacker = struct.Struct("!L")
    fileLenPacked = longPacker.pack(filesize)
    s.send(fileLenPacked)

    mdhash = md5()

    bytes_to_send = filesize

    start_stamp = time.process_time()
    while (bytes_to_send > 0):
        buff = fd.read(BUFFERSIZE)
        mdhash.update(buff)
        totalsent = 0
        while (totalsent < len(buff)):
            sent = s.send(buff[totalsent:])
            if (sent == 0):
                raise RuntimeError("broken socket")
            totalsent = totalsent + sent
        bytes_to_send = bytes_to_send - len(buff)

    end_stamp = time.process_time()
    lapsed_seconds = end_stamp - start_stamp

    digest = mdhash.digest()
    long = len(digest)
    digestLenPacked = longPacker.pack(long)
    sent = s.send(digestLenPacked)
    if (sent != 4):
        raise RuntimeError("socket broken")
    sent = s.send(digest)
    if (sent != len(digest)):
        raise RuntimeError("socket broken")
    if (lapsed_seconds > 0.0):
        print("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " %
              (filesize, lapsed_seconds,
               (filesize / lapsed_seconds) / (1024 * 1024)))
    else:
        print("client1: sent %d bytes in %d seconds, inf MB/s " %
              (filesize, lapsed_seconds))
    fd.close()
    s.close()
Example #13
0
import sock352
import struct

sock352.init('8000', '3221')
mysocket = sock352.socket()
givenHeader = mysocket.connect(("localhost", 1111))
x = struct.unpack("!12B", givenHeader)
print(x)
import sock352
import struct
import socket as syssock

sock352.init(8883, 9993)

keysInHex = sock352.readKeyChain('keychain.txt')

s = sock352.socket()

s.connect(('127.0.0.1', 1111), sock352.ENCRYPT)

message = b"I never offered Pardons to Homeland Security Officials, never ordered anyone to close our Southern Border (although I have the absolute right to do so, and may if Mexico does not apprehend the illegals coming to our Border), and am not \"frustrated.\" It is all Fake & Corrupt News!"

size = len(message)
longPacker = struct.Struct("!L")
package = longPacker.pack(size)
s.send(package)

b = s.send(message)

print("Sent " + str(b) + " bytes")

s.close()
Example #15
0
import sock352

sock352.init(38911, 38912)

socket = sock352.socket()

# Client will drop 20% of the packets it sends.
socket.dropPercentage = 0

# Create really long string.
s = "".join([str(i) for i in range(100000)])
# for i in range(100000):
# 	s += str(i)

print len(s)

print "Connecting..."
socket.connect(('ilab.cs.rutgers.edu', 1010))
print "Connected"

print "Sending TEST"
socket.send(s)

ret = socket.recv(488890)

print "DATA RECEIVED BACK"
print "VALID: " + str(ret == s)

socket.close()

print "Closed socket."