GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Dec 22, 2014 9:22 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Help with GTK, autorefresh textview
PostPosted: Thu Apr 14, 2011 4:34 am 
Offline

Joined: Thu Apr 14, 2011 4:30 am
Posts: 4
Hello guys, Im new here.

I got assignment to create chatting programing using C and socket programming

I already create the GUI using GTK, and now I face 1 big problem..

when I send the chat from server to client, it can't appear, it just appear once

this is the code :

http://pastebin.com/nHMDCqbL

I think because of the gtk_main();.. one of the solution is creating refresh program, but ofcouse its very bad, everytime chat I must press the refresh button

anyone can help me ??

thanks


Top
 Profile  
 
 Post subject: Re: Help with GTK, autorefresh textview
PostPosted: Thu Apr 14, 2011 4:40 am 
Offline

Joined: Thu Apr 14, 2011 4:30 am
Posts: 4
polkjhydvrg wrote:
ImageBetween us speaking, I would arrive differently.


im sorry, what do you mean ?


Top
 Profile  
 
 Post subject: Re: Help with GTK, autorefresh textview
PostPosted: Thu Apr 14, 2011 9:00 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Hi and welcome,

Since this is for an assignment I will not post any code.

I had a quick look at your code and there seams to be a number of of errors.

The first error I spotted is never use the function gets(), it is error prone and a security risk, use fgets() instead.
Next you are trying to have your own busy loop around gtk_main() and test(). This is the incorrect way to use it as gtk_main() is the main event loop for your application.

There are two ways to to monitor file IO within the gtk_main() event loop. Firstly using g_io_channel functions http://developer.gnome.org/glib/stable/glib-IO-Channels.html.

These are the basic functions you will need

// Linux etc...
GIOChannel * g_io_channel_unix_new (int fd);
// Windows
GIOChannel * g_io_channel_win32_new_socket (gint socket);

// connect a callback to the channel to watch and what to watch for, ie.
guint g_io_add_watch (GIOChannel *channel,
GIOCondition condition,
GIOFunc func,
gpointer user_data);

// This is the prototype for your callback
gboolean (*GIOFunc) (GIOChannel *source,
GIOCondition condition,
gpointer data);

Secondly directly in the main loop http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html.
This is a much more complex API and I would advise the use of GIO functions instead.

Hope this is a pointer in the right direction. If you need any more help after giving it a try then please as again.

E.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Help with GTK, autorefresh textview
PostPosted: Thu Apr 14, 2011 10:01 am 
Offline

Joined: Thu Apr 14, 2011 4:30 am
Posts: 4
thx for the reply

how about using

while (gtk_events_pending())
gtk_main_iteration ();

It work, it can refresh the GUI, and it will print the message that received, but I can type in the entry.


Top
 Profile  
 
 Post subject: Re: Help with GTK, autorefresh textview
PostPosted: Thu Apr 14, 2011 4:21 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Yes that would work, but you will still be in a busy loop consuming CPU cycles and making your machine less responsive. By using a callback such as the GIO API for files, pipes and sockets can reduce the number CPU cycles your application uses. Also using the gtk_events_pending() loop can become error prone once your application becomes bigger.

So making your code look more like this for reading from the network may improve performance. I shall leave you to fill in the other bits. Also using this method it is possible to watch for more than reading, such as if there is space to write data, if the network has been disconnected or there are errors with a small modification.

Code:

gboolean test(GIOChannel *source, GIOCondition condition, gpointer data)
{
    ....

    return TRUE;
}

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

    ....

    channel = g_io_channel_unix_new(sock);
    g_io_add_watch(channel, test, NULL);

    gtk_main();

    ....
}

E.

_________________
E.


Last edited by errol on Fri Apr 15, 2011 2:48 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Help with GTK, autorefresh textview
PostPosted: Thu Apr 14, 2011 8:52 pm 
Offline

Joined: Thu Apr 14, 2011 4:30 am
Posts: 4
thx for reply

I have tried it, and my last code is like this

http://pastebin.com/2E4V0YQV

when I compile, it show many error (forget first about the gets)

client.c:111: warning: passing argument 1 of ‘g_io_channel_unix_new’ makes integer from pointer without a cast
client.c:117: warning: passing argument 3 of ‘g_io_add_watch’ from incompatible pointer type
/usr/local/include/glib-2.0/glib/giochannel.h:199: note: expected ‘GIOFunc’ but argument is of type ‘void (*)(void)’

when I run it, it appear other error

(client.out:2171): GLib-WARNING **: giounix.c:411Error while getting flags for FD: Bad file descriptor (9)

thx ;)

============= EDIT ===============

Im sorry, now it can :D I do several changes.. I forget to change the socket name into my sock name :D

can I ask 1 more thing ?

do you know how to make event so when I press my "enter" in my keyboard, it can do several event (send message maybe)

thx ;)

my program look :

Image


Top
 Profile  
 
 Post subject: Re: Help with GTK, autorefresh textview
PostPosted: Fri Apr 15, 2011 2:48 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
You can assign the enter key as an accelerator to your send button, so your user can click on the send button or press enter. It looks like you are using Glade, so to get to this feature select your button. Then select the Common tab on the right hand side. Scroll down and you will see Accelerators where you can click on the button to then assign a key press to an event.

E.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group