Next: , Previous: microhttpd-cb, Up: Top

5 Starting and stopping the server

— Function: void MHD_set_panic_func (MHD_PanicCallback cb, void *cls)

Set a handler for fatal errors.

function to call if MHD encounters a fatal internal error. If no handler was set explicitly, MHD will call abort.
closure argument for cb; the other arguments are the name of the source file, line number and a string describing the nature of the fatal error (which can be NULL)

— Function: struct MHD_Daemon * MHD_start_daemon (unsigned int flags, unsigned short port, MHD_AcceptPolicyCallback apc, void *apc_cls, MHD_AccessHandlerCallback dh, void *dh_cls, ...)

Start a webserver on the given port.

OR-ed combination of MHD_FLAG values;
port to bind to;
callback to call to check which clients will be allowed to connect; you can pass NULL in which case connections from any IP will be accepted;
extra argument to apc;
default handler for all URIs;
extra argument to dh.

Additional arguments are a list of options (type-value pairs, terminated with MHD_OPTION_END). It is mandatory to use MHD_OPTION_END as last argument, even when there are no additional arguments.

Return NULL on error, handle to daemon on success.

— Function: void MHD_stop_daemon (struct MHD_Daemon *daemon)

Shutdown an HTTP daemon.

— Function: int MHD_run (struct MHD_Daemon *daemon)

Run webserver operations (without blocking unless in client callbacks). This method should be called by clients in combination with MHD_get_fdset() if the client-controlled select-method is used.

Return MHD_YES on success, MHD_NO if this daemon was not started with the right options for this call.

— Function: void MHD_add_connection (struct MHD_Daemon *daemon, int client_socket, const struct sockaddr *addr, socklen_t addrlen)

Add another client connection to the set of connections managed by MHD. This API is usually not needed (since MHD will accept inbound connections on the server socket). Use this API in special cases, for example if your HTTP server is behind NAT and needs to connect out to the HTTP client.

The given client socket will be managed (and closed!) by MHD after this call and must no longer be used directly by the application afterwards.

daemon that manages the connection
socket to manage (MHD will expect to receive an HTTP request from this socket next).
IP address of the client
number of bytes in addr

This function will return MHD_YES on success, MHD_NO if this daemon could not handle the connection (i.e. malloc failed, etc). The socket will be closed in any case.