Today
-
Yesterday
-
Total
-
  • GLib와 GIO를 이용한 간단한 네트워크 서버와 클라이언트 예제
    Programmer/Computer Skills 2013. 8. 13. 16:25

    예전에 인터넷에서 우연히 발견한 GLib와 GIO를 이용한 간단한 네트워크 서버/클라이언트 예제이다. GLib의 이벤트 루프와 GIO의 네트워크 기능을 활용하여 서버를 개발하는 좋은 샘플이 될 것으로 기대한다.


    서버 소스

    #include <glib.h>
    #include <gio/gio.h>
    
    static GInetAddress*
    get_localhost()
    {
        /* No clean way to handle IPV6, short of guess/check */
        GInetAddress* addr = g_inet_address_new_any(G_SOCKET_FAMILY_IPV4);
        if (!addr) {
            addr = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6);
        }
        return addr;
    }
    
    /* this function will get called everytime a client attempts to connect */
    gboolean
    incoming_callback(GSocketService *service,
                      GSocketConnection *connection,
                      GObject *source_object,
                      gpointer user_data)
    {
        g_print("Received connection from client!\n");
        GInputStream *istream = g_io_stream_get_input_stream(G_IO_STREAM(connection));
        gchar message[1024];
        g_input_stream_read(istream,
                            message,
                            1024,
                            NULL,
                            NULL);
        g_print("Message was: \"%s\"\n", message);
        return FALSE;
    }
    
    int
    main(int argc, char **argv)
    {
        /* initialize glib */
        g_type_init();
    
        /* initialize the thread system */
        if (!g_thread_supported())
            g_thread_init(NULL);
    
        GError *error = NULL;
    
        /* create a new socketservice */
        GSocketService *service = g_threaded_socket_service_new(10);
    
        /* create a new address */
        GInetAddress *inet_addr = get_localhost();
        GSocketAddress *sock_addr = g_inet_socket_address_new(inet_addr,
                                                              1500); /* your port goes here */
    
        /* connect to the address */
        GSocketAddress* effective_addr;
        g_socket_listener_add_address(G_SOCKET_LISTENER(service),
                                      sock_addr,
                                      G_SOCKET_TYPE_STREAM,
                                      G_SOCKET_PROTOCOL_TCP,
                                      NULL,
                                      &amp;effective_addr,
                                      &amp;error);
    
        /* don't forget to check for errors */
        if (error != NULL) {
            g_error(error-&gt;message);
        }
    
        /* listen to the 'incoming' signal */
        g_signal_connect(service,
                         "incoming",
                         G_CALLBACK(incoming_callback),
                         NULL);
    
        /* start the socket service */
        g_socket_service_start(service);
    
        /* enter mainloop */
        g_print("Waiting for client!\n");
        GMainLoop *loop = g_main_loop_new(NULL, FALSE);
        g_main_loop_run(loop);
        g_main_loop_unref(loop);
    
        return 0;
    }



    클라이언트 소스

    #include <glib.h>
    #include <gio/gio.h>
    
    int
    main(int argc, char **argv)
    {
        /* initialize glib */
        g_type_init();
    
        GError *error = NULL;
    
        /* create a new connection */
        GSocketConnection *connection = NULL;
        GSocketClient *client = g_socket_client_new();
    
        /* connect to the host */
        connection = g_socket_client_connect_to_host(client,
                                                     (gchar *)"localhost",
                                                     1500, /* your port goes here */
                                                     NULL,
                                                     &amp;error);
    
        /* don't forget to check for errors */
        if (error != NULL) {
            g_error(error-&gt;message);
        }
        else {
            g_print("Connection successful!\n");
        }
    
        /* use the connection */
        GInputStream *istream = g_io_stream_get_input_stream(G_IO_STREAM(connection));
        GOutputStream *ostream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
        g_output_stream_write(ostream,
                              "Hello server!", /* your message goes here */
                              13,              /* length of your message */
                              NULL,
                              &amp;error);
    
        /* don't forget to check for errors */
        if (error != NULL) {
            g_error(error-&gt;message);
        }
    
        return 0;
    }



    댓글

Designed by Tistory.