Following is my client code....I am receiving data of equal size of JPG file... so if anyone can help me out with reading buffer properly and writing it in to jpg file at client side
================================================

do
{
memset(buffer,0x0,BUFFER_SIZE); // init line
rc = read(sd, buffer, BUFFER_SIZE);
if( rc > 0)
{
printf("%s",buffer);
size +=rc;
}
// printf(" %d ",size);
}while(rc>0);

pFile = fopen ( "new.jpg" , "wb" );
fwrite (buffer, 1 , size , pFile );
fclose (pFile);


printf("\n Total received response bytes: %d\n",size);

// return
close(sd);

Such flat, lifeless code. Why? No code tags! ;)

You're reading over and over to the same part of the buffer (and zeroing it before every read). Move the zeroing (if necessary) outside the loop, and either move along the buffer (read into buffer+size) or do the file operations on a buffer by buffer basis, as below.

memset(buffer,0x0,BUFFER_SIZE); // init line
pFile = fopen ( "new.jpg" , "wb" );

for(;;)
{
  rc = read (sd, buffer, BUFFER_SIZE);
  if (rc <= 0) break;
  size += rc;
  fwrite (buffer, 1 , rc, pFile );
}

fclose (pFile);
printf("\n Total received response bytes: %d\n",size);

Thanks a lot nucleon....my client is working properly.. but I am facing one issue with Server after sending data it hangs and i can not make another request....following is my part of server code which sends data...
====================

if((strcmp(tmpstr[1],"jpg")==0))
		{    
		 FILE *pFile;
		  long lSize;
		  char *buf;
		  size_t result;

		  strcat(sfileorg,".jpg");
		  //printf("File: - %s \n",sfileorg);		 

		  pFile = fopen(sfileorg,"r");
		  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}		 

		  // obtain file size:
		  fseek(pFile , 0 , SEEK_END);
		  lSize = ftell (pFile);
		  printf("size of file : %ld ", lSize);
		  rewind(pFile);

		  
		  // allocate memory to contain the whole file:
		  buf = (char*) malloc (sizeof(char)*lSize);
		  if (buf == NULL) {fputs ("Memory error",stderr); exit (2);}

		  // copy the file into the buffer:
		  result = fread (buf,1,lSize,pFile);
		  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

		  /* the whole file is now loaded in the memory buffer. */

		  // terminate
		  fclose (pFile);
		  
		  printf("size of file : %ld ", sizeof(buf));
			
		bytes_sent = send(newSd,buf,lSize,0);	
			

		close(newSd);
		} // binary file readin and sending ends here

So you're saying that this piece of code will send a jpg file the first time it executes but not the second time or after that? What's the value of bytes_sent when the error occurs? If it is -1, what is the value of errno?

Notes:

You should open binary files with the "b" attribute since this makes a difference on some systems. At a minimum, your program will be more portable.

You may as well just return the same number in exit() for the different errors. If you actually meant these errors to be tested by another program, then you would give them all symbolic names and put that in a header for the other program to use. The most common return value seems to be -1 (all bits set in 2's complement).

sizeof(char) is always 1 by definition since sizeof returns its result in multiples of char, so you don't have to multiply by sizeof(char) when allocating space for chars.

It is working now. I was accessing an integer again and again without making it 0 for the next request. so I added one line P=0 and it started working.

No need to read in "b" it can read in "r" also and can send to client..for me its working

Thanks for your help.....

This question has already been answered. Start a new discussion instead.