2012-03-01 14 views
7

मैं एक सर्वर और क्लाइंट एप्लिकेशन बनाना चाहता हूं जो जीआईओ का उपयोग करके सॉकेट के माध्यम से संवाद करता है। GSocketService और GSocketClient इस उद्देश्य के लिए बिल्कुल सही प्रतीत होते हैं लेकिन दुर्भाग्यवश मुझे कुछ ट्यूटोरियल या उदाहरण कोड नहीं मिला (कि एक जीएलआईबी, जीआईओ, ... नौसिखिया समझ सकता है)। क्या किसी को कुछ अच्छे संसाधन पता हैं या यहां उदाहरण कोड पोस्ट कर सकते हैं?जीआईओ सॉकेट-सर्वर/-client उदाहरण

+0

आप किसी भी प्रगति कर किया था: पहले

एक सर्वर है कि ब्लॉक नहीं करता है, उदाहरण के आधार पर की एक पूरी उदाहरण दिया? मैं वही चीज़ ढूंढ रहा हूं। [एपीआई] के अलावा (http://developer.gnome.org/gio/stable/highlevel-socket.html) और यह [SO उत्तर] (http://stackoverflow.com/a/2145259/545442) मैं हूं ' टी कुछ भी मिला। – noisebleed

+0

@noisebleed: हाँ मैंने वास्तव में प्रगति की है। मुझे वास्तव में नहीं मिलता है कि मैं अपनी पहली कोशिश में सर्वर और क्लाइंट दोनों बनाने में सक्षम नहीं था। शायद मुझे एक ही समय में सी, ग्लिब और ओजीआरई सीखने की कोशिश नहीं करनी चाहिए। – drakide

उत्तर

17

मैं अंत में दोनों एक सरल सर्वर और ग्राहक चिकना और Gio का उपयोग कर बनाने में कामयाब रहे।
मेरे सर्वर इस तरह दिखता है:

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

/* 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(); 

    GError * error = NULL; 

    /* create the new socketservice */ 
    GSocketService * service = g_socket_service_new(); 

    /* connect to the port */ 
    g_socket_listener_add_inet_port ((GSocketListener*)service, 
            1500, /* your port goes here */ 
            NULL, 
            &error); 

    /* don't forget to check for errors */ 
    if (error != NULL) 
    { 
     g_error (error->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); 
    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, 
               &error); 

    /* don't forget to check for errors */ 
    if (error != NULL) 
    { 
     g_error (error->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, 
          &error); 
    /* don't forget to check for errors */ 
    if (error != NULL) 
    { 
     g_error (error->message); 
    } 
    return 0; 
} 

नोट हालांकि, कि मैं अभी भी GLib लिए नया हूँ, जियो और यहां तक ​​कि सी है, तो डबल मेरी कोड की जाँच करें इसका उपयोग करने से पहले।

+0

इसे साझा करने के लिए धन्यवाद। यह जीआईओ नवागंतुकों के लिए एक अमूल्य संसाधन होगा। संदर्भ दस्तावेज अच्छा है लेकिन उदाहरणों की कमी है। – noisebleed

+0

@noisebleed: यहां प्रलेखन के बारे में एक ही राय। – drakide

+0

असल में, आप क्लाइंट के रूप में प्रमाणित करने के लिए क्लाइंट के रूप में इस तरह के एक कमांड "echo anyword | nc localhost 1500" का उपयोग कर सकते हैं। – mxi1

5

आने वाले कॉलबैक को जैव दस्तावेज़ीकरण से अवरुद्ध नहीं करना चाहिए: "हैंडलर को कनेक्शन की हैंडलिंग शुरू करनी चाहिए, लेकिन ब्लॉक नहीं हो सकता है; संक्षेप में, एसिंक्रोनस ऑपरेशंस का उपयोग किया जाना चाहिए।"

मुझे एसिंक संस्करण में कनेक्शन के साथ कुछ समस्या थी, इसे उपयोगकर्ता द्वारा संदर्भित किया जाना चाहिए या आने वाले कॉलबैक रिटर्न के बाद कनेक्शन बंद हो जाएगा।

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

#define BLOCK_SIZE 1024 
#define PORT 2345 

struct ConnData { 
    GSocketConnection *connection; 
    char message[BLOCK_SIZE]; 
}; 

void message_ready (GObject * source_object, 
    GAsyncResult *res, 
    gpointer user_data) 
{ 
    GInputStream *istream = G_INPUT_STREAM (source_object); 
    GError *error = NULL; 
    struct ConnData *data = user_data; 
    int count; 

    count = g_input_stream_read_finish (istream, 
     res, 
     &error); 

    if (count == -1) { 
    g_error ("Error when receiving message"); 
    if (error != NULL) { 
     g_error ("%s", error->message); 
     g_clear_error (&error); 
    } 
    } 
    g_message ("Message was: \"%s\"\n", data->message); 
    g_object_unref (G_SOCKET_CONNECTION (data->connection)); 
    g_free (data); 
} 

static gboolean 
incoming_callback (GSocketService *service, 
    GSocketConnection * connection, 
    GObject * source_object, 
    gpointer user_data) 
{ 
    g_message ("Received Connection from client!\n"); 
    GInputStream *istream = g_io_stream_get_input_stream (G_IO_STREAM (connection)); 
    struct ConnData *data = g_new (struct ConnData, 1); 

    data->connection = g_object_ref (connection); 

    g_input_stream_read_async (istream, 
     data->message, 
     sizeof (data->message), 
     G_PRIORITY_DEFAULT, 
     NULL, 
     message_ready, 
     data); 
    return FALSE; 
} 

int main() 
{ 
    GSocketService *service; 
    GError *error = NULL; 
    gboolean ret; 

    service = g_socket_service_new(); 
    ret = g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service), 
     PORT, NULL, &error); 

    if (ret && error != NULL) 
    { 
    g_error ("%s", error->message); 
    g_clear_error (&error); 
    return 1; 
    } 

    g_signal_connect (service, 
     "incoming", 
     G_CALLBACK (incoming_callback), 
     NULL); 

    g_socket_service_start (service); 
    GMainLoop *loop = g_main_loop_new(NULL, FALSE); 
    g_main_loop_run(loop); 

    /* Stop service when out of the main loop*/ 
    g_socket_service_stop (service); 
    return 0; 
} 
संबंधित मुद्दे