Socket-programming: recv ()

I have an application in which various objects communicate with each other via sockets, and I use the C programming language. When an object sends a long message to another object, the recv () function can read this message in parts. Therefore, I must rebuild the message on the recipient side, adding all the received parts.

My question is a general socket programming question related to recv (). How does recv () know when a message has been fully read? Should I abort a message with a special character, such as "\ n"? or should I send the message size as a header? What is common practice?

+4
source share
3 answers

, . - .

, , : ( ), ( ​​ , ). , . - , , .

+6

send() recv() .

:

send(new_socket,message,strlen(message),0);

- .

, , TCP-, send() recv() . , , , , send().

\0 .

+2

C, , ! , , , C , !

, ZeroMQ (http://zeromq.org/bindings:c) C. , .. , ; , .

ZeroMQ , . , (AFAIK), , , - , - . , .

-, . , , . , ASN.1 Objective Systems (http://www.obj-sys.com/index.php). , , , .

As well as serialization procedures, they give you very convenient extras that C does not provide. For example, their code generator will provide you with routines for copying data types, which is very convenient if this data type is a structure full of pointers that reference allocated memory.

There probably are some free tools and libraries. Google protocol buffers that have a C binding ( http://code.google.com/p/protobuf-c/ ) are a good alternative .

0
source

All Articles