Sending requests with packets

This is a discussion on Sending requests with packets within the Networking/Device Communication forums, part of the General Programming Boards category; Simple (stupid) question: I just have been tinkering around with sockets, and I want to know what message I have ...

  1. #1
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498

    Sending requests with packets

    Simple (stupid) question:

    I just have been tinkering around with sockets, and I want to know what message I have tp send to request a webpage using send(). For example, I have a php module that inputs data like so:
    http://example.com/mymodule.php?name...ress=123street
    I don't really need to get the webpage back--all it really needs to do is send the request, so that the php module can input the data into a mysql database.

    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I think submiting information to a php script is done with the GET method.
    Try this.
    Code:
    HOST example.com (\r\n)
    GET /mymodule.php?name=joe&address=123street http/1.1(\r\n)
    (\r\n)
    (\r\n)

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    After connecting to my.domain.com:
    Code:
    GET /mypage.php?name=me&style=cool&score=1000 HTTP/1.1\r\n
    Host: my.domain.com\r\n
    Connection: Close\r\n
    \r\n
    You might be able to then close the connection entirely... you might also want to just read off the return from the server to a meaningless buffer. (Shouldn't be much, since it's your script...)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Use ethereal to capture the network activity when you do this with a browser.

    Then compare that with what you get from your program.
    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.

  5. #5
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Thanks, I just realized that I might want to also get data back (just the html) from a webpage. What do I do there?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Don't close the connection and call recv untill you've got all the data.

  7. #7
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    OK, I've got a slight problem here.

    Another thing to watch out for when calling bind(): don't go underboard with your port numbers. All ports below 1024 are RESERVED (unless you're the superuser)! You can have any port number above that, right up to 65535 (provided they aren't already being used by another program.)
    This was taken from an online sock tutorial, which I have been using. Now I'm a bit confused. I know that the HTTP port is 80, and that there are lots of ports that are below 1024! When I try calling bind() on port 80, it fails, just like this said it would. What do I do?
    Last edited by joeprogrammer; 05-18-2006 at 05:38 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you're trying to connect to a HTTP server on port 80 you don't need to call bind. You need to fill out a SOCKADDR_IN structure (don't forget to call htons() on the port number).

  9. #9
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Yeah, that was a stupid question of mine. The only reason you would want to bind the http port is if you're creating a web server, in which case you would have super-user access anyway.

    Yet another question: It doesn't seem to be working. Could it be that I am using stream sockets when I really should be using datagrams?
    Last edited by joeprogrammer; 05-18-2006 at 06:54 PM.

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    No, stream sockets should work. Posting some code would allow better help. Getting a webpage is essentially:
    1) Connect to the server.
    2) Send your HTTP request & headers (what I posted above... this can get more complicated. Look up the HTTP specs / use Ethereal to do capturing and see for yourself)
    3) Wait & get the response.

    The double newline signals the end of the headers/request. Then you just read off everything the server sends you. The result will also contain headers - these are separated from the content with a double newline as well.

    Another thing to watch out for (but unlikely to be your problem now) is transfer-enconding... the response will have "Transfer-Encoding: --something--" in it... some pages are compressed, some are "chunked" (I think Google's page is).

    At any rate: Where is your program failing? Connecting, sending, receiving? Do you get odd results, no results, or does your program crash? As long as we (the readers) can't answer these questions, we probably can't offer much help. Code snippits, and your results, will aid greatly.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  11. #11
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    OK, sorry about that. I figured out on my own (sigh) that yes, there does have to be a double newline for the server to return information. That solved my problem. Another thing is: when I call recv(), it works, but I notice that it's in synchronous mode. This is not particularly useful, as I do not always know when a packet is going to be sent to the program. There's a way, looking at all the programs that do this.

    Another (unrelated) question: I notice that games like Halo can get a list of current servers even on a LAN where there is no central server that keeps track of all the other servers. How does Halo and other games do this? (there's probably some kernal call or something, but I'm too lazy to look it up)

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by joeprogrammer
    Another thing is: when I call recv(), it works, but I notice that it's in synchronous mode. This is not particularly useful, as I do not always know when a packet is going to be sent to the program. There's a way, looking at all the programs that do this.
    You can change a socket to non-blocking (asynchronous) mode with the ioctlsocket() function. This function can also be used to tell you the amount of data waiting to be recieved and then you can recv() that amount so you're not waiting around doing nothing.

    Another (unrelated) question: I notice that games like Halo can get a list of current servers even on a LAN where there is no central server that keeps track of all the other servers. How does Halo and other games do this? (there's probably some kernal call or something, but I'm too lazy to look it up)
    This is done by sending a UDP packet the to the broadcast address which means it is readable by every host on the LAN. Hosts with the program installed will respond to the program that sent it. I believe the broadcast address is xxx.xxx.xxx.255 (The xs is your subnet address) I havn't ever done this though.
    Last edited by Quantum1024; 05-20-2006 at 10:08 PM.

  13. #13
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    You can change a socket to non-blocking (asynchronous) mode with the ioctlsocket() function.
    Hey, that's a Windows-only function! After a little bit of googling though, I found the open-source version, ioctl:
    http://www.opengroup.org/onlinepubs/...ons/ioctl.html

    Anyway, thanks for all the help, I think I can manage socket programming now!

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Oh sorry I thought it was the same on both platforms.

  15. #15
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I believe a simply #define in a header file can cancel portability problems. For the purpose of making sockets non-blocking, I think the differences between ioctl and ioctlsocket are insignificant. Haven't tested this much though.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending and recieveing packets with dynamic size.
    By c++.prog.newbie in forum Game Programming
    Replies: 1
    Last Post: 05-31-2006, 01:06 PM
  2. Writing all HTTP requests from a website to a log file
    By goomyman in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 09:18 AM
  3. Sending Raw Packets?
    By Coder87C in forum Networking/Device Communication
    Replies: 7
    Last Post: 12-03-2003, 12:27 PM
  4. Sending data packets
    By neandrake in forum Networking/Device Communication
    Replies: 6
    Last Post: 11-26-2003, 12:41 PM
  5. Sending raw packets.
    By Denethor2000 in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2002, 02:08 PM

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