需求:服务端 接收客户端数据存入到本地 数据包格式 包头前4个字节是一个int 解析出来指的是后面跟随的数据的大小 (字节数)

这里我们就要解析包头 存入数据

其实这个思路很简单 就是用socket接收4个字节 解析出int 数据包的大小为x 再去接收x个字节的数据存入到本地就可以了

注意: 由于网络可能会有延迟 recv不一定能完全接收到相应大小的数据 比如 要接收2000个数据 结果只接收了1900个 所以要写个循环判断不断接收到相应字节数为止 包头的那4个字节也要写相同的判断 虽然4个字节漏的概率很小 但是考虑到安全性

下面我给出的是python代码

import socket
import struct

'''
The code is server for receiving a voice data, and save in a file 
'''

#create server

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0)  # create socket

HOST = '0.0.0.0' # set host ip 127.0.0.1 will failed so set 0.0.0.0
PORT = 8888 # set host port
s.bind((HOST,PORT)) # bind host and port

s.listen(5) # begin listenging , can conneted max host number is 1

'''
       The following function ensure the packdata can reavice right
       pack_date is received packet data
       pack_date_len is now haved receiving data
       need_packetdata_len is we then need add packet data length for ensuring pack_data complete 
       packet_len is packet head tell us the packet data length
       file for write the voice bit
       
       algorithm:
       if recv data is less packet_len we need, we should add packet we should need until the we received data complete
       
       function use:
       parameter initialize: packet_data_len initialize 0 for while can run, and need_packetdata_len should be packet_len for begining receive
'''
def ReceiveData(packet_data_len,packet_len,need_packetdata_len,file):
    while (packet_data_len < packet_len):
        packet_data = con.recv(need_packetdata_len)
        packet_data_len = len(packet_data) + packet_data_len
        need_packetdata_len = packet_len - packet_data_len
        print("now have receive packet_data len is", packet_data_len)
        file.write(packet_data)

'''
    This function algorithm is same as upper function
    packet_head_len need us to write how long the packet head    
    return the packet len for know the following data length
'''
def ParsingPacketHead(packet_head_len):
    packet_head = con.recv(packet_head_len)
    recv_packet_head_len = len(packet_head)
    while (recv_packet_head_len < packet_head_len):
        need_add_length = packet_head_len - recv_packet_head_len
        packet_head = packet_head + con.recv(need_add_length)
        packet_head_len = len(packet_head)
        print("now packet head len is ",len(packet_head))
    print("break while, packet head len = ",len(packet_head))
    packet_len, = struct.unpack('i', packet_head)  # unpack byte to int
    return packet_len

while(True):
    con, addr = s.accept()  # build client connection, con received new socket object, addr received client ip and  process
    file = open("test.pcm", mode='wb')  # create file for saving binary file
    while(True):
        print('client infomation is ', addr)
        packet_len = ParsingPacketHead(4)
        print("packet_len is ",packet_len)
        ReceiveData(0,packet_len,packet_len,file)

    file.close()
   

 嘿嘿 英文很菜 linux的pycharm中文不太兼容 就随便写的注释 代码很短 还是参考代码为主

 

posted on 2021-12-31 13:54  Rabbit_XIN  阅读(433)  评论(0)    收藏  举报