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.
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
-- 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:
Also, this code:
...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:
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..
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)
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)
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)
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)
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)
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)
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)
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)