Problem with read & write


 
Thread Tools Search this Thread
Top Forums Programming Problem with read & write
# 1  
Old 08-30-2006
Problem with read & write

Hello mates:

I met problem with using read() & write(). I m trying to use read twice on client first time is the size of buffer, 2nd time is the buffer. I think I have to, coz I dnot know file size. So, I write twice on server as well -- 1st, filesize; 2nd, buffer.

The problem is, sometimes, I cannot get anything at 2nd time. The file I got is empty, nothing. However, if I put a sleep(1) before the 2nd write() on server, I can read( but this sometimes may cause a segmentation fault)...

i.e.:
Client Server

read() 1st write() 1st <-----both OK

(if I put a sleep(1)here, it's OK)

read() 2d write() 2nd <-----problem


Does anybody know the reason?

BEST REGARDS,

Elton
# 2  
Old 08-30-2006
If you are using sockets send() and recv() offer much greater control over your data transmission
# 3  
Old 08-30-2006
Quote:
Originally Posted by Hitori
If you are using sockets send() and recv() offer much greater control over your data transmission
But ordinary read and write should still work, yes? No reason for them to segfault without an error in the program. If my hunch is correct send() and recv() could suffer the same issues...

Check to make sure you're recieving the number of bytes you intended. I think you might be getting crazy numbers for filesize, which could cause the crash.
# 4  
Old 08-30-2006
Try them and if the error disappears then the reason exists Smilie
# 5  
Old 08-30-2006
How so? Even if the error goes away, the program still lacks the error-checking to handle them. It should fail, not crash.
# 6  
Old 08-30-2006
It's really a frustrating problem Smilie


I m trying to use 2 write() in a row, first write() is used to send file size, 2nd send the file. On client side, I use malloc() for the buf size and free later.

Current situation is if I put sth (like sleep(1) or a read()) btw 2 write() , they both work fine. Othrewise, I lose some bytes on client side which is from the 2nd write(). That makes the browser cannnot recognize the HTTP header....

REGARDS,

ELton
# 7  
Old 08-30-2006
OK, I suggest that you set up a proper handshake. Currently your scenario is like this:
server -- write size
client -- read size
server -- write data_buffer
client -- read data_buffer

Change that to:
server -- write size
client -- read size
client -- write ACK
server -- read ACK
server -- write data_buffer
client -- read data_buffer
client -- write ACK

and so on....
So it will never be that your server is continuously writing to a socket that the client is not able to read cause it crashed...
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