GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Apr 20, 2014 8:15 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: gtk2.0 client chat UI ("DATA" help)!
PostPosted: Mon Jul 09, 2012 9:10 pm 
Offline
Familiar Face

Joined: Mon Mar 05, 2012 11:55 pm
Posts: 12
So, I am coding a client chat UI with GTK2.0 for Linux. I have the shell of the UI as well as a client side socket. Now I have to make the data part of the chat work.

So, now I have a question.

1. How would I do this?
I assume I would create a gtk bufer and use it with the Linux send or write function and set up signal handlers and callbacks for the send button. What I am afraid of is that if I use a gtk buffer to store the data, it will encode the data in the gtk based buffer using UTF-8. I would most likely then have to convert the data from UTF-8 to us-ascii; Am i wrong?

If someone could please explain how I would achieve this or a better way to accomplish this task; I would greatly appreciate it. Thanks

HERE IS MY CODE:
Code:
#include <gtk/gtk.h>
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

static void destroy_event(GtkWidget *widget, gpointer data) {
gtk_main_quit();
}

static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
gtk_main_quit();
return FALSE;
}

//To compile append `pkg-config --cflags --libs gtk+-2.0`
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *table;
GtkWidget *view;
GtkWidget *view2;
GtkWidget *file;
GtkWidget *quit;
GtkWidget *button;
GtkWidget *scrolledwindow;
GtkWidget *scrolledwindow2;
GtkWidget *menubar;
GtkWidget *filemenu;
GtkWidget *text_entry;

GdkColor color;

int sockfd, portno, yes = 1;
ssize_t bytes_read, bytes_written; 
struct hostent *server;
struct sockaddr_in serv_addr, cli_addr;
char buffer[4096];

   if(argc < 3)
   {
   fprintf(stderr, "Ussage: %s + IP Address + port No.\n", argv[0]);
   exit(0);
   }

sockfd = socket(AF_INET, SOCK_STREAM, 0);
   if(sockfd < 0) {
   printf("SOCKET(-1) error ---> %s.\n", strerror(errno));
   exit(EXIT_FAILURE);
   }

   if(sockfd == 0) {
   printf("SOCKET(0) error ---> %s.\n", strerror(errno));
   exit(EXIT_FAILURE);
   }


setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

bzero((char *) &serv_addr, sizeof(serv_addr));
server = gethostbyname(argv[1]);
   if(server == NULL) {
   fprintf(stderr, "No such host.\n");
   printf("%s\n", strerror(errno));
   exit(EXIT_FAILURE);
   }

portno = atoi(argv[2]); //Grabbing the port number from the command line
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
serv_addr.sin_port = htons(portno);

   if(connect(sockfd, (const struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
   fprintf(stderr, "%s. CONNECT()\n", strerror(errno));
   exit(EXIT_FAILURE);
   }
      else {
      printf("Made a connection to %s\n", inet_ntoa(serv_addr.sin_addr));
      }

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 500, 400);
gtk_widget_show(window);

table = gtk_table_new(10, 10, TRUE);
gtk_container_add(GTK_CONTAINER(window), table);
gtk_widget_show(table);

menubar = gtk_menu_bar_new();
gtk_table_attach_defaults(GTK_TABLE(table), menubar, 0, 10, 0, 1);
gtk_widget_show(menubar);

filemenu = gtk_menu_new();
gtk_widget_show(filemenu);

file = gtk_menu_item_new_with_label("File");
quit = gtk_menu_item_new_with_label("Quit");
gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
gtk_widget_show(quit);
gtk_widget_show(file);

view = gtk_text_view_new();
gtk_widget_show(view);

//text_entry = gtk_entry_new();
//gtk_table_attach_defaults(GTK_TABLE(table), text_entry, 1, 8, 9, 10);
//gtk_widget_show(text_entry);

button = gtk_button_new_with_label("Send");
gtk_table_attach_defaults(GTK_TABLE(table), button, 8, 9, 9, 10);
gtk_widget_show(button);

gdk_color_parse("black", &color);
gtk_widget_modify_bg(GTK_WIDGET(menubar), GTK_STATE_NORMAL, &color);

scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_IN);
gtk_container_add(GTK_CONTAINER(scrolledwindow), view);
gtk_table_attach_defaults(GTK_TABLE(table), scrolledwindow, 1, 9, 2, 7);
gtk_widget_show(scrolledwindow);

view2 = gtk_text_view_new();
gtk_widget_show(view2);

scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow2), GTK_SHADOW_OUT);
gtk_container_add(GTK_CONTAINER(scrolledwindow2), view2);
gtk_table_attach_defaults(GTK_TABLE(table), scrolledwindow2, 1, 8, 9, 10);
gtk_widget_show(scrolledwindow2);

g_signal_connect_swapped(G_OBJECT(quit), "activate", G_CALLBACK(gtk_main_quit), NULL);

g_signal_connect_swapped(G_OBJECT(window), "destroy-event", G_CALLBACK(destroy_event), NULL);

g_signal_connect_swapped(G_OBJECT(window), "delete-event", G_CALLBACK(delete_event), NULL);

gtk_main();
return 0;
}


Top
 Profile  
 
 Post subject: Re: gtk2.0 client chat UI ("DATA" help)!
PostPosted: Tue Jul 10, 2012 11:39 pm 
Offline
Familiar Face

Joined: Mon Mar 05, 2012 11:55 pm
Posts: 12
I actually found documentation on this and you can find it here -> http://developer.gnome.org/glib/2.32/glib-IO-Channels.html


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

All times are UTC


Who is online

Users browsing this forum: No registered users 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