Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-06-2006, 12:43 AM   #1
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Sockets and Binary data

I have to write a program that sends binary files over a TCP socket in C. I've worked with sockets before but only with text. Is there anything I need to worry about before I start? For example, when one opens a file for reading, one needs to specify whether it's binary or not. Does this need to be done using TCP sockets too? Also, I normally use a char buffer to read data from the stream, what should one use with binary data?

Thanks for any help or pointers.
Old 04-06-2006, 12:57 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Sockets are intrinsically "binary" (for example, there's no such thing as "text mode" vs "binary mode" with a socket).

"char buff[BUFMAX]" is absolutely the right way to go, so you're covered there, too.

Byte order is definitely an issue:
  // BAD: potential read error on different platforms
  unsigned long u = 0xabcd;
  write (s, &u, sizeof (unsigned long));

  // BETTER (you'll need a corresponding "ntohl()" on the receiving end...)
  unsigned long u = htonl (0xabcd);
  write (s, &u, sizeof (unsigned long));
But basically, all of your reads and writes are just a binary byte stream. If you treat it as such, you should have no problem.

'Hope that helps .. PSM
Old 04-06-2006, 01:14 AM   #3
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Original Poster
Rep: Reputation: 30
Thanks for your help. You answered all my questions.

Thanks again.
Old 04-06-2006, 05:35 AM   #4
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Be aware that systems are not expected to agree on sizeof(unsigned long). Be aware of structures directly passed to socket, too, because the offsets that lead to the struct members may be different across platforms. The best way would be to strictly declare each member with types such as uint32_t, etc. and code a function to write the structure. You may use writev() to add each member and specify its size, which is better than manually parsing each one.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
writing binary data via the shell? zero79 Programming 3 03-04-2006 12:35 PM
send/recv binary files with C++ sockets dafatdude Programming 3 02-12-2006 09:02 AM
sockets dont ever send data alaios Programming 1 09-21-2005 07:59 PM
Can't figure out how to be listening for data all the time with C sockets Eddie101 Programming 2 03-25-2005 11:34 PM
recording raw binary data as an MP3 uman Linux - General 1 02-19-2005 10:07 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:42 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration