LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 11-29-2010, 05:00 AM   #1
Carlos2313
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Rep: Reputation: 0
Question Sending binary data through sockets in C


Hi everyone,

I am coding a http server which has to send the file(s) such as images, .avi files, .mpeg, that the client is going to request.
I have been trying of sending files through sockets .

Quote:
char info [256];
bzero(info, 256);
//memset(&info,0,sizeof(info));

read(socket, info, 255);
write(socket, HTTP, 255);
FILE *fl= fopen(info,"rb");
fseek(fl, 0, SEEK_END);
long len = ftell(fl);
printf("largo: %ld\n", len);
unsigned char *ret = (char*) malloc(len);
fseek(fl, 0, SEEK_SET);
fread(ret, 1, len, fl);
However, it's supposed to be shown in Mozilla Firefox (as the client). But it is not doing it, so.. It's just not getting the complete file...
So, could some please give me a guide?

Last edited by Carlos2313; 11-29-2010 at 05:04 AM.
 
Old 11-29-2010, 05:47 AM   #2
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Oh boy,

Code:
read(socket, info, 255);
What if the client sends less than or more than 255 bytes?

Code:
write(socket, HTTP, 255);
So HTTP is exactly 255 bytes long?

Code:
FILE *fl= fopen(info,"rb");
fseek(fl, 0, SEEK_END);
long len = ftell(fl);
What if fopen() fails?

Code:
unsigned char *ret = (char*) malloc(len);
fseek(fl, 0, SEEK_SET);
fread(ret, 1, len, fl);
Do you really want/need to read the whole file into a buffer, regardless of its size?

You never send anything back to the client.
 
  


Reply



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
How to detect broken connection before sending data with sockets Fabio Paolini Linux - Networking 3 11-03-2010 04:59 AM
Sending and receiving data simultaneously using BSD sockets nagaraju1024 Linux - Networking 2 06-27-2010 10:17 PM
PHP5 binary with enable-sockets OliXNet Linux - Newbie 1 11-16-2006 01:37 AM
Sockets and Binary data oulevon Programming 3 04-06-2006 04:35 AM
send/recv binary files with C++ sockets dafatdude Programming 3 02-12-2006 08:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 AM.

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
Open Source Consulting | Domain Registration