How do I send [this] over a socket?

Answer Posted / chaitanya

Anything other than single bytes of data will probably get mangled unless you take care. For integer values you can use htons() and friends, and strings are really just a bunch of single bytes, so those should be OK. Be careful not to send a pointer to a string though, since the pointer will be meaningless on another machine. If you need to send a struct, you should write sendthisstruct() and readthisstruct() functions for it that do all the work of taking the structure apart on one side, and putting it back together on the other. If you need to send floats, you may have a lot of work ahead of you. You should read RFC 1014 which is about portable ways of getting data from one machine to another (thanks to Andrew Gabriel for pointing this out).

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How does a socket work?

487


What is socket address with example?

485


What is the purpose of socket?

498


How many sockets can a port have?

477


Is tcp or unix socket faster?

492






Are sockets files?

494


How do unix sockets work?

486


Why does the sockets buffer fill up sooner than expected?

988


How can I force a socket to send the data in its buffer?

1088


What is the difference between a socket and a port?

458


How to find other end of unix socket connection?

507


What is a socket api?

521


How can I set the timeout for the connect() system call?

1104


How can I write a multi-homed server?

1271


How many sockets can a cpu have?

490