1

struct ss { int data ;string name;}*o;

this is my structure in the client application i want to send that to the server (in TCP Server under windows); How to do that.. i Know the serilization is the solution. but i don't know how to do that.. please help me if you can.

4 Answers 4

2

http://www.parashift.com/c++-faq-lite/serialization.html

If you use Boost for serialization then read this Serialize and send objects by TCP using boost

I normally write my own data format for transferring this data.

I will create a character buffer.

Put the size of the packet. Convert integer to bytes and copy it. Append the string length Copy the name send it across.

Sign up to request clarification or add additional context in comments.

Comments

0

First one must know whether client and server are known to be always the same architecture or not. This decides whether you can just send data as it is or whether you must care about endianness and the size of an integer. In any case, ntohl and htonl will take care of byte ordering and allow you to transfer the int in a simple, defined way (no-op on machines that are already network-byte-order).

About the string, you can send both the size and the contents over TCP just fine (converting the size with htonl), assuming that the string data is either in the same encoding on both sides, or a "general, agnostic" encoding is always used, such as UTF-8.
If you don't know what encodings the machines on both ends are using, you are in trouble. In that case, you must include a message that defines this and convert accordingly (similar to as for example webservers do).

Having TCP operate in "normal mode" means that Nagle's algorithm will be enabled, so you can just use 3 calls to send and the network layer will coerce that into as few packets as it believes is reasonable (instead of sending an individual packet just for an integer).

That all for the simple case in your example, or you can do some proper serialization, which is much more work, of course.

Comments

0

This is a very good guide: http://beej.us/guide/bgnet/output/html/multipage/index.html. it is not strictly for windows but the changes are very slight.

You basically need to serialize your data to a buffer and then use send function. pass your socket identifier and buffer etc...

int send( 

__in SOCKET s, __in const char *buf, __in int len, __in int flags );

from http://msdn.microsoft.com/en-us/library/ms740149(v=vs.85).aspx

Comments

0
// send some data to a socket send( socket, // the open socket o, // pointer to the data sizeof( ss ), // number of bytes 0 ); // no special flags 

The difficulty is at the other end! The recipient needs to know how many bytes to read, and what structure to store them in.

You either need to write your own code to deal with these problems, or use a protocol that works on top of sockets. The code isn't hard, but some experience with some of the many protocols available would help with the gotchas!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.