Thread: Question about sending binary data over socket

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    Question about sending binary data over socket

    Hi.

    I`m working on an application which reads a file from the disk, and sends it`s content over socket to a browser. The problem is that when the application starts sending the data, the browser shows the waiting for data icon for a while, and then stops with the no data received message. The function is the following:

    Code:
    void stream(int clientDescriptor, char *path)
    {
    	FILE *fp 			= NULL;
    	char *fileContent 	        = NULL;
    	long fileSize		= 0;
    	int i				= 0;
    
    	fp 		= fs_openFile(path, "rb");
    	fileSize	= fs_getFileSize(fp);
    	fileContent	= fs_getFileByPointer(fp);
    
    	for (i=0;i<fileSize;i++)
    	{
    printf("%c",fileContent[i]);
    		write(clientDescriptor, fileContent[i], 1);
    	}
    }
    The printf("%c",fileContent[i]); part is working fine, it prints the whole content of the file to the screen, but the same fileContent[i] doesn`t arrive to the browser, or maybe the browser waits for some extra character...

    I know there are many examples on the internet, but none of those worked for me. Maybe you can help me.


    Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your compiler should be complaining about that code. Make sure you have the warnings turned up and make sure you get rid of all of them. Those warnings are the compiler telling you you're probably/definitely doing something wrong.

    Quote Originally Posted by man 2 write
    ssize_t write(int fd, const void *buf, size_t count);
    Look at the type write() expects for buf. It's a void *. That void * can take a pointer to anything, including a char *, but you're sending it fileContent[i], which is just a plain old char. You would need to call it like:
    Code:
    write(clientDescriptor, &fileContent[i], 1);
    Add that ampersand to get an address (make it a char *). You also need to check the return value of all your file and network IO functions. Read the docs on write for details. If it fails, print a sensible error message by using the perror function.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    66
    That worked.

    Thank you very much.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    66
    Hi again.

    I have another problem at this part, maybe you can help me in this too.

    Now the content of the file is received correctly, but the browser doesn`t want to recognize the type of the media.

    Now I`m trying to send the content of an mp3 file with these headers:

    Code:
    	write(client, "HTTP/1.0 200 OK\r\n",strlen("HTTP/1.0 200 OK\r\n"));
    	write(client, "Connection: close, close\r\n",strlen("Connection: close, close\r\n"));
    	write(client, "Server: My stream server\r\n",strlen("Server: My stream server\r\n"));
    	write(client, "Content-Type: audio/mpeg\r\n",strlen( "Content-Type: audio/mpeg\r\n"));
    	write(client, "Content-Range: bytes 0-16379980/16379980\r\n",strlen("Content-Range: bytes 0-16379980/16379980\r\n"));
    	write(client, "Content-length:16379980\r\n",strlen("Content-length:16379980\r\n"));

    I installed an http header monitor, and as I see, the browser doesn`t receive any of these headers, or ignores them. Am I missing something?

    If you could help me with this thing too, it would be great.

    Thank you.

    P.S. I took these headers from Gnump3d, and there it works, but not in my application.
    Last edited by raczzoli; 08-01-2011 at 01:57 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Hmm...I'm not a HTTP expert, so you would have to consult the RFC for more details on whether you're talking HTTP correctly. Since it "works in Gnump3d", I'm guessing it's not the header content, but rather your socket. I also couldn't tell, with that limited sample of code, which of a myriad of problems you could be experiencing. Are you sure you're talking TCP, not UDP? Are you on the right port (probably 80)? Are you sure you got the socket set up correctly? I see you're still not checking the return value of your write calls (and probably not the socket creation/setup calls either). You need to check ever single IO call for error. For all I know, you didn't even set up the socket right and you aren't sending the headers anywhere. When you do that and come back with a sufficient code sample and have shown that you attempt to check for, catch and process errors with your network and file handling code, then I will help you more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending in socket
    By fairyjerry14 in forum C Programming
    Replies: 4
    Last Post: 10-08-2007, 07:11 AM
  2. sending zero bytes over TCP socket
    By nantonop in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-03-2007, 08:10 AM
  3. Trouble with sending binary data
    By Mcmaxx in forum C Programming
    Replies: 15
    Last Post: 04-11-2007, 05:44 PM
  4. Sockets... Sending Binary Data. Please help
    By avalanche333 in forum C++ Programming
    Replies: 16
    Last Post: 03-31-2007, 12:56 PM
  5. sending HTTP POST data with Socket
    By Overtaker in forum Networking/Device Communication
    Replies: 10
    Last Post: 09-07-2006, 10:11 AM