【问题标题】:g_io_channel + socket = client, and GIO not work properlyg_io_channel + socket = 客户端,GIO 不能正常工作
【发布时间】:2011-03-20 17:45:55
【问题描述】:

老兄,我要创建客户端并与 GIO 通道结合,在我把它们放在一起之后,它似乎可以在套接字上工作,但是 g_io_channel 不像在看,像崩溃之类的......

请看以下代码:

#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)
    {
        perror("recv");
        close(didi->id_sock);
        g_source_remove(didi->id_gio_watch);
        return FALSE;
    }
    else
    {
        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");

    return(0);  
}

void destroy(GtkWidget * widget, gpointer data)
{
    gtk_main_quit();  
}
// end of gtk area



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

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

     gtk_init(&argc,&argv);
     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);
    gtk_container_set_border_width(GTK_CONTAINER(window),10);

    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_container_add(GTK_CONTAINER(window),button);

    gtk_widget_show(button);
    gtk_widget_show(window);

    //gtk bussiness done here...


    // network code //
    struct dada didi;
    memset(&didi,0,sizeof(didi));

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

    //socket
    rootsock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    //binding
    memset(&my,0,sizeof(my));
    my.sin_addr.s_addr = inet_addr("127.0.0.1");
    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 //

     gtk_main();





    return 0;






}

编译:

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

我自己的电脑作为服务器运行,端口为 1111 和流连接 (TCP):

$ nc -v -l 1111

运行我的应用程序:

$ ./konek_gioglib
sock : 6
sock : 6

服务器建立连接并发送一些消息:

$ nc -v -l 1111
Connection from 127.0.0.1 port 1111 [tcp/*] accepted
a
a

当服务器发送一些东西时,gtk 窗口会显示,但会出现如下错误:

有没有人不介意解释一下,为什么这些事情会发生在我身上?

【问题讨论】:

  • gtk 在套接字工作时崩溃(错误),您可能没有尝试在您的机器上运行这些代码..
  • 有没有人可以帮帮我?或者我必须在 gtk 中学习线程来完成这些事情 -_-'

标签: c sockets glib gio


【解决方案1】:

好吧,昨晚我用自己的心来集思广益,终于可以让这些东西发挥作用了,

这里是正确的代码

#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();
#include <fcntl.h>
#include <stdlib.h>

struct dada
{
    gint id_sock;
    guint id_gio_connect;
    guint id_gio_watch;
};


gboolean readdata(GIOChannel *chan,GIOCondition condition, struct dada *didi)
{

  gchar dada[20] = {0};
  int dadaz =0;


 if( condition != G_IO_IN )
    return FALSE;



  if(dadaz = recv(g_io_channel_unix_get_fd(chan),dada,19,0)<=0)
  {
      perror("recv");
      close(didi->id_sock);
      g_source_remove(didi->id_gio_connect);
      g_source_remove(didi->id_gio_watch);
      exit(0);
      return FALSE;
  }

      printf("data in : %s\n",dada);




         return TRUE;
}


gboolean incoming(GIOChannel *chan, GIOCondition condition, struct dada *didi )
{


     if( condition & G_IO_ERR || condition & G_IO_HUP )
     return FALSE;


    didi->id_gio_watch = g_io_add_watch(chan,G_IO_IN | G_IO_ERR | G_IO_HUP,(GIOFunc)readdata,didi);


    return FALSE;
}


// 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");

    return(0);
}

void destroy(GtkWidget * widget, gpointer data)
{
    gtk_main_quit();
}
// end of gtk area



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

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

     gtk_init(&argc,&argv);
     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);
    gtk_container_set_border_width(GTK_CONTAINER(window),10);

    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_container_add(GTK_CONTAINER(window),button);

    gtk_widget_show(button);
    gtk_widget_show(window);

    //gtk bussiness done here...


    // network code //
    struct dada didi;
    memset(&didi,0,sizeof(didi));

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

    //socket
    rootsock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);



    memset(&your,0,sizeof(your));


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

    your.sin_family = AF_INET;
    your.sin_addr.s_addr = inet_addr("127.0.0.1");
    your.sin_port   =   htons(1111);

    connect(rootsock,(struct sockaddr*)&your,sizeof(your));

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

    // network code //

     gtk_main();





    return 0;






}

唯一不同的是“工作”代码,需要:

- gio for connecting, and gio for watching

而不是“非工作”代码(only gio for connecting),但我再次想知道“为什么”,为什么在 connect() 上需要两个 gio(递归)才能使这些“gio 事物”工作,

这些真的很奇怪,如果我在 g_io_channel + socket = server , still just get one client ? in C language 上看到的话

accept() 只需要一个 gio 并且只用于观看。

如果有人能解释这些,那该有多好:)

【讨论】:

    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 2019-02-06
    • 2017-09-23
    • 2018-10-12
    • 2020-04-18
    • 1970-01-01
    相关资源
    最近更新 更多