def gotoSendFile(sMain, filepaths): global currentPortAddress #filepaths=askopenfilenames() num_files = len(filepaths) send(sMain, "Length" + SEPARATOR + str(num_files) + "\n", 'utf-8') print("sending Request") #msg=receive(sMain,BUFFER_SIZE_VERY_SMALL,'utf-8') req = recvGetline(sMain) ports = [] if (req == RECEIVING_ACK): for filepath in filepaths: filename = os.path.basename(filepath) filesize = int((os.stat(filepath)).st_size) currentPortAddress += 2 fileInfo = filename + SEPARATOR + str(filesize) + SEPARATOR + str( currentPortAddress) + "\n" ports.append(currentPortAddress) send(sMain, fileInfo, 'utf-8') msg = receive(sMain, BUFFER_SIZE_SMALL, 'utf-8') if (msg == RECEIVING_ACK): #file data is sent, now send the files on separate threads return ports return True print("File cant be reached: FILEOPENERROR") return False
def checkConnection(self): if (self.mainSocket != None): su.send(self.mainSocket, CONNECTION_ESTABLISHED_SERVER, 'utf-8') ack = su.receive(self.mainSocket, BUFFER_SIZE_VERY_SMALL, 'utf-8') if (ack == RECEIVING_ACK): #the sent packet is acknowledged print("UPLOADING SUCCESS") self.sending = 1 self.updateUi() else: print("UPLOADING FAILED") self.sending = 0 self.conn = 0 self.mainSocket.close() return msg = su.receive(self.mainSocket, BUFFER_SIZE_VERY_SMALL, 'utf-8') if (msg == CONNECTION_ESTABLISHED_CLIENT): #the message is received su.send(self.mainSocket, RECEIVING_ACK, 'utf-8') print("DOWNLOADING SUCCESS") self.receiving = 1 self.updateUi() else: print("DOWNLOADING FAILED") self.receiving = 0 self.conn = 0 self.updateUi() self.mainSocket.close() return
def on_parent(self, screen, parent): if (len(self.ids) != 0): # print("here",parent) box = self.ids.receive_box_id #sock=self.manager.get_screen('screen_server').mainSocket man = self.manager item_list = man.get_screen('screen_server').down_item_list sock = man.get_screen('screen_server').mainSocket su.send(sock, RECEIVING_ACK, 'utf-8') a = 0 pool = mp.Pool() for i in item_list: j = i.replace("\n", "") j = j.split(SEPARATOR) nm = j[0] size = int(j[1]) port = int(j[2]) newLayout = self.ReceiveTodo(nm, 100) box.add_widget(newLayout) pool.apply_async(su.receiveFile, args=( nm, size, port, a, ), callback=self.onProcessEnd) a += 1 pool.close() pool.join()
def receiveFile(filename, filesize, port, i): try: print(filename, port) import os import socket from constants import BASEPATH, BUFFER_SIZE_MEDIUM, REQUEST_TO_SEND, SEPARATOR, ALLOW_TO_RECV from wifiUtils import getIpAddress from socketUtils import send, receive, recvGetline from kivy.uix.label import Label from kivy.uix.boxlayout import BoxLayout f = open(BASEPATH + filename, "wb") subSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) subSock.bind((getIpAddress(), port)) subSock.listen() print("Socket for ", i, " created") conn, addr = subSock.accept() print("connection created ", addr) #msg="REQUEST_TO_SEND"+SEPARATOR+str(i)+"\n" #send(conn,msg,'utf-8') msg = recvGetline(conn) #print(msg) msg = msg.split(SEPARATOR) msg[0] += "\n" if (msg[0] == REQUEST_TO_SEND and int(msg[1]) == i): print("Request arrived") msg = "ALLOW_TO_RECV" + SEPARATOR + str(i) + "\n" send(conn, msg, 'utf-8') iter = filesize uptonow = 0 per = 0 # perTv=layout.ids.prec_id # perTv.text=str(per)+"%" while True: bytes_read = receive( conn, BUFFER_SIZE_MEDIUM) #conn.recv(BUFFER_SIZE_MEDIUM) iter -= len(bytes_read) f.write(bytes_read) uptonow += len(bytes_read) newper = int((uptonow * 100) / filesize) if (newper != per): per = newper # perTv.text=str(per)+"%" if (iter <= 0): break print("File is saved ", i) f.close() return filename else: return None subSock.close() except Exception as ex: print(ex) return None
def gotoRecvFile(sMain): msg = recvGetline(sMain) msg = msg.split(SEPARATOR) num_files = int(msg[1]) send(sMain, RECEIVING_ACK, 'utf-8') fileinfo = [] for i in range(num_files): fileinfo.append(recvGetline(sMain)) return fileinfo send(sMain, RECEIVING_ACK, 'utf-8') for i in range(num_files): f = fileinfo[i].split(SEPARATOR) filename = f[0] filesize = int(f[1]) port = int(f[2]) #create thread for each file pool.apply_async(receiveFile, args=(filename, filesize, port, i)) pool.close() pool.join() return True
def sendFile(filepath, i, port, filesize): try: import socket import os from constants import BUFFER_SIZE_MEDIUM, SEPARATOR from wifiUtils import getIpAddress from main import send from main import receive print("Port :", port) subsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) subsock.bind((getIpAddress(), port)) subsock.listen() print("Waiting", i) conn, addr = subsock.accept() print("Connection established", i, addr) msg = "REQUEST_TO_SEND" + SEPARATOR + str(i) + "\n" send(conn, msg, 'utf-8') recv = "" while True: ch = receive(conn, 1, 'utf-8') if (ch != '\n'): recv += ch else: recv += ch break exp = "ALLOW_TO_RECV" + SEPARATOR + str(i) + "\n" if (recv == exp): print("request accepted", i) #sending file by bytes f = open(filepath, "rb") byte = f.read(BUFFER_SIZE_MEDIUM) count = 0 a = 0 print("Sending in progress", end=" ") while byte: send(conn, byte) byte = f.read(BUFFER_SIZE_MEDIUM) count += len(byte) #print(".",end="") rem = filesize % BUFFER_SIZE_MEDIUM byte = f.read(rem) send(conn, byte) conn.close() return i else: print("receiving not allowed") conn.close() return -1 except Exception as ex: print(ex) return -1