The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read and write from a file rinku Shell Programming and Scripting 2 01-10-2008 10:22 PM
read/write socket error gio High Level Programming 2 04-16-2007 08:52 PM
sed to read and write to very same file 435 Gavea Shell Programming and Scripting 5 06-29-2006 08:04 PM
popening for read and write szzz High Level Programming 1 11-18-2003 09:05 AM
read, write & STDOUT_FILENO.... M3xican High Level Programming 2 07-17-2002 01:41 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #8  
Old 08-30-2006
Registered User
 

Join Date: Aug 2006
Posts: 11
Thanku blowtorch,

Ye, I think that's a way. But why cannot we use 2 write() in a row? why that could crash read() on client? Do anybody know this?


Chees,

Elton
Reply With Quote
Forum Sponsor
  #9  
Old 08-30-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
I do not mean to say that two writes in a row would crash a client. I'm just trying to say that this would be a better way. If there was any error in the read on the client side, the server would be notified of this and would resend the buffer.

Regarding the client side crash, you need to post the code, or atleast the part where the crash is occurring, so that someone could take a look at it. Also, if posting code, please remember to use the code tags. Not using those makes it very difficult to read the code.
Reply With Quote
  #10  
Old 08-30-2006
Registered User
 

Join Date: Aug 2006
Posts: 11
Hi guys,

This my problem prone code:

On server side:
.
.
.
Code:
void reply(int s, SizedBuffer p, char *type)
{
	SizedBuffer sBuf;
    char *r;
    char l[10], whole[10];
    int dataStart;

    sprintf(l,"%d",p.size);

    dataStart = strlen(header1) + strlen(l) + strlen(header2) + strlen(type);

    r = (char *) calloc(dataStart + p.size + 1, sizeof(char));

    strcat(r,header1);
    strcat(r,l);
    strcat(r,header2);
    strcat(r,type);
    bcopy(p.buffer, r+dataStart, p.size);

        
    /*Write file size to client.*/
    sprintf(whole, "%d", dataStart + p.size);
    whole[strlen(whole)] = '\0';

    write(s, whole, strlen(whole));

   /*problem happens here...*/
    write(s, r, dataStart + p.size);

    free(r);
}
.
.
On client side:
.
.
Code:
while(1)
   {
      memset(buf,'\0', BUFFER_SIZE);
      if((newSock = acceptConnection(sp->sock)) == 0)
         exit(-1);
      nread = Read(newSock, buf, BUFFER_SIZE);
      
      if((c = makeClientSocket((sp->count), ipAddr)) == 0)
         exit(-1);

      cSock = c->socket;
      
      Write(cSock, buf, BUFFER_SIZE);
      memset(buf,'\0', BUFFER_SIZE);
      
      Read(cSock, buf, 10);
      fLen = atoi(buf);
      printf("fLen is :%d\n", fLen);
      
	  Write(cSock, buf, 10);
      
      char * file = malloc((fLen)*sizeof(char));
     
     /*problem happens here: I lose some bytes of the buffer.*/
      recv(cSock, file, fLen, MSG_WAITALL);
      
      printf("==> %s\n", file);
      
      write(newSock, file, fLen);
      close(newSock);
	  free(file);
      
   }
.
.

hope you guys can figure it out

REGARDS,

Elton
Reply With Quote
  #11  
Old 08-30-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
That code is missing a great deal, like, what header1, header2, whole, and type is.
Reply With Quote
  #12  
Old 08-31-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
Code:
      r = (char *) calloc(dataStart + p.size + 1, sizeof(char));
      r[0]='\0';
      strcat(r,header1);
The bit in red is essential for your program to reliably work. When you allocate memory, it's not necessarily blank memory -- there could be anything in it. Meaning, when you use strcat, it tries to append it to the end of what's already in there instead of putting it at the beginning.

A more proper way to do this would be
Code:
strcpy(r,header1);
-- which overwrites whatever's in r with no regard for what was in it before, and leaves it's own valid NULL terminator.

Or perhaps even get rid of that mess of strcats -- wasteful, it re-measures the length of the string every time -- with a sprintf:
Code:
sprintf(r,"%s%s%s%s",header1,l,header2,type);
Also, this code:
Code:
sprintf(whole, "%d", dataStart + p.size);
    whole[strlen(whole)] = '\0';
...hints at a deep misunderstanding of strings in C. strlen won't work unless the string already has a null terminator! The null terminator is what it uses to measure the length of the string; and any standard C string functions, with the exception of strncpy, will always add the null for you.

Also, this:
Code:
Read(cSock, buf, 10);
assumes that the length string is 10 bytes long. This also hints at a deep misunderstanding of strings in C... the buffer the server held the string in was 10 bytes, but the actual string it held, and the actual data it sent, could be anything between zero and nine bytes. So in reading the 10 bytes, you're reading past the length string into your binary data.

My suggestion? Throw all that code out, learn how strings work in C, start over. It's not just a program bug, it's a design that can't work.

Last edited by Corona688; 08-31-2006 at 12:02 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0