fseek() on socket

This is a discussion on fseek() on socket within the Networking/Device Communication forums, part of the General Programming Boards category; Hello, Is there a way to fseek() on a socket? Or at least someway that doesn't require a linear search? ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    fseek() on socket

    Hello,
    Is there a way to fseek() on a socket? Or at least someway that doesn't require a linear search?

    Thanks,
    Yasir

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Short answer is no.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Is that a "no" to both of my questions?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Well, I'll tell you why I want to fseek() into a socket.

    There is a blog, and it has become quite popular. It has over 3000 posts. The problem with this is that each time you want to access the blog, you have to download 3 MB worth of data. This is made worse by the fact the HTTP header does not send back the date last modified field, so nothing is cached. Well, I want to store each post seperately in a database, and display, say, 20 entries on a page at once. There will be links to different page number, like a regular message board. The problem here is that I don't have access to the database or whatever they are using to store the data. So I plan on having a link that says "last page" that when clicked will go to the last 20 posts. I plan on doing this by storing a file pointer up to the point the last entry into the blog. When "last page" is clicked, I will use the file pointer to look ahead if there is a new post, and if so, add the new post to the database and increment the file pointer. The file pointer itself is stored within a database.

    This way I am also doing the website administrator a favor by reducing his bandwidth (even though that is not my intention).

    I can post the link to the blog, but I fear that I may be moderated, because the blog itself isn't moderated and people with ill intentions have posted obscene stuff (but it is only a fraction of a fraction of fraction of the total posts).

    Of course, I would appreciate a better solution than storing a file pointer.

    Thanks,
    Yasir
    Last edited by Yasir_Malik; 05-21-2005 at 05:19 PM.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The way that HTTP typically works is that a client opens a connection and requests a document, the server sends the response and closes the connection. It is very unlikely you will be able to keep the connection permanently open waiting for new entiries. I suggest that the website administrator install one of the many free blog software packages. Or relocate most of the entries to archive pages. If the entries are posted newest first you could request the blog page and then just close the connection after any new entries.
    Last edited by anonytmouse; 05-22-2005 at 04:55 AM. Reason: Spelling.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    RFC2616 Hypertext Transfer Protocol -- HTTP/1.1
    Get it at http://www.rfc-editor.org
    I suggest you read it thoroughly before you do much more.

    If your blog HTML simply grows in size (appended to only), then I suggest you look into the "range" feature of HTML requests.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Quote Originally Posted by anonytmouse
    The way that HTTP typically works is that a client opens a connection and requests a document, the server sends the response and closes the connection. It is very unlikely you will be able to keep the connection permanently open waiting for new entiries. I suggest that the website administrator install one of the many free blog software packages. Or relocate most of the entries to archive pages. If the entries are posted newest first you could request the blog page and then just close the connection after any new entries.
    No, I didn't intend to keep the socket open with a call to select() or whatever, but I intended to keep the data byte offset (the file pointer) around, so each time "last page" would be clicked, I would reopen the connection to the site and use the file pointer to move to the last entry I read.

    Quote Originally Posted by salem
    If your blog HTML simply grows in size (appended to only), then I suggest you look into the "range" feature of HTML requests.
    This sounds promising. I'll look into this. Thanks.
    Last edited by Yasir_Malik; 05-22-2005 at 06:56 AM.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    By the way, here's the blog:
    http://brokensaints.com/blog/?p=48

    It is probably the biggest blog you'll ever see.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    When I send the following message, I get the entire the page back:
    Code:
    GET /stuff.html HTTP/1.1\r\n
    Host: localhost\r\n
    Accept: text/html\r\n
    Content-Range: bytes 0-2/*\r\n
    Connection: Close\r\n\r\n
    Sending
    Code:
    GET /stuff.html HTTP/1.1\r\n
    Host: localhost\r\n
    Accept: text/html\r\n
    Content-Range: bytes 3000000-3000002/*\r\n
    Connection: Close\r\n\r\n
    also returns the entire page. I'm running Apache 2.0.52.

    Thanks,
    Yasir

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    My guess would be the * is doing something.

    Or you HTTP server either isn't 1.1 compatible (seems unlikely), or isn't set up to deal with ranges.

    Have you spied the connection request/response with say ethereal?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Quote Originally Posted by Salem
    My guess would be the * is doing something.
    According to the RFC, you are allowed to use * if you don't know the size of the data. Nevertheless, I used 3056950, which is the size of the data, but still got the same result.

    Or you HTTP server either isn't 1.1 compatible (seems unlikely), or isn't set up to deal with ranges.
    Yeah, I except Apache 2 to be fully HTTP 1.1 compliant.

    Have you spied the connection request/response with say ethereal?
    I have dial up, and I don't want to spend time downloading the package (it's almost 6 MB).
    However, this is the response I got from the server:
    Code:
    HTTP/1.1 200 OK
    Date: Sun, 22 May 2005 19:44:37 GMT
    Server: Apache/2.0.52 (Unix) PHP/5.0.2
    Last-Modified: Sun, 22 May 2005 17:12:59 GMT
    ETag: "6b17-2ea536-11265cc0"
    Accept-Ranges: bytes
    Content-Length: 3056950
    Connection: close
    Content-Type: text/html; charset=ISO-8859-1
    Since I am not using a browser, the request is what I posted in my last post because I am sending everything through a socket I created in a PHP program.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well 6MB is well worth it IMO - given the amount of help it's likely to be in figuring out all sorts of network coding problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    My development box is NetBSD and it is not networked. My Windows box is networked, but it's dialup. Just to install Ethereal, I need the packages metioned here:
    ftp://ftp.netbsd.org/pub/NetBSD/NetB...al/README.html

    I probably have some of them, and I might not even have the right version. I'm probably going to be in dependency hell. Downloading everything will be definetly more than 6 MB. The CS server doesn't have Ethereal or else I could ssh in and run Ethereal. The sysadmin is a lazy and is obsessed with security (he makes us run the CGI version of PHP when there's entire corporations that run regular PHP), and probably won't install it.

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    But what more do you need other than the response from the server (I'm communicating with localhost)?

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > My development box is NetBSD and it is not networked
    So how do you develop a networking program, if there's nothing to talk to?

    So long as the machines are on the same local subnet, you can run ethereal on any of them, and see all local traffic no matter which machine originated it.

    You don't have to run ethereal on the machine actually sending/receiving the traffic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 09:33 AM
  4. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 12:38 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21