Hello Friends,

I am new to c programming. I want to create data structure for client server communication. I can simply transmitt simple char to server and return it back. My requirement is to have dynamic number of variable in client side i.e my client should be able to transmitt any number of variable to server. Sometime it would be 1 or 2 or so on. On other hand my server should respond it accordingly independent of number of variable.
Please help me how should I approach in creating such datastructure.

Thanks

Personally I would send the type followed by 2 bytes for the size ( unless the type dictated size )

For instance

If :

INT = 1;
STRING = 2;

Then sending the int 5 followed by the string HELLO would result in the following message

01 00 00 00 05 02 00 05 48 45 4C 4C 4F

If you use the terminiating char for the string then you don't have to include the size for the string, although this means you can't simply jump it if you want to ignore it.

You may want to encode and decode from some form of linked list, if you need anymore type information ( for instance, some messages may go to controller x and some to controller y, then just add a byte to dictate that as well ).

Thanks Paul.
Is it possible to send all data in one shot ? Here number of data will be generated dynamically by some controller. So at one time client will have 5sets of varibale or at one some n sets of variable. I am looking for unique client-server code that will independent of number of data send in one shot.
Your inputs will help me to design such kind of data structure.

Thanks

If you're using UDP 65,507 bytes is the largest size, for TCP it's 1GB.

If you're using TCP there is very little reason not to send the data in smaller chunks, then combining the buffers again at the other end. TCP should deal with the order of the packets, but if you're going to take this approch you may also want to have 4 bytes at the start describing how long the message is.

If your using UDP and you plan to send in multiple chunks you either have to add a sequence number ( such as in tcp ) or send in such a way that you don't spit any of the data.

http://pic.dhe.ibm.com/infocenter/tpfhelp/current/index.jsp?topic=%2Fcom.ibm.ztpf-ztpfdf.doc_put.cur%2Fgtpc2%2Fcpp_send.html

Thanks Paul...this would help me.

UDP is not recommended for long distances because there is no guarentee that the packets will ever arrive. TCP is best for that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.