LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

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
paulsm4
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:
Code:
  // 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
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 435

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


Thanks again.
 
Old 04-06-2006, 05:35 AM   #4
primo
Member
 
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.
 
  


Reply


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


All times are GMT -5. The time now is 08:34 PM.

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