Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I need to make a daemon which listens to port 81 for messages like localhost/message?command=move&dir=left So far I made a daemon which serves as a simple stream server: I ...
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    3

    [SOLVED] Handle query strings using socket programming

    Hi, I need to make a daemon which listens to port 81 for messages like localhost/message?command=move&dir=left
    So far I made a daemon which serves as a simple stream server: I set up a socket to listen to a non-reserved port (like 9999), but I don't know how to read the query strings.

    Linux distro: Kubuntu 9.04
    Language: C

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    man recv

    File: *manpages*, Node: recv, Up: (dir)

    RECV(2) Linux Programmer's Manual RECV(2)



    NAME
    recv, recvfrom, recvmsg - receive a message from a socket

    SYNOPSIS
    #include <sys/types.h>
    #include <sys/socket.h>

    ssize_t recv(int sockfd, void *buf, size_t len, int flags);

    ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
    struct sockaddr *src_addr, socklen_t *addrlen);

    ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);

    for complete details open a terminal and type info revc or man recv
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    5
    Are you having trouble with knowing where the query strings get passed to you or how to read data from the socket?

  4. #4
    Just Joined!
    Join Date
    Dec 2009
    Posts
    3
    Quote Originally Posted by jrop View Post
    Are you having trouble with knowing where the query strings get passed to you or how to read data from the socket?
    First of all, thanks for the replies.

    I don't know where the query strings get passed. I tried with recv, but didn't manage to get something from it.

  5. #5
    Just Joined!
    Join Date
    Jun 2009
    Posts
    5
    They get passed by the browser in the Http Request:

    This is sort of a minimal HTTP Request:
    <start HTTP Request>
    GET [pathToPage]?[queryString] HTTP/1.1
    Host: [yourhost]
    [other headers here...]

    <end HTTP Request>

    Notice that the header region ends with two newlines.

    Hope that helps.

  6. #6
    Just Joined!
    Join Date
    Jun 2009
    Posts
    5
    Also, you might try getting a packet sniffer just to be able to see what "real" requests and responses look like. (Wireshark seems to be a good one)

  7. #7
    Just Joined!
    Join Date
    Dec 2009
    Posts
    3
    I finally managed to get the query string. It worked with recv. I wasn't reading the buffer correctly because I didn't know the format. Thank you for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...