import socket 
import time
import threading 
import os
import sys
from random import randint

host = raw_input("enter IP address: ")
port = 5000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))

def StopAndWait(file_name, addr):
     with open(file_name, 'rb') as f:
        # bytesToSend = f.read(512)
        i=0
        # flag = 1
        bytesToSend=None
        while bytesToSend != "":
            # if flag == 0:
            bytesToSend = f.read(512)
            i+=1
            randpacket = randint(0, 9)
            sock.settimeout(10)
            ack = False
            if randpacket == 4:
                print "dropped!"
                while not socket.timeout:
                    continue
            else:
                sock.sendto(str(i), addr)
                sock.sendto(bytesToSend, addr)
            while not ack:
                try:
                    print "TRY"
                    data, addr = sock.recvfrom(512)
                    ack = True
                except socket.timeout :
                    print "timeout, resend packet..."
                    sock.sendto(str(i), addr)
                    sock.sendto(bytesToSend, addr)
            print data
            # flag = 0



def Main():
    data, addr = sock.recvfrom(512)
    print data + "Server Started.\n Client connected <" + str(addr) + ">"
    quitting = False
    while not quitting:
        filename, addr = sock.recvfrom(512)
        if str(filename) == "quit":
            quitting = True
            continue
        print filename +" "+ str(addr)
        if(os.path.isfile(filename)):
            print "Exists "
            sock.sendto(("EXISTS" + str(os.path.getsize(filename))), addr)
            userResponse, addr = sock.recvfrom(512)
            if(userResponse == 'OK'):
                print (userResponse + " received")
                StopAndWait(filename,addr)


               



    sock.close()

Main()
6948f72dae3dc1e5e25a9d69d06529cfa709818a
import socket 
import time
import threading 
import os
import sys
from random import randint

host = raw_input("enter IP address: ")
port = 5000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))

def StopAndWait(file_name, addr):
     with open(file_name, 'rb') as f:
        # bytesToSend = f.read(512)
        i=0
        # flag = 1
        bytesToSend=None
        while bytesToSend != "":
            # if flag == 0:
            bytesToSend = f.read(512)
            i+=1
            randpacket = randint(0, 9)
            sock.settimeout(10)
            ack = False
            if randpacket == 4:
                print "dropped!"
                while not socket.timeout:
                    continue
            else:
                sock.sendto(str(i), addr)
                sock.sendto(bytesToSend, addr)
            while not ack:
                try:
                    print "TRY"
                    data, addr = sock.recvfrom(512)
                    ack = True
                except socket.timeout :
                    print "timeout, resend packet..."
                    sock.sendto(str(i), addr)
                    sock.sendto(bytesToSend, addr)
            print data
            # flag = 0



def Main():
    data, addr = sock.recvfrom(512)
    print data + "Server Started.\n Client connected <" + str(addr) + ">"
    quitting = False
    while not quitting:
        filename, addr = sock.recvfrom(512)
        if str(filename) == "quit":
            quitting = True
            continue
        print filename +" "+ str(addr)
        if(os.path.isfile(filename)):
            print "Exists "
            sock.sendto(("EXISTS" + str(os.path.getsize(filename))), addr)
            userResponse, addr = sock.recvfrom(512)
            if(userResponse == 'OK'):
                print (userResponse + " received")
                StopAndWait(filename,addr)


               



    sock.close()

Main()