gpt4 book ai didi

Python:尽管尝试了多种方法,ABP 位仍然没有翻转,如何修复?

转载 作者:行者123 更新时间:2023-12-01 05:32:01 26 4
gpt4 key购买 nike

为了回应被搁置的问题,我修改了我的问题。希望这能更好地符合 SO 标准。

该程序的目的是构建和发送使用交替位协议(protocol)作为简单重发机制的 UDP 数据包。我已经确认数据包可以正确发送和接收。问题在于 ABP 位及其翻转。

我现在面临的问题是,尽管尝试了多种不同的方法,但我无法翻转用于确认收到的数据包或确认是否是正确编号的 ABP 位。我首先发送一个 ABP 位 = 0 的数据包,作为响应,接收进程应该看到这一点并发回一个 ABP 位 = 0 的确认。接收到该数据后,发送程序将其 ABP 位翻转为 1,并发送具有该 ABP 位值的新数据包。接收方收到后,发送一个匹配的 ABP 位=1 的 ack,发送方收到后,将其位翻转回 0,继续循环,直到程序发送完信息。

下面的代码,抱歉有点长,但它已经完成并且可以运行了。该程序需要四个命令行参数,这是我一直在使用的命令:

% python ftpc.py 164.107.112.71 4000 8000 manygettysburgs.txt

其中 ftpc.py 是发送程序的名称,164.107.112.71 是 IP 地址,4000 和 8000 是端口号,manygettysburgs.txt 是我一直发送的文本文件。如果使用不同的 .txt 应该不会有什么影响,但为了完全准确,请使用长度在 8000 到 9000 个字符之间的文件。

import sys
import socket
import struct
import os
import select

def flipBit(val): #flip ABP bit from 0 to 1 and vice versa
foo = 1 - val
return foo

def buildPacketHeader(IP, Port, Flag, ABP):
#pack IP for transport

#split into four 1-byte values
SplitIP = IP.split('.')

#create a 4-byte struct to pack IP, and pack it in remoteIP
GB = struct.Struct("4B")

remoteIP = GB.pack(int(SplitIP[0]),int(SplitIP[1]),int(SplitIP[2]),int(SplitIP[3]))
#remoteIP is now a 4-byte string of packed IP values

#pack Port for transport

#create a 2-byte struct to pack port, and pack it in remotePort
GBC = struct.Struct("H")

remotePort = GBC.pack(int(Port)) #needs another byte
#remotePort is now a 2-byte string

#add flag
flag = bytearray(1)
flag = str(Flag)

#add ABP
ABP = flipBit(ABP)
abp = str(ABP)

#create header and join the four parts together

Header = ''.join(remoteIP)
Header += remotePort
Header += flag
Header += abp

return Header


#assign arguments to local values

IP = sys.argv[1]
PORT = sys.argv[2]
TPORT = sys.argv[3]
localfile = sys.argv[4]

#declare the socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

#create destination information arrays
remoteIP = bytearray(4)
remotePort = bytearray(2)

#create flag array
flag = bytearray(1)

#create ABP bit
bit = 1
print bit

#send file size packet

#get size of file from os command
filesize = bytearray(4)
#save as 4-byte string
filesize = str(os.stat(localfile).st_size)

#package the filesize string
filesizestr = buildPacketHeader(IP,PORT,1,bit) #build header
print bit
filesizestr += filesize #complete the packet
s.sendto(filesizestr, ('127.0.0.1', int(TPORT))) #send packet

# end of send file name packet


#begin listening for responses
read, write, err = select.select([s], [], [], 1) #timeout set to 1 seconds

if len(read) > 0:
#data received
data = read[0].recv(1200)
if data[7] != bit:
print "failed ack"
#resend packet
else:
print "Timeout."
#resend packet

#send next data packet

#get filename as string (from arg4 diredtly)
filename = bytearray(20)
#save as 20-byte string
filename = localfile

#package the filename string
filenamestr = buildPacketHeader(IP,PORT,2,bit) #build header
print bit
filenamestr += filename #complete the packet
s.sendto(filenamestr, ('127.0.0.1', int(TPORT))) #send packet

#end of send file name packet


#send file content packet

#reading while loop goes here

with open(localfile, 'rb', 0) as f: #open the file
while True:
fstr = f.read(1000)
if not fstr:
print "NOTHING"
break

#put together the main packet base
filecontentstr = buildPacketHeader(IP,PORT,3,bit)
print bit

filecontentbytearray = bytearray(1000) #create ytear array
filecontentbytearray = fstr #assign fstr to byte array

filecontentsend = ''.join(filecontentstr) #copy filecontentstr to new string since we will be using filecontentstr again in the future for other packets

filecontentsend += filecontentbytearray #append read data to be sent

s.sendto(filecontentsend, ('127.0.0.1', int(TPORT))) #send the file content packet

#end of send file content packet
s.close()

在此代码中,每次调用 buildPacketHeader 时,它都会执行 FlipBit 作为其操作的一部分。 FlipBit 应该翻转 ABP 的位值。我已设置打印以在所有调用 buildPacketHeader 之后打印出新的 bit 值,以便跟踪该值。然而,每当我运行该程序时,我总是看到 ABP 位的值相同。

我尝试了多种方法,包括更改为 boolean 值。以下是我尝试过的对 FlipBit 的一些更改:

def flipBit(val):   #flip ABP bit from 0 to 1 and vice versa
if val == 0:
val = 1
else:
val = 0

return val

还有一些用 boolean 值代替:

def flipBit(val):
val = not val
return val

def flipBit(val):
val = (True, False)[val]
return val

根据过去的经验,我认为其中许多方法实际上都是可行的选择。也就是说,我完全困惑为什么它在这个程序中没有按预期工作。我认为这是我对 python 缺乏经验的错,因为尽管现在已经使用它相当长的时间了,但仍然有一些我没有注意到的特性。任何帮助是极大的赞赏。

最佳答案

我不明白你对 Python int 的反对是什么,但是 ctypes 模块提供了一个低级可变对象的世界;例如,

>>> import ctypes
>>> i = ctypes.c_ushort(12) # 2-byte unsigned integer, initial value 12
>>> i
c_ushort(12)
>>> i.value += 0xffff - 12
>>> hex(i.value)
'0xffff'
>>> i.value += 1
>>> i.value # silently overflowed to 0
0

关于Python:尽管尝试了多种方法,ABP 位仍然没有翻转,如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20011682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com