1. 程式人生 > >glib實現Socket通訊

glib實現Socket通訊

server

#include <glib.h>
#include <gio/gio.h>
#include <stdio.h>

gchar *buffer;

gboolean network_read(GIOChannel *source,
            GIOCondition cond,
            gpointer data)
{
    GError *error=NULL;
    gsize len;
    GString *s = g_string_new(NULL);
    const gchar *msg = "The price of greatness is responsibility."
; GIOStatus ret = g_io_channel_read_line_string(source, s, NULL, &error); if (ret == G_IO_STATUS_ERROR){ g_error ("Error reading: %s\n", error->message); g_object_unref(data); return FALSE; } else if (ret == G_IO_STATUS_EOF) { g_print("finished\n"
); return FALSE; } else { g_print("Got: %s\n", s->str); //write ret = g_io_channel_write_chars(source, msg, -1, &len, &error); g_io_channel_flush(source, NULL); if (ret == G_IO_STATUS_ERROR) g_error ("Error writing: %s\n"
, error->message); printf ("Wrote %u bytes.\n", len); return TRUE; } } gboolean new_connection(GSocketService *service, GSocketConnection *connection, GObject *source_object, gpointer user_data) { // tell glib not to disconnect g_object_ref(connection); GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, NULL); GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr)); guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr)); g_print("New Connection from %s:%d\n", g_inet_address_to_string(addr), port); GSocket *socket = g_socket_connection_get_socket(connection); gint fd = g_socket_get_fd(socket); GIOChannel *channel = g_io_channel_unix_new(fd); g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, connection); return TRUE; } int main(int argc, char **argv) { GSocketService *service = g_socket_service_new(); GInetAddress *address = g_inet_address_new_from_string("127.0.0.1"); GSocketAddress *socket_address = g_inet_socket_address_new(address, 4000); g_socket_listener_add_address(G_SOCKET_LISTENER(service), socket_address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL); g_object_unref(socket_address); g_object_unref(address); g_socket_service_start(service); g_signal_connect(service, "incoming", G_CALLBACK(new_connection), NULL); GMainLoop *loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(loop); }

client

#include <glib.h>
#include <gio/gio.h>

int main(int argc, char *argv[])
{

    GError *error = NULL;

    GSocketClient * client = g_socket_client_new();

    GSocketConnection * connection = g_socket_client_connect_to_host (client,"127.0.0.1",4000,NULL,&error);

    if (error){
        g_error(error->message);
    }else{
        g_message("Connection ok");
    }

    gchar *buffer="hello";
    gchar incoming_buff[100]={0};

    GInputStream * in_stream = g_io_stream_get_input_stream(G_IO_STREAM(connection));
    GOutputStream * out_stream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
    g_output_stream_write(out_stream, buffer, 6, NULL, &error);
    int incoming_num = g_input_stream_read(in_stream,incoming_buff,100,NULL,&error);

    if(error)
        g_error(error->message);
    else{
        g_print("incoming: %s\n", incoming_buff);
    }

    return TRUE;
}

編譯

	gcc -g  server.c -o server $(pkg-config --cflags --libs glib-2.0 gobject-2.0 gtk+-2.0)
	gcc -g  client.c -o client $(pkg-config --cflags --libs glib-2.0 gobject-2.0 gtk+-2.0)