我正在处理一个家庭作业问题,在该问题中,我设置了一个客户端和服务器程序以模仿DNS请求,从而彼此对话。要发送数据,我正在使用struct.pack和struct.unpack方法。使用sendto()和recvfrom()函数打包并发送数据后,我在解压缩数据(4个整数,一个长整数和一个字符串)时遇到了问题。
我尝试了各种格式的字符串,并调用calcsize()和len()函数来尝试匹配字节长度要求,但是我仍然遇到此错误(struct.error:unpack需要42个字节的缓冲区)。
服务器端
Sub DeleteMeeting()
Dim i As Long, j As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.Namespace
Dim objFolder As Outlook.MAPIFolder
Dim objAppointment As Outlook.AppointmentItem
Set objOutlook = Outlook.Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)
Set oItems = objFolder.Items
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Licences")
r = ws.Cells(Rows.Count, 1).End(xlUp).Row
For i = 5 To r
For j = oItems.Count To 1 Step -1
If Cells(i, 6).Value <> "" And Cells(i, 5) = "Mobile Plant" Then
Set objAppointment = oItems.Item(j)
With objAppointment
If .Subject = "WBC - Send licence - " + ws.Cells(i, 4).Value + " " + ws.Cells(i, 12).Value Then
objAppointment.MeetingStatus = olMeetingCanceled
.Recipients.Add Sheets("Email List").Range("C3")
.Recipients.Add Sheets("Email List").Range("C4")
.Recipients.Add Sheets("Email List").Range("C5")
.Recipients.Add Sheets("Email List").Range("C6")
.Recipients.Add Sheets("Email List").Range("C7")
.Recipients.Add Sheets("Email List").Range("C8")
objAppointment.Save
objAppointment.Send
End If
End With
End If
If Cells(i, 6).Value <> "" And Cells(i, 5) = "Section 50" Then
Set objAppointment = oItems.Item(j)
With objAppointment
If .Subject = "WBC - Send licence - " + ws.Cells(i, 4).Value + " " + ws.Cells(i, 12).Value Then
objAppointment.MeetingStatus = olMeetingCanceled
.Recipients.Add Sheets("Email List").Range("C3")
.Recipients.Add Sheets("Email List").Range("C4")
.Recipients.Add Sheets("Email List").Range("C5")
.Recipients.Add Sheets("Email List").Range("C6")
.Recipients.Add Sheets("Email List").Range("C7")
.Recipients.Add Sheets("Email List").Range("C8")
objAppointment.Save
objAppointment.Send
End If
End With
End If
If Cells(i, 6).Value <> "" And Cells(i, 5) = "Section 50 Extension" Then
Set objAppointment = oItems.Item(j)
With objAppointment
If .Subject = "WBC - Send licence - " + ws.Cells(i, 4).Value + " " + ws.Cells(i, 12).Value Then
objAppointment.MeetingStatus = olMeetingCanceled
.Recipients.Add Sheets("Email List").Range("C3")
.Recipients.Add Sheets("Email List").Range("C4")
.Recipients.Add Sheets("Email List").Range("C5")
.Recipients.Add Sheets("Email List").Range("C6")
.Recipients.Add Sheets("Email List").Range("C7")
.Recipients.Add Sheets("Email List").Range("C8")
objAppointment.Save
objAppointment.Send
End If
End With
End If
If Cells(i, 6).Value <> "" And Cells(i, 5) = "Non Excavation Permit" Then
Set objAppointment = oItems.Item(j)
With objAppointment
If .Subject = "LBRuT - Send licence - " + ws.Cells(i, 4).Value + " " + ws.Cells(i, 12).Value Then
objAppointment.MeetingStatus = olMeetingCanceled
.Recipients.Add Sheets("Email List").Range("C3")
.Recipients.Add Sheets("Email List").Range("C4")
.Recipients.Add Sheets("Email List").Range("C5")
.Recipients.Add Sheets("Email List").Range("C6")
.Recipients.Add Sheets("Email List").Range("C7")
.Recipients.Add Sheets("Email List").Range("C8")
objAppointment.Save
objAppointment.Send
End If
End With
End If
If Cells(i, 6).Value <> "" And Cells(i, 5) = "Non Excavation Extension" Then
Set objAppointment = oItems.Item(j)
With objAppointment
If .Subject = "WBC - Send licence - " + ws.Cells(i, 4).Value + " " + ws.Cells(i, 12).Value Then
objAppointment.MeetingStatus = olMeetingCanceled
.Recipients.Add Sheets("Email List").Range("C3")
.Recipients.Add Sheets("Email List").Range("C4")
.Recipients.Add Sheets("Email List").Range("C5")
.Recipients.Add Sheets("Email List").Range("C6")
.Recipients.Add Sheets("Email List").Range("C7")
.Recipients.Add Sheets("Email List").Range("C8")
objAppointment.Save
objAppointment.Send
End If
End With
End If
Next j
Next i
End Sub
客户端
# Create a UDP socket. Notice the use of SOCK_DGRAM for UDP packets
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Assign server IP address and port number to socket
serverSocket.bind((serverIP, serverPort))
print("The server is ready to receive on port: " + str(serverPort))
fileReader = open("dns-master.txt", "r") # open dns-master.txt
dnsRegistry = []
for line in fileReader: # read line by line and fill each into a list
if (line[0] != '#') & (line[0] != "\n"):
dnsRegistry.append(line.rstrip())
print(line.rstrip())
# loop forever listening for incoming UDP messages
while True:
# Receive and print the client data from "data" socket
data, address = serverSocket.recvfrom(1024)
print("data length " + str(len(data)))
unpackedData = unpack('iilii22s', data)
print("unpacked " + str(unpackedData))
要打包的字段是print(questionString)
qs = bytes(questionString, 'utf-8')
data = pack('iilii22s', messageType, returnCode, messageIdent, questionLen, answerLen, qs) # pack the data
for i in range (0, 3): # send request up to three times
# Send data to server
print("Sending Request to " + hostIP + ", " + str(port) + ":")
print("Message ID: " + str(messageIdent))
print("Question Length: " + str(questionLen) + " bytes")
print("Answer Length: " + str(answerLen) + " bytes")
print("Question: " + questionString)
clientsocket.sendto(data, (hostIP, port))
,这是我已转换为字节对象的字符串,qs
,是介于1到100之间(包括1和100)的随机整数,messageIdent
是长度questionLen
(在我的情况下为22)中,其余均为0-2之间的整数。我希望服务器程序可以解压缩这些值并将其提取到适当的字段中。当我打印questionString
时似乎正在执行此操作,但是我遇到了回溯错误以及我在问题标题中提到的struct.error。
输出:
data
因此,我又如何才能将这些值打包到一个结构中,发送到另一个程序,然后正确地解压缩以便使用这些值?还要注意,当我在服务器端打印The server is ready to receive on port: 9999
host1.student.test A IN 3600 192.168.10.1
host2.student.test A IN 3600 192.168.20.2
host3.student.test A IN 3600 192.168.30.3
host99.student.test A IN 3600 192.168.90.9
host123.student.test A IN 3600 192.168.120.123
Traceback (most recent call last):
File "C:/Users/Adam/Documents/NJIT/6. Fall 2019/CS356 - Intro to Computer Networks/DNS-Assignment/server.py", line 32, in <module>
unpackedData = unpack('=iilii22s', data)
struct.error: unpack requires a buffer of 42 bytes
data length 42
unpacked (1, 0, 36, 22, 0, b'host1.student.net A IN')
data length 14
时,它被打印两次,并且每次都得到两个不同的值。为什么会这样?