如何在Python 2.7 UDP套接字中创建序列号和ACK

时间:2019-04-20 04:13:00

标签: python python-2.7 sockets header udp

我有下面的代码。现在,我只是将sequence numberACK放入有效负载中。我想知道两件事:

  1. 如何将此信息放入客户端和服务器的标头中?
  2. 如何在客户端上打印出ACK,在服务器上打印出序列号?

我正在Linux上使用Python 2.7.13。


客户

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.settimeout(4.0)
message = "Sequence Number: 100\nACK: 1"
addr = ("127.0.0.1", 6000)

client_socket.sendto(message, addr)
try:
    data, server = client_socket.recvfrom(1024)
    # Print the ACK the server sent
except socket.timeout:
    print('Timed out')

服务器:

import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(("127.0.0.1", 6000))

while True:
    message, address = server_socket.recvfrom(1024)
    # Print the sequence number the client sent
    # Set ACK to 0 and send this ACK to the client
    server_socket.sendto(message, address)

0 个答案:

没有答案