g_io_channel + socket = client, and GIO not work properly..

folks, i intend to create tcp client with socket, glib, and gio library,
here my code

#include <stdio.h>
#include <gio/gio.h> // g_timeout_add
#include <gtk/gtk.h> // gtk 
#include <netinet/in.h> //sockaddr_in
#include <sys/socket.h> // socket();
#include <arpa/inet.h> // inet_addr();
#include <string.h> // memset();

struct dada
    gint id_sock;
    guint id_gio_watch;

gboolean incoming(GIOChannel *chan, GIOCondition condition, struct dada *didi )
    int byte;

    int insock = g_io_channel_unix_get_fd(chan);

    #define MAXMAX 128
    char buff[128];

    printf("sock : %d\n",insock);

    byte = recv(insock,buff,MAXMAX-1,0);

    if(byte <= 0)
        return FALSE;
        buff[byte] = '\0';
        printf("coming : %s",buff);

    return TRUE;

// gtk area

void hello(GtkWidget *widget, gpointer data)
    g_print("Haii world  %s\n", (char *)data);


gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
    g_print("a delete event has been occured properly :D\n");


void destroy(GtkWidget * widget, gpointer data)
// end of gtk area

int main(int argc, char **argv)

    //gtk bussines from here
      GtkWidget *window;
    GtkWidget *button;

     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(delete_event), NULL);
    gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy), NULL);

    button = gtk_button_new_with_label("ohayo");

    gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(hello), (gpointer)"hha" );
    gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(window));



    //gtk bussiness done here...

    // network code //
    struct dada didi;

    struct sockaddr_in my; // set my network device info
    gint rootsock;         // handle the root socket

    rootsock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    my.sin_addr.s_addr = inet_addr("");
    my.sin_family      = AF_INET;
    my.sin_port        = htons(1111);
    //bind(rootsock,(struct sockaddr*)&my,sizeof(my));

    printf("sock : %d\n",rootsock);
    connect(rootsock,(struct sockaddr*)&my,sizeof(my));

    didi.id_sock = rootsock;
    didi.id_gio_watch = g_io_add_watch(g_io_channel_unix_new(didi.id_sock),G_IO_IN|G_IO_OUT,(GIOFunc)incoming,&didi);

    // network code //


    return 0;


and compiled : 
$ gcc -o konek_gioglib konek_gioglib.c `pkg-config glib-2.0 --libs --cflags gtk+-2.0`

on the other terminal i run server(with netcat) which waiting the client on port 1111 :
$ nc -v -l 1111

ultimately, i run my output binary file
$ ./konek_gioglib
sock : 6
sock : 6

the data stream is delivered but gtk likely look like a crashed, why these could be happened ?
anyone ?

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]