Example: How to Packetize a TCP Stream

原创 2002年11月08日 09:35:00

The following example code is designed to replace the recv() call. recv_packet() differs by returning only when it has received a full packet, or has failed trying. This is only example code, for educational purposes. See below for more information on the code's design limitations and ways you can work around them.

The default packet size of 2 bytes is large enough to allow 64 KB packets. That's the size I use in my programs, but the code is flexible enough to allow any packet size you want, up to 4 bytes. Beyond that, you'll have to update the bit-shifting logic to be able to handle integers beyond 32 bits, or, if your compiler and platform support them, use 64-bit integers. Also, if you choose to use 1-byte length prefixes, you can simplify the code below significantly.

The code assumes that the length prefix is part of the packet. That is, if you are sending 8 data bytes, a 2-byte length prefix will be "10" for this packet. It's not necessary to do it this way, but it does make the code simpler.

recv_packet.cpp



static const int   
static char 
static int   





int  , char , int 

    int   
    int 
    bool   true

      sizeof

    
    if    
          
        , , 
          
          
    

    
    while   
        if  
            if    
                  
                for int        
                      
                      
                
                  false
                if    
                    
                    
                    return 
                
            
        

        if      
            break  
        

        int   ,   ,
                  , 
        if        
            return        
        
          
    

    
    
        
    ,   , 
    
    return 


There are several reasons not to use this code as-is in a real program. First, this code has no real error handling. Since every program handles errors differently, I've simply marked the places you need to add error handling code.

The second major problem is the global variables. They prevent you from using recv_packet() with more than one socket or with multiple threads. You can move them into a structure and pass an instance of that structure to recv_packet(). Or, you can wrap the function and all the data it needs up into a class. This would be a good start towards your own socket class library.

Third, notice that the code checks that the holding buffer and the caller's buffer are the same size. If the callers' buffer is larger than the holding buffer, the memcpy() call at the bottom of recv_packet() can overflow the holding buffer. If the holding buffer is larger than the callers' buffer and the recv() call returns enough bytes, the memcpy() call at the top of the function can overflow the callers' buffer. This is a symptom, rather than the problem itself. The actual problem is that the buffering logic is too simple to allow more complex usage patterns. This is good for educational purposes, but bad for efficiency. The key feature of a better buffering mechanism would be giving recv_packet() access to additional memory, so that overflows wouldn't be an issue. One way to do this would simply be to allow recv_packet() to allocate dynamic memory. Another would be to set up a large ring buffer. A related improvement would be if recv_packet() could return more than one packet per call, which would save all the shuffling that goes on in the current code when more than one packet gets read into the holding buffer. When considering new buffering strategies, be sure not to use a design that encourages multiple calls to recv() with small buffers.

The fourth problem is that the code does not scale well beyond 2-byte prefixes. If you use 3-byte prefixes, that demands up to a 16 megabyte buffer, and for 4-byte prefixes you'd need a 4 gigabyte buffer. If you find yourself needing to transmit such large messages and you can't split them up into smaller packets, you'll probably need to use some other buffering area than main memory. The right storage area to use for packet buffering will depend on your program, of course.

The final problem is that recv_packet() is only usable with blocking sockets.

How Many Answers Are Wrong 并查集

题意为前两个数之间的和为第三个数,这个题我们可以这么考虑,比如a 到 b之间和为 s,那么 s = (开始到b之间的和) - (开始到a-1之间的和),ab的根相等 那么它们在一个集合里 那么s即为它...
  • tydfhihfhgjkj
  • tydfhihfhgjkj
  • 2015年09月14日 22:43
  • 123

5-TCP Client&Server Example

Please indicate the source: http://blog.csdn.net/gaoxiangnumber1Welcome to my github: https://github...
  • gaoxiangnumber1
  • gaoxiangnumber1
  • 2016年09月10日 10:00
  • 776

Nginx发布1.9.0版本,新增支持TCP代理和负载均衡的stream模块

文章来源:http://zhangge.net/5037.html昨天在公司微信群,CTO分享了这个消息,对运维来说以后基于TCP协议的后端业务的高可用又多了一个新的选择,实在是棒极了!一直以来,Ng...
  • libaineu2004
  • libaineu2004
  • 2015年08月19日 15:03
  • 40654

Linux C TCP Programming example

//server.c #include #include #include #include #include #include #include void error(const ...
  • adream307
  • adream307
  • 2016年02月09日 15:25
  • 297

wireshark的follow tcp stream功能 使用时要注意的一个问题

有时候抓到的包用rtsp过滤了之后,存在多个tcp连接,导致用
  • ljh081231
  • ljh081231
  • 2014年04月29日 17:13
  • 5471

Java TCP NIO Example

Server端: import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; ...
  • difffate
  • difffate
  • 2017年04月07日 21:37
  • 327

Spark学习笔记 --- SparkStreaming 实现对 TCP 数据源处理

Spark学习笔记 --- SparkStreaming 实现对 TCP 数据源处理
  • u012965373
  • u012965373
  • 2017年03月30日 15:58
  • 846

Internet thin-stream 交互式应用 TCP延迟

Internet thin-stream 交互式应用 休闲 TCP延迟   1. thin-stream的特征     在大量使用可靠传输协议的Internet服务中...
  • caodongfang126
  • caodongfang126
  • 2017年09月15日 18:02
  • 88

Nginx1.9 配置TCP负载均衡

将nginx-1.9.9.tar.gz解压 tar -zxvf nginx-1.9.9.tar.gz mv nginx-1.9.9 nginx cd nginx ./configure -...
  • chan112147
  • chan112147
  • 2016年01月05日 12:35
  • 2514

Nginx TCP代理及负载均衡stream模块简介

Nginx从1.9.0开始发布ngx_stream_*_module模块,该模块支持tcp代理及负载均衡。注意和http层代理和负载均衡区别开来,这可是在网络4层上做的负载均衡哦。举个使用stream...
  • wangjianno2
  • wangjianno2
  • 2017年07月16日 13:18
  • 585
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Example: How to Packetize a TCP Stream
举报原因:
原因补充:

(最多只允许输入30个字)