LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-17-2004, 01:24 PM   #1
dafatdude
LQ Newbie
 
Registered: Oct 2003
Posts: 18

Rep: Reputation: 0
send/recv binary files with C++ sockets


hi all,

i'm just throwing around some ideas for an algorithm on how to send a binary file over a network socket. in C++. i realise you need to put the bytes into a buffer and send the buffer, but how are you supposed to determine the length of it? Is there a simpler method?

Also, when you receive the buffer on the other end, what would be the best way to put the filestream back together.

thanks

dafat
 
Old 05-17-2004, 04:16 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
There's a lazy and high performance way to do it on the sending side: see sendfile(2)

Sockets and tcp don't give you a way on the receiving side to figure out how long the file is. An easy solution would be to make the first 4 bytes of the transmission be a network order unsigned integer which says how long the file is going to be. This also allows the receiving side to allocate sufficient memory in one go.

Of course, if you plan to close the connection after doing the one send, the recipient can just wait for EOF. This makes memory allocation for the recipient slightly harder though (has to use realloc())
 
Old 02-12-2006, 08:35 AM   #3
crapodino
Member
 
Registered: Aug 2005
Distribution: SuSE 10
Posts: 72

Rep: Reputation: 15
i have the same problem. I send the files in blocks. And then i send the bytes that were not send. Does tcp/ip let you send files of big size? i heard that it doesn't let you send more than 64KB in one send(). is this true ? or i can send all files no matter which sizes they have ?
 
Old 02-12-2006, 09:02 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
If you are sending via tcp then theres no prob of packets being out of order / lost, so your recv func wants to look something like this and the send similar.
Every message that you send attach a header like aluser suggested.
Code:
//read in the header which will give you the total size of the message
//...........
	//is there more to receive or was the header the only thing?
	if( p_header->size > sizeof(Msg_header) )
	{		
		int remaining = p_header->size - bytes_recv;//whats the bytes remaining
		
		while(remaining != 0)//loop until we get all the data		
		{				
			//cast required for winblows
			this_recv = recv(m_socket,reinterpret_cast<char*>(buf + bytes_recv),remaining,0);			
			if(this_recv <= 0)//there was an error
			{
				//this could be would block determine if we carry on
				//.........
				//if we are not carrying on return an error
				return this_recv; 
			}
			else
                        {
			         remaining -= this_recv;	
			         bytes_recv += this_recv;
                        }	
		}	
	}

	return bytes_recv;
this just loops until you get the correct amount of bytes or there was an error which means you want to exit ie. connection reset.

Last edited by dmail; 02-12-2006 at 09:19 AM.
 
  


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
cant send a whole struct with sockets (c code) alaios Programming 9 04-12-2020 10:52 AM
high recv-q and send-q and collision rate sarah_haff Linux - Networking 2 08-19-2008 07:49 AM
Post fix can recv mail from outside but cant send. luvonmik Linux - Networking 1 03-30-2004 12:07 PM
Cable modem send/recv constantly /home/kyle Linux - Software 2 03-12-2004 01:09 PM
Red Hat 9 - How do I change TCP send/recv windows? oswald21 Linux - Networking 1 07-16-2003 06:21 PM

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

All times are GMT -5. The time now is 08:25 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