Exemplo n.º 1
0
    def clientStart(self, bindIP, bindPort, bindpath):
        global CHUNK_SIZE
        startIP = bindIP
        startPort = bindPort
        startPath = bindpath

        sock = socket.socket(socket.AF_INET,
                             socket.SOCK_STREAM)  # Create a TCP socket

        try:
            self.textLog.append('[-] Server: {0}:{1}\n'.format(
                startIP, startPort))
            self.textLog.moveCursor(QTextCursor.End)

            sock.connect((startIP, startPort))  # Accept Connection Request

            msgId = 0

            reqMsg = Message()
            filesize = os.path.getsize(startPath)
            reqMsg.Body = BodyRequest(None)
            reqMsg.Body.FILESIZE = filesize
            reqMsg.Body.FILENAME = os.path.basename(startPath)

            msgId += 1
            reqMsg.Header = Header(None)
            reqMsg.Header.MSGID = msgId
            reqMsg.Header.MSGTYPE = message.REQ_FILE_SEND
            reqMsg.Header.BODYLEN = reqMsg.Body.GetSize()
            reqMsg.Header.FRAGMENTED = message.NOT_FRAGMENTED
            reqMsg.Header.LASTMSG = message.LASTMSG
            reqMsg.Header.SEQ = 0

            MessageUtil.send(
                sock, reqMsg
            )  # Connect to the server, the client sends a file transfer request message.
            rspMsg = MessageUtil.receive(
                sock)  # Receive a response from the server

            if rspMsg.Header.MSGTYPE != message.REP_FILE_SEND:
                self.textLog.append(
                    'This is not a normal server response.\n{0}\n'.format(
                        rspMsg.Header.MSGTYPE))
                self.textLog.moveCursor(QTextCursor.End)
                exit(0)

            if rspMsg.Body.RESPONSE == message.DENIED:
                self.textLog.append('The server refused to send the file.\n')
                self.textLog.moveCursor(QTextCursor.End)
                exit(0)

            with open(
                    os.path.relpath(startPath), 'rb'
            ) as file:  # Prepare to open the file and send it to the server.
                totalRead = 0
                msgSeq = 0  # ushort
                fragmented = 0  # byte
                fragmentedCnt = 0
                if filesize < CHUNK_SIZE:
                    fragmented = message.NOT_FRAGMENTED
                else:
                    fragmented = message.FRAGMENTED

                while totalRead < filesize:
                    rbytes = file.read(CHUNK_SIZE)
                    totalRead += len(rbytes)

                    fileMsg = Message()
                    fileMsg.Body = BodyData(
                        rbytes)  # Send the file to the server
                    # in a 0x03 message before the transfer is complete.
                    header = Header(None)
                    header.MSGID = msgId
                    header.MSGTYPE = message.FILE_SEND_DATA
                    header.BODYLEN = fileMsg.Body.GetSize()
                    header.FRAGMENTED = fragmented
                    if totalRead < filesize:
                        header.LASTMSG = message.NOT_LASTMSG
                    else:
                        header.LASTMSG = message.LASTMSG

                    header.SEQ = msgSeq
                    msgSeq += 1

                    fragmentedCnt += 1

                    fileMsg.Header = header
                    MessageUtil.send(sock, fileMsg)

                self.textLog.append(
                    '# Fragmented count: {0}'.format(fragmentedCnt))
                self.textLog.moveCursor(QTextCursor.End)

                rstMsg = MessageUtil.receive(
                    sock)  # Get a receive to see if it's been sent properly

                result = rstMsg.Body
                self.textLog.append('File Receive Success: {0}\n'.format(
                    result.RESULT == message.SUCCESS))
                self.textLog.moveCursor(QTextCursor.End)

        except Exception as err:
            self.textLog.append('Exception has occurred.\n{0}\n'.format(err))
            self.textLog.moveCursor(QTextCursor.End)

        sock.close()
        self.textLog.append('The client finished.\n')
        self.textLog.moveCursor(QTextCursor.End)
Exemplo n.º 2
0
            if filesize < CHUNK_SIZE:
                fragmented = message.NOT_FRAGMENTED
            else:
                fragmented = message.FRAGEMENTED

            while totalRead < filesize:
                rbytes = file.read(CHUNK_SIZE)
                totalRead += len(rbytes)

                fileMsg = Message()
                fileMsg.Body = BodyData(rbytes)

                header = Header(None)
                header.MSGID = msgId
                header.MSGTYPE = message.FILE_SEND_DATA
                header.BODYLEN = fileMsg.Body.GetSize()
                header.FRAGMENTED = fragmented
                if totalRead < filesize:
                    header.LASTMSG = message.NOT_LASTMSG
                else:
                    header.LASTMSG = message.LASTMSG

                header.SEQ = msgSeq
                msgSeq += 1

                fileMsg.Header = header
                print("#", end='')

                MessageUtil.send(sock, fileMsg)

            print()
Exemplo n.º 3
0
            if filesize < CHUNK_SIZE:
                fragmented = message.NOT_FRAGMENTED
            else:
                fragmented = message.FRAGMENTED
    
            while totalRead < filesize:
                rbytes = file.read(CHUNK_SIZE)
                totalRead += len(rbytes)

                fileMsg = Message()            
                fileMsg.Body = BodyData(rbytes)

                header = Header(None)
                header.MSGID = msgId
                header.MSGTYPE = message.FILE_SEND_DATA
                header.BODYLEN = fileMsg.Body.GetSize()
                header.FRAGMENTED = fragmented
                if totalRead < filesize:
                    header.LASTMSG = message.NOT_LASTMSG
                else:
                    header.LASTMSG = message.LASTMSG

                header.SEQ = msgSeq
                msgSeq += 1
            
                fileMsg.Header = header
                print("#", end = '')

                MessageUtil.send(sock, fileMsg)

            print()