예제 #1
0
def bye(addr):
    text = 'bye'
    msgT = msg_t()

    msgT.payload = str.encode(text)
    msgT.payload_len = len(text)
    msgT.cur_seq = cur_seq
    msgT.max_seq = max_seq
    msgT.msg_type_t = msg_type_t.MSG_TYPE_FINISH
    #UDP is not very good at signalling things are done, since its unreliable. So send about 5 UDP reqs.
    for i in range(1,5):
        serversocket.sendto(structToBytes(msgT), addr)
    serversocket.close()
예제 #2
0
port = int(sys.argv[1])#8081


# create an INET, STREAMing socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to a public host, and a well-known port
serversocket.bind(("", port))
# become a server socket
serversocket.listen(1)#1 client,

## ACCEPT connections from outside
(clientsocket, addr) = serversocket.accept()

#RECEIVE
data = clientsocket.recv(sizeof(msg_t))
someMsgT = msg_t()
someMsgT = bytesToStruct(data)

print("server: RX ", someMsgT.msg_type_t, someMsgT.cur_seq, someMsgT.max_seq, someMsgT.payload_len)
##Double check that it was a get
if(someMsgT.msg_type_t !=msg_type_t.MSG_TYPE_GET):
    print("The first message was not MSG_TYPE_GET")
    clientsocket.close()
    exit()

filename = someMsgT.payload.decode("UTF-8")
cur_seq = 0
max_seq = 0
try:
    with open(filename,mode = 'r',encoding = 'utf-8') as f:
        #get the filesize so we can have a max_seq
예제 #3
0
    print("not enough arguments. arg1 is server ip, arg2 is port, arg3 is filename")
    exit()
serverIP = sys.argv[1]#localhost
port = int(sys.argv[2])#8081
filename = sys.argv[3]

try:
    # UDP gets INET, DATAGRAM
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # UDP doesn't need connection handshake
    # s.connect((serverIP, port))
except Exception as e:
    print('something\'s wrong with %s:%d. Exception type is %s' % (serverIP, port, e))

#form the first message that asks for the file
firstMsg = msg_t()
firstMsg.msg_type_t = msg_type_t.MSG_TYPE_GET
firstMsg.cur_seq = 0
firstMsg.max_seq = 0
firstMsg.payload_len = len(filename)
firstMsg.payload = str.encode(filename)
#conver the message to bytes to send as a datagram
x = structToBytes(firstMsg)

s.sendto(x,(serverIP, port))
fileText = ''
clientSeq=0

while True:
    d = s.recvfrom(sizeof(msg_t))
    data = d[0]
예제 #4
0
if len(sys.argv)!=4:
    print("not enough arguments. arg1 is server ip, arg2 is port, arg3 is filename")
    exit()
serverIP = sys.argv[1]#localhost
port = int(sys.argv[2])#8081
filename = sys.argv[3]

try:
    # create an INET, STREAMing socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # now connect to the web server on port 8081 - the normal http port
    s.connect((serverIP, port))
except Exception as e:
    print('something\'s wrong with %s:%d. Exception type is %s' % (serverIP, port, e))

firstMsg = msg_t()
firstMsg.msg_type_t = msg_type_t.MSG_TYPE_GET
firstMsg.cur_seq = 0
firstMsg.max_seq = 0
firstMsg.payload_len = len(filename)
firstMsg.payload = str.encode(filename)

x = structToBytes(firstMsg)

s.send(x)
fileText = ''
clientSeq=0

while True:
    data = s.recv(sizeof(msg_t))
    if not data: