Example: How to Packetize a TCP Stream (轉)
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 platfosupport 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.
/wskfaq/examples/src/recv_packet.cpp">recv_packet.cpp
#include
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 consring new buffering strategies, be sure not to use a design that encourages .
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.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-996658/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- An Example of How Oracle WorksOracle
- JDBC Connection Pool Example (轉)JDBC
- [轉]Angular 4|5 Material Dialog with ExampleAngular
- [TCP]轉TCP相關知識TCP
- 玩轉TCPTCP
- Spark exampleSpark
- oracle exampleOracle
- An example of polybase for OracleOracle
- angular 2 by exampleAngular
- Oracle By Example (OBE)Oracle
- simd example code
- How Users Read on the Web (轉)Web
- [轉]How to release space from databaseDatabase
- 轉:How is Internat Bank going?Go
- [轉]how to introduce yourself in the interviewView
- How Oracle Store Number internal?(轉載)Oracle
- The Cryptography API, or How to Keep a Secret(四) (轉)API
- An example about git hookGitHook
- react router animation exampleReact
- [Typescript] Query builder exampleTypeScriptUI
- Nginx基於TCP/UDP埠的四層負載均衡(stream模組)配置梳理NginxTCPUDP負載
- Forms Personalization in Oracle Apps R12 Example: Zoom Functionality(轉)ORMOracleAPPOOMFunction
- Java 中將 InputStream 轉換為 StreamJava
- UDP轉TCP隧道工具udptunnelUDPTCP
- perl script for scan TCP ports .(轉)TCP
- [轉帖]【tcp】關於tcp 超時重傳次數TCP
- A example that using JQuery clonejQuery
- a simple example for spring AOPSpring
- An Application Context exampleAPPContext
- 如何將CentOS 8轉換為CentOS StreamCentOS
- list轉map,使用java8,stream流Java
- 用流(stream)技術做下載 (轉)
- Java-stream(1) Stream基本概念 & Stream介面Java
- 理解 TCP(四):狀態流轉TCP
- TCP/IP協議原理【轉載】TCP協議
- 【RabbitMQ】direct type exchange example in golangMQGolang
- 【RabbitMQ】topic type exchange example in golangMQGolang
- 【RabbitMQ】fanout type exchange example in golangMQGolang