dikidera
dikidera

Reputation: 2043

Server in C. How do i do it with query strings?

So, i am assuming that i will need to use sockets(i am a newbie to C).

The program will be for Windows(in pure C). And i shall be using these examples

http://cs.baylor.edu/~donahoo/practical/CSockets/winsock.html

My question is, instead of the client program connecting via TCP, i want the server to accept connections via a web browser i.e via HTTP.

So if the server program is running you type http://yourip:port/?gettemps and the server responds, but how do i do it?

As you might have guessed, this program will be for monitoring temps, remotely, via a web browser. But not for the CPU, for the GPU using AMD's ADL library(so yeah, only AMD cards).

Upvotes: 1

Views: 703

Answers (4)

shobhonk
shobhonk

Reputation: 656

Web browsers sends http get request to the server via tcp. If you are writing a web server from scratch than, you will need to parse data from web browser. http get request are string like for example GET /images/logo.png HTTP/1.1. So tokenize that string as it comes through tcp and get the command.

As you received your commands to the server call appropriate functions to handle your request. Here is an great example of simple http server. You might want to make server multi-threaded as you may have multiple simultaneous users.

Upvotes: 2

Karel Petranek
Karel Petranek

Reputation: 15154

HTTP is quite a big standard, you might want to use some library such as libcurl to handle the details for you.

If you decide to code it yourself, HTTP is running over TCP so you first need to open a TCP socket at the standard HTTP port 80. Then simply listen on the socket and parse the incoming HTTP data - a great summary is given here: http://www.jmarshall.com/easy/http/.

Upvotes: 2

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84179

The simplest option that is supported by most web servers is CGI - Common Gateway Interface.

Microsoft, of cource, has their own way of running web apps - ISAPI.

Upvotes: 4

blankabout
blankabout

Reputation: 2627

If you have already set up your web server to run the app on the appropriate port you can use getenv("QUERY_STRING") to access the web equivalent of command line parameters.

It would be better to call your program directly rather than just using the server to access a single default program as your example does, thus you could use http://yourip:port/yourprogram?cmd=gettemps. In this example getenv("QUERY_STRING") would return 'cmd=gettemps'.

Upvotes: 0

Related Questions