Problem with read & write


 
Thread Tools Search this Thread
Top Forums Programming Problem with read & write
# 8  
Old 08-30-2006
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
# 9  
Old 08-30-2006
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.
# 10  
Old 08-31-2006
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 Smilie

REGARDS,

Elton
# 11  
Old 08-31-2006
That code is missing a great deal, like, what header1, header2, whole, and type is.
# 12  
Old 08-31-2006
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 04:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

MAN and read & write function

How to use MAN to find information about read() and write() function ? The command "man read" show some rubbish, for example "man open" show great information about function I need. (2 Replies)
Discussion started by: bbqtoss
2 Replies

2. Programming

help: problem with sockets write/read

I am trying to make a server and client, the client will choose between some options and the server will react accordingly. After a some reads and writes that work the server needs to read from client an INT i use this: read(newSd,&k,sizeof(int));But even if all the other times there was no... (1 Reply)
Discussion started by: theSling
1 Replies

3. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

4. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

5. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

6. UNIX for Dummies Questions & Answers

About read,write & execute permissons of a directory

Hi all, I want to know differences between read,write & execute permissons given to directory. Thanx in advance. (6 Replies)
Discussion started by: vishwasrao
6 Replies

7. UNIX for Dummies Questions & Answers

user & group read/write access question

folks; I created a new users on my SUSE box and i need to give this user/group a read write access to one specific folder. here's the details: - I created new user "funny" under group "users". - I need to give this user "funny" a read/write access to another directory that is owned by "root".... (3 Replies)
Discussion started by: Katkota
3 Replies

8. Shell Programming and Scripting

File read & execute problem

Hi folks, Need your help. I am writing a KSH script to read a few commands from a file & execute. I am using the following code to read the file line by line & excute each command. When I am printing each line I see it is printing properly but while excuting, the particular "ps" command... (5 Replies)
Discussion started by: tipsy
5 Replies

9. UNIX for Dummies Questions & Answers

How do i access (mount, read & write) a floppy disk from the console, not being root?

welll, the title quite explains what i want to do thanks for your time! (4 Replies)
Discussion started by: kfaday
4 Replies

10. Programming

read, write & STDOUT_FILENO....

hi guys, I'have a question 4 u. Why this code give me the right output (an integer on the stdout): read(fd,&mpid,sizeof(pid_t)); printf("%d\n",mpid); Instead this code give me only a blank line: read(fd,&mpid,sizeof(pid_t)); write(STDOUT_FILENO,&mpid,sizeof(pid_t)); ... (2 Replies)
Discussion started by: M3xican
2 Replies
Login or Register to Ask a Question