Hello,

I am trying to use GIOChannel in order to use sockets in a simple way (I
am trying to get notification on events like read and disconnect).
Anyway, here is the code that creates the GIOCHannel

void
on_ListenButton_clicked                (GtkButton       *button,
                                        gpointer         user_data)
{
        GtkWidget *text = lookup_widget(GTK_WIDGET(button),
"StatusText");
        
        // create the listening socket
        int sockfd, new_fd;  
        struct sockaddr_in my_addr;    
        struct sockaddr_in their_addr; 
        int sin_size;

         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
          perror("socket");
          exit(1);
        }
         
        my_addr.sin_family = AF_INET;        
        my_addr.sin_port = htons(1352); 
        my_addr.sin_addr.s_addr = INADDR_ANY; 
        bzero(&(my_addr.sin_zero), 8);       

        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct 
                        sockaddr)) == -1) {
                perror("bind");
                exit(1);
        }
         
         
        if (listen(sockfd, 5) == -1) {
                perror("listen");
                exit(1);
        }
        gtk_entry_set_text(GTK_ENTRY(text), "Listening");
         
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, 
                        &sin_size)) == -1) {
                perror("accept");
        }
          
        // we got a socket so set text box
        gtk_entry_set_text(GTK_ENTRY(text), "Connection accepted");
          
        // now create GIOChannel from connection 
        GIOChannel *iochannel = g_io_channel_unix_new(new_fd);
        
        // now add to main event loop
        GtkWidget *window = lookup_widget(GTK_WIDGET(button),
                                                "MainWindow");
        g_io_add_watch(iochannel, G_IO_IN, (GIOFunc *) &ReceiveData,    
                                                (gpointer) window);
         
        
        
        // now close the listener
        close(sockfd);
        
}

The function g_io_watch() ties to the G_IO_IN (there is data to read on
the socket) event to the function ReceiveData that is defined as

gboolean ReceiveData(GIOChannel *source, GIOCondition condition,
gpointer data)
{
        gchar buf[500];
        int bytes_read;
        GtkWidget *text = NULL;
        GQuark quark;
        GError *error = NULL;
        gint sd;        
        
        
        
        g_print("data received\n");
        text = lookup_widget(GTK_WIDGET(data), "StatusText");
        gtk_entry_set_text(GTK_ENTRY(text), "received data");
        
        sd = g_io_channel_unix_get_fd(source);
        
        bytes_read = recv(sd, buf, 500, 0);
        
        if(bytes_read == -1) {
                g_print("error\n");
        }
        else {
                buf[bytes_read] = '\0';
                g_print("buf = %s\n", buf);
                text = lookup_widget(GTK_WIDGET(data), "ReceivedText");
                gtk_entry_set_text(GTK_ENTRY(text), buf);
        }
        
        return TRUE;
}

The problem is this.  If the client closes the connection then I get a
flood of empty buffers and the ReceiveData() function is repeatedly
called.  What could cause this weird behavior?


Travis Miller

_______________________________________________
gtk-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gtk-list

Reply via email to