socket编程可靠的数据协议

时间:2017-03-24 23:43:05

标签: python sockets tcp udp

对于项目的第1部分,您将实现类似于TCP的简单回溯N协议。该协议称为352RDPv1。 (不幸的是我的python知识不是那么强大,我被迫在python中编写代码)

我必须实现: init(udp_port1,udpport2)

socket()

连接(地址)

我得到以下伪代码:

import binascii
import socket as syssock
import struct
import sys

# this init function is global to the class and
# defines the UDP ports all messages are sent
# and received from.
def init(UDPportTx,UDPportRx):   # initialize your UDP socket here
    # create a UDP/datagram socket 
    # bind the port to the Rx (receive) port number 
pass 

class socket:

def __init__(self):  # fill in your code here
    # create any lists/arrays/hashes you need
    return

def connect(self,address):  # fill in your code here
    global UDPportTx  # example using a variable global to the Python module 

    #  create a new sequence number 
    #  create a new packet header with the SYN bit set in the flags (use the      Struct.pack method)
    #  also set the other fields (e.g sequence #) 
    #   add the packet to the outbound queue
    #   set the timeout
    #      wait for the return SYN
    #        if there was a timeout, retransmit the SYN packet 
    #   set the outbound and inbound sequence numbers  
    return 

到目前为止,我已经尝试了几种方法,但我知道我有错误而且我的程序不起作用。

import binascii
import socket as syssock
import struct
from collections import namedtuple
import sys
import select

version = 0x1
header_len = 7
payload_len = 0
flags = 0
SOCK352_SYN = 0x01
SOCK352_FIN = 0x02
SOCK352_ACK = 0x04
SOCK352_RESET = 0x08
SOCK352_HAS_OPT = 0xA0
sequence_no = 0
ack_no = 0
timeout = 0.2
#given these values to set them to


def init(UDPportTx,UDPportRx):
    global sock
    global useRx
    global useTx
    useTx = int(UDPportTx)
    useRx = int(UDPportRx)
    sock = syssock.syssock(syssock.AF_INET, syssock.SOCK_DGRAM)
    sock.bind(('', useRx))
    print "Listening on port :", useRx
pass

class socket:
def __init__(self):
    global pkt
    pkt = namedtuple("pkt",["version", "flags", "sequence_no", "ack_no", "payload_len"])
    return

 def connect(self, address):

            global header_raw
            udpPkt = struct.Struct('!BLBBB')
            header_raw = udpPkt.pack(version, SOCK352_SYN, sequence_no, ack_no, payload_len)
            sock.sendto(header_raw, ('', useTx))
            return

我相信我在前几种方法中遇到错误,在进入其他方法之前,我需要考虑一下,我想看看是否有人能够帮助我理解如何处理这几个方法。

0 个答案:

没有答案