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 many sockets can a port have?

560


What is the function of socket?

577


What are the pros/cons of select(), non-blocking I/O and SIGIO?

1040


What is active unix domain sockets?

618


Can multiple sockets use the same port?

565






Is there any advantage to handling the signal, rather than just ignoring it and checking for the EPIPE error? Are there any useful parameters passed to the signal catching function?

1105


Why do we need socket programming?

566


How many socket connections can a server handle?

539


How to find other end of unix socket connection?

598


How does unix socket work?

574


What is a socket set used for?

595


What is a deep well socket?

607


How does a socket work?

560


Where is the socket located?

550


What is the purpose of socket?

594