Fedora Linux Support Community & Resources Center
  #1  
Old 23rd February 2007, 07:56 PM
pepribal's Avatar
pepribal Offline
Registered User
 
Join Date: Sep 2005
Posts: 98
Another sockets problem

I'm getting a full file from a server, doing:

flags=fcntl(socknum,F_GETFL,NULL);
flags|=O_NONBLOCK;
fcntl(socknum,F_SETFL,flags);

while(read(socknum,&buffer,1)>0)
cout << (int)buffer << " - " << buffer << endl;

If I don't set the O_NONBLOCK flag this works fine, except that after the last character has been read, the next read() blocks, and I want it to exit the while(). But if I set the nonblocking flag, then it doesn't read a single character.

What am I missing?

Thanks.
Reply With Quote
  #2  
Old 23rd February 2007, 09:47 PM
Bookar Offline
Registered User
 
Join Date: Feb 2007
Posts: 12
1)The problem here is that you don't mark end of data that should be read. To tell the client program that no more data will be sent through this socket you should call close(socknum) function on the server side which will close connection. After connection is closed next read() call you make on the client side will return 0 so you can get out of loop. (didn't check this but it will probably work)

If you want some way to tell the client that server sent whole file but you don't want to close connection it is more complicated - you could for example send size of the file you want to send first, and then send contents of the file.
Another method involves using one special character (for example '^') as a marker of end of transmission/file/package (whatever you need). But the major drawback of this method is that this character cannot be present in transmitted data.

2) When you set socket to nonblocking it will never wait for incoming data. It will read only data which is available at certain moment. If no data is available read function will return -1 and errno global variable will contain EAGAIN value (didn't check it either).
Reply With Quote
  #3  
Old 24th February 2007, 06:43 AM
pepribal's Avatar
pepribal Offline
Registered User
 
Join Date: Sep 2005
Posts: 98
The problem is that I have no control over the server. It is a simple text file accessed via HTTP 1.1 protocol. In this case, how should this be handled?
Reply With Quote
  #4  
Old 24th February 2007, 06:52 AM
pepribal's Avatar
pepribal Offline
Registered User
 
Join Date: Sep 2005
Posts: 98
Solved! I've just seen the length info the HTTP 1.1 protocol sends before the actual file.
Reply With Quote
  #5  
Old 26th February 2007, 10:45 AM
ibbo's Avatar
ibbo Offline
Registered User
 
Join Date: Jun 2005
Location: Leeds
Posts: 1,264
pepribal

What is it your attempting to do?

Ibbo
__________________
A Hangover Lasts A Day, But Our Drunken Memories Last A Lifetime
--
Linux user #349545
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCfdjyzXscddzQvlhBedAcD7qfKmHo==zx0H
Reply With Quote
  #6  
Old 26th February 2007, 07:08 PM
pepribal's Avatar
pepribal Offline
Registered User
 
Join Date: Sep 2005
Posts: 98
ibbo, just retrieving a plain text file from a server via HTTP protocol. It's solved anyway. Thanks!
Reply With Quote
Reply

Tags
problem, sockets

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mtr: unable to get raw sockets DesiSniper Servers & Networking 4 20th February 2012 08:24 PM
Divert sockets arrowheart Servers & Networking 0 26th October 2008 05:52 AM
sockets and strings fizy Programming & Packaging 4 10th May 2008 10:40 PM
sockets are hanging nimbius Servers & Networking 12 19th May 2007 12:37 PM
upgrade php with -sockets sadouk Using Fedora 0 5th March 2005 07:38 PM


Current GMT-time: 21:43 (Sunday, 19-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat