Networking with GIO
These examples demonstrate the networking functionality that has recently landed in GLib as official part of GIO.
You will need GLib version >= 2.22 (Check with pkg-config glib-2.0 --modversion), Libgee version >= 0.6.4 (pkg-config gee-1.0 --modversion) and Vala >= 0.11.0
Simple Webserver Example
/**
* Based on an example of Jezra Lickter
* http://hoof.jezra.net/snip/nV
*/
uses
GLib
Gee
init
var ws = new WebServer()
ws.run()
struct Request
full_request : string
path : string
query : string
struct Response
status_code : string
content_type : string
data : string
class WebServer
def run()
tss : ThreadedSocketService = new ThreadedSocketService(100)
ia : InetAddress = new InetAddress.any(SocketFamily.IPV4)
isaddr : InetSocketAddress = new InetSocketAddress(ia, 8001)
try
tss.add_address(isaddr, SocketType.STREAM, SocketProtocol.TCP, null, null);
except e : Error
stderr.printf("%s\n", e.message)
return
// add connection handler
tss.run.connect( connection_handler )
ml : MainLoop = new MainLoop()
tss.start()
stdout.printf("Serving on port 8001\n")
ml.run()
def connection_handler ( conn : SocketConnection ) : bool
first_line : string = ""
size : size_t = 0;
request : Request = Request()
dis : DataInputStream = new DataInputStream (conn.input_stream)
dos : DataOutputStream = new DataOutputStream (conn.output_stream)
try
first_line = dis.read_line(out size)
// here you could analyze request information
var parts = first_line.split(" ");
if parts.length > 1 do request.full_request = parts[1]
except e : Error
stderr.printf("%s\n", e.message)
response : Response = Response()
response.status_code = "HTTP/1.1 200 OK\n"
response.content_type = "text/html"
response.data = "<html><body><h1>Hello, Genie webserver alive</h1></body></html>"
serve_response ( response, dos )
return false
def serve_response ( response : Response, dos : DataOutputStream )
try
dos.put_string (response.status_code)
dos.put_string ("Server: Genie Socket\n")
dos.put_string("Content-Type: %s\n".printf(response.content_type))
dos.put_string("Content-Length: %d\n".printf(response.data.length))
dos.put_string("\n");//this is the end of the return headers
dos.put_string(response.data)
except e : Error
stderr.printf("%s\n", e.message)
Compile and Run
$ valac --pkg gio-2.0 --pkg gee-1.0 webserver.gs $ ./webserver