## Server

Server — TCP server object

## Synopsis

    #include <gnet.h>

                        GServer;
    void                (*GServerFunc)                      (GServer *server,
                                                             GConn *conn,
                                                             gpointer user_data);
    GServer*            gnet_server_new                     (const GInetAddr *iface,
                                                             gint port,
                                                             GServerFunc func,
                                                             gpointer user_data);
    void                gnet_server_delete                  (GServer *server);
    void                gnet_server_ref                     (GServer *server);
    void                gnet_server_unref                   (GServer *server);

## Description

A GServer represents a TCP server. GServer is a thin wrapper around a TCP server socket. To create a new GServer, call `gnet_server_new()`. One of the arguments is a callback function that is called with a new GConn whenever a new client connects. Remember to call `gnet_conn_set_callback()` on the new GConn to set the GConn callback. To delete a GServer, call `gnet_server_delete()`.

See also GConn and the echoserver-gserver example.

## Details

### GServer

    typedef struct {
      GInetAddr* 	iface;
      gint		port;

      GTcpSocket* 	socket;

      guint 	ref_count;

      GServerFunc	func;
      gpointer	user_data;
    } GServer;

GServer is a high-level interface to a TCP server socket. The callback is called with a GConn whenever a new connection is made.

GInetAddr *`iface`; | interface address
gint `port`; | port number
GTcpSocket *`socket`; | TCP server socket
guint `ref_count`; | [private]
GServerFunc `func`; | callback function
gpointer `user_data`; | user data for callback

### GServerFunc ()

    void                (*GServerFunc)                      (GServer *server,
                                                             GConn *conn,
                                                             gpointer user_data);

Callback for `gnet_server_new()`. When a client connects, this callback is called with a new connection. If the server fails, this callback is called with `conn` set to NULL. The callback is not called again.

If `conn` is non-NULL, the address (IP, port) of the client that established the connection can be found in the inetaddr member of the GConn structure.

`server` : | server
`conn` : | new connection (or NULL if error)
`user_data` : | user data specified in `gnet_server_new()`

### gnet_server_new ()

    GServer*            gnet_server_new                     (const GInetAddr *iface,
                                                             gint port,
                                                             GServerFunc func,
                                                             gpointer user_data);

Creates a new GServer object representing a server. Usually, `iface` is set to NULL to bind to all interfaces and `port` is a specific number. The callback is called whenever a new connection arrives or if there is a server error. The callback is not called again after a server error.

`iface` : | interface to bind to (NULL for all interfaces)
`port` : | port to bind to (0 for an arbitrary port)
`func` : | callback to call when a connection is accepted
`user_data` : | data to pass to callback
Returns : | a new GServer.

### gnet_server_delete ()

    void                gnet_server_delete                  (GServer *server);

Closes and deletes a GServer.

`server` : | GServer to delete.

### gnet_server_ref ()

    void                gnet_server_ref                     (GServer *server);

Adds a reference to a GServer.

`server` : | a GServer

### gnet_server_unref ()

    void                gnet_server_unref                   (GServer *server);

Removes a reference from a GServer. A GServer is deleted when the reference count reaches 0.

`server` : | a GServer