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
  #1  
Old 08-29-2006
Registered User
 

Join Date: Aug 2006
Posts: 11
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
Reply With Quote
Forum Sponsor
  #2  
Old 08-30-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
If you are using sockets send() and recv() offer much greater control over your data transmission
Reply With Quote
  #3  
Old 08-30-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 974
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.
Reply With Quote
  #4  
Old 08-30-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
Try them and if the error disappears then the reason exists
Reply With Quote
  #5  
Old 08-30-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 974
How so? Even if the error goes away, the program still lacks the error-checking to handle them. It should fail, not crash.
Reply With Quote
  #6  
Old 08-30-2006
Registered User
 

Join Date: Aug 2006
Posts: 11
It's really a frustrating problem


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
Reply With Quote
  #7  
Old 08-30-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
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...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:33 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