C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 08-08-2009, 09:57 AM   #1
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
Send a byte or whole text

Is there any preference when sending data as to send it byte by byte or sending
a whole buffer (the whole file) at once?

Is the latter faster than the former for example?
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 08-08-2009, 10:35 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
You can pass as many bytes as you like to send(), but you'd better pay attention to the return result.

If it's less than the amount you asked to be sent, then you need to call send() again, from the point where the previous attempt left off.

A bit like this.
Code:
size_t num_to_send;
char *buff;
while ( num_to_send ) {
  int num_sent = send( sock, buff, num_to_send, 0 );
  if ( num_sent > 0 ) {
    num_to_send -= num_sent;
    buff += num_sent;
  }
}
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 08-08-2009, 12:08 PM   #3
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 891
In addition to what Salem said: send as much as you have available to send, but obviously try to keep the size of what you must send small (if you have control over the protocol/its contents) - ie, a PNG or a GIF is better than a BMP.
recv as much as you can recv.
__________________
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
Cactus_Hugger is offline   Reply With Quote
Old 08-09-2009, 04:43 AM   #4
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
Thank you Salem and Cactus_Hugger!

I see you use a char *buff; in your code.

Does it matter as to which one of the 3 we use?

Code:
char *buffer;
char  buffer[256];
char *buffer = new char[256];
__________________
Using Code::Blocks,MingW with Windows.

Last edited by Ducky; 08-09-2009 at 05:31 AM.
Ducky is offline   Reply With Quote
Old 08-09-2009, 04:47 AM   #5
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
So long as it's allocated, no.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 08-22-2009, 02:46 AM   #6
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
May i ask whats the point of this line in your code Salem?
Code:
buff += num_sent;
I suppose its for decreasing the length of buff.

But if i do a test like this, i get completely random values for buf:

Code:
#include <iostream>

using namespace std;

int  main()
{
   int i = 0;
   char * buf = new char[i];
   while(i<15)
   {
        buf  += i;
        i++;
        cout << strlen(buf) <<"\n";
   }

   delete [] buf;

   return  0;
}
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 08-22-2009, 06:07 AM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> buff += num_sent;
If you've got a buffer of 100 bytes, and send 10, that means you've got 90 left (that's what the -= is for).
It also means that you need to try the next send from 10 bytes into the buffer (that's what the += is for).
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 08-22-2009, 07:04 AM   #8
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
Ah, ok thanks!
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
DirectX | Drawing text gavra Game Programming 4 06-08-2009 12:23 AM
How to use FTP? maxorator C++ Programming 8 11-04-2005 03:17 PM
Removing text between /* */ in a file 0rion C Programming 2 04-05-2004 08:54 AM
LISP (DrScheme) any one? Jeremy G A Brief History of Cprogramming.com 5 03-31-2004 12:52 PM
Ok, Structs, I need help I am not familiar with them incognito C++ Programming 7 06-29-2002 09:45 PM


All times are GMT -6. The time now is 02:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22