Trying to create a gsocket server with GUI program - sockets

I am trying to set up server that also has a gui using GTK on a Raspberry Pi. The program is just a proof-of-concept demo.
I create a socket and the client can connect and send data, I see it in wireshark. But the read does not complete until the client disconnects. My code uses g_data_input_stream_read_line_async and g_data_input_stream_read_line_finish. The program displays the data the client sent after the client disconnects.
Here is the output of the program (that is the correct data that was sent by the client).
g_socket_listener_add_inet_port status: 1
g_signal_connect status: 2 (waits here until client connects)
socket connection established!
after g_data_input_stream_new
after g_data_input_stream_set_newline_type
after g_data_input_stream_read_line_async (waits here after client connects, and after client sends data)
before g_data_input_stream_read_line_finish (displays remaining output lines after client disconnects)
after g_data_input_stream_read_line_finish
length: 14
0 5 0 4 0 1 0 1 0 6 0 0 0 7
I suspect I am missing something simple since the data is received, but I've spent quite a few hours unsuccessfully trying to figure out what is missing.
// compiles with:
// gcc `pkg-config --cflags gtk+-3.0` gtk3serv.c -o gtk3serv `pkg-config --libs gtk+-3.0`
#include <gtk/gtk.h>
#include <gio/gio.h>
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *button_box;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
gtk_container_add (GTK_CONTAINER (window), button_box);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_container_add (GTK_CONTAINER (button_box), button);
gtk_widget_show_all (window);
}
// v<>v<>v<>v<>v<>v<>v<>v<>v<>v<>v
static void on_input_read_finish(GObject *object,
GAsyncResult *result,
gpointer user_data)
{
gchar *clientdata;
gsize length = -1;
gsize i;
g_print("before g_data_input_stream_read_line_finish\n"); // only for debug
clientdata = g_data_input_stream_read_line_finish(G_DATA_INPUT_STREAM(object),
result,
&length,
NULL);
g_print("after g_data_input_stream_read_line_finish\n"); // only for debug
g_print("length: %d\n", length); // only for debug
for(i=0; i<length; i++)
{
g_print("%x ", clientdata[i]);
}
g_print("\n");
}
gboolean sockconnectionestablished(GSocketService *sockservice,
GSocketConnection *connection,
GObject *source_object,
gpointer user_Data)
{
char *clientdata;
gsize length = -1;
gsize i;
g_print("socket connection established!\n"); // only for debug
GDataInputStream *gis = g_data_input_stream_new(g_io_stream_get_input_stream(G_IO_STREAM(connection)));
g_print("after g_data_input_stream_new\n"); // only for debug
g_data_input_stream_set_newline_type(G_DATA_INPUT_STREAM(gis), G_DATA_STREAM_NEWLINE_TYPE_ANY);
g_print("after g_data_input_stream_set_newline_type\n"); // only for debug
g_data_input_stream_read_line_async(G_DATA_INPUT_STREAM(gis),
G_PRIORITY_DEFAULT,
NULL,
on_input_read_finish,
NULL);
g_print("after g_data_input_stream_read_line_async\n"); // only for debug
return 1;
}
// ^<>^<>^<>^<>^<>^<>^<>^<>^<>^<>^
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
// v<>v<>v<>v<>v<>v<>v<>v<>v<>v<>v
GError *sockerror = NULL;
GSocketService *sockservice;
gboolean sockstatus;
sockservice = g_socket_service_new();
sockstatus = g_socket_listener_add_inet_port(G_SOCKET_LISTENER(sockservice),
8888,
NULL,
NULL);
g_print("g_socket_listener_add_inet_port status: %d\n", sockstatus); // only for debug
status = g_signal_connect(sockservice,
"incoming",
G_CALLBACK(sockconnectionestablished),
NULL);
g_print("g_signal_connect status: %d\n", status); // only for debug
// ^<>^<>^<>^<>^<>^<>^<>^<>^<>^<>^
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}

Related

Gtk+ Serial port event cause responsiveness

I am trying to implement event triggered serial port read with Gtk3+.
When the data is received, GUI is getting unresponsive. I think after data receive event the loop does not go to gtk_main(). Sometimes data flow continue from sender device on serial port and GUI turn back to responsive.
So, how to return gtk_main(() loop after receive some bytes from serial?
This is the code that I wrote;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
#include <gtk/gtk.h>
void signal_handler_IO (int status); /* definition of signal handler */
volatile int n, fd;
volatile char buf [100];
int connected;
struct termios termAttr;
struct sigaction saio;
void UartInit(){
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyO1\n");
exit(1);
}
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, O_ASYNC ); /**<<<<<<------This line made it work.**/
tcgetattr(fd,&termAttr);
//baudRate = B115200; /* Not needed */
cfsetispeed(&termAttr,B115200);
cfsetospeed(&termAttr,B115200);
termAttr.c_cflag &= ~PARENB;
termAttr.c_cflag &= ~CSTOPB;
termAttr.c_cflag &= ~CSIZE;
termAttr.c_cflag |= CS8;
termAttr.c_cflag |= (CLOCAL | CREAD);
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST;
tcsetattr(fd,TCSANOW,&termAttr);
printf("UART1 configured....\n");
//close(fd);
}
int main(int argc, char *argv[])
{
GtkWidget *main_window, *label, *image, *image_alt;
GtkWidget *main_box; /* VBox to hold main_hbox and the controls */
GtkWidget *controls; /* HBox to hold the buttons and the slider */
gtk_init(&argc, &argv); //Inititalize GTK
main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(main_window, 480, 800);
label = gtk_label_new("deeneme");
//gtk_container_add(GTK_CONTAINER(window), label);
gtk_window_set_decorated(GTK_WINDOW(main_window), FALSE); //Hide menu bar
image = gtk_image_new_from_file("/home/x/Desktop/downArrow2.png");
image_alt = gtk_image_new_from_file("/home/x/Desktop/arcelik.png");
//gtk_container_add(GTK_CONTAINER(main_window), image);
controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_pack_start (GTK_BOX (controls), image, FALSE, FALSE, 50);
gtk_box_pack_start (GTK_BOX (controls), label, FALSE, FALSE, 0);
main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start (GTK_BOX (main_box), controls, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (main_box), image_alt, FALSE, FALSE, 0); //altda ki image ekle
gtk_container_add (GTK_CONTAINER (main_window), main_box);
gtk_widget_show_all(main_window); //Show all widgets
// gtk_widget_hide(image);
UartInit(); //Initialize UART
gtk_label_set_markup(GTK_LABEL(label), "<span foreground=\"white\" size='300100'>1</span>"); //Set number color and size......1234
gtk_main(); //Start main loop
return 0;
}
void signal_handler_IO (int status)
{
int n = read (fd, &buf, sizeof buf);
if(n > 1)
{
buf[n] = '\0';
printf("Receive OK %s\n",buf);
}
}

How to implement a tcp server in GTK based application using GIOChannel

I have got below sample from stackoverflow itself.
#include <glib.h>
#include <gio/gio.h>
gchar *buffer;
gboolean
network_write(GIOChannel *source,
GIOCondition cond,
gpointer data)
{
return TRUE;
}
gboolean
network_read(GIOChannel *source,
GIOCondition cond,
gpointer data)
{
GString *s = g_string_new(NULL);
GError *error;
g_print("Inside network_read function\n");
GIOStatus ret = g_io_channel_read_line_string(source, s, NULL, &error);
if (ret == G_IO_STATUS_ERROR)
g_error ("Error reading: %s\n", error->message);
else
g_print("Got: %s\n", s->str);
return TRUE;
}
gboolean
new_connection(GSocketService *service,
GSocketConnection *connection,
GObject *source_object,
gpointer user_data)
{
GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, NULL);
GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));
g_print("New Connection from %s:%d\n", g_inet_address_to_string(addr), port);
GSocket *socket = g_socket_connection_get_socket(connection);
gint fd = g_socket_get_fd(socket);
g_print("Naseeb fd: %d\n", fd);
GIOChannel *channel = g_io_channel_unix_new(fd);
if(!g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, NULL))
{
g_print("Got Error while adding network_read\n");
}
// g_io_add_watch(channel, G_IO_OUT, (GIOFunc) network_write, NULL);
return TRUE;
}
int main(int argc, char **argv) {
g_type_init();
GSocketService *service = g_socket_service_new();
GInetAddress *address = g_inet_address_new_from_string("0.0.0.0");
GSocketAddress *socket_address = g_inet_socket_address_new(address, 3001);
g_socket_listener_add_address(G_SOCKET_LISTENER(service), socket_address, G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL);
g_object_unref(socket_address);
g_object_unref(address);
g_socket_service_start(service);
g_signal_connect(service, "incoming", G_CALLBACK(new_connection), NULL);
g_socket_service_start(service);
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
return 0;
}
Using this code, i am able to get the client connected to server. I confirm the same using message printed in the function new_connection
But when i send data from client, callback network_read never gets called at server. Although client side, send() API return value shows total bytes sent.
1) Is there any api missing at server side.
2) What is proper way to invoke network_write ?
You shouldn't be using GIOChannel, new_connection() already has a connection and you can just use stream = g_io_stream_get_input_stream (G_IO_STREAM (connection)); to get a GInputStream to read from.

how to set xfce's panel plugin width

I build one simple xfce panel plugin which dispalys a button labeled as 'Hello World". However, only half of the string can be displayed.
http://en.zimagez.com/miniature/debian807092016010347.png
The code is simple:
#include <gtk/gtk.h>
#include <libxfce4panel/xfce-panel-plugin.h>
static void sample_construct(XfcePanelPlugin *plugin);
XFCE_PANEL_PLUGIN_REGISTER(sample_construct);
static void hello(GtkWidget *widget, gpointer data)
{
g_print("Hello World\n");
}
static void sample_construct(XfcePanelPlugin *plugin)
{
GtkWidget *button;
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked", G_CALLBACK (hello), NULL);
gtk_container_add (GTK_CONTAINER (plugin), button);
gtk_widget_show (button);
}
build and install with this script:
#!/bin/bash
gcc -Wall -shared -o libsample.so -fPIC sample.c $(pkg-config --cflags --libs libxfce4panel-1.0) $(pkg-config --cflags --libs gtk+-2.0) || \
{ echo "Compiling failed!"; exit 10; }
cp libsample.so /usr/lib/xfce4/panel-plugins
cp sample.desktop /usr/share/xfce4/panel-plugins
OTHER INFO: xfce4.10, Debian 8 jessie.
You missed the "size-changed" signal.
If you add the following code it's going to work as expected:
static gboolean
sample_size_changed (XfcePanelPlugin *plugin,
gint size,
void *data)
{
GtkOrientation orientation;
orientation = xfce_panel_plugin_get_orientation (plugin);
if (orientation == GTK_ORIENTATION_HORIZONTAL)
gtk_widget_set_size_request (GTK_WIDGET (plugin), -1, size);
else
gtk_widget_set_size_request (GTK_WIDGET (plugin), size, -1);
return TRUE;
}
static void sample_construct(XfcePanelPlugin *plugin)
{
...
g_signal_connect (G_OBJECT (plugin), "size-changed",
G_CALLBACK (sample_size_changed), NULL);
gtk_widget_show_all (button);
}
Pay attention that you're also missing important callbacks such as "free-data" and "orientation-changed". Note that since Xfce 4.12, GTK+ 3.0 plugins are also supported, you only need to set X-XFCE-API=2.0 in .desktop file.
Source: xfce4-sample-plugin

GTK Main blocking Others threads.How to solve

I have 2 threads
1)Holds the GTK main and gtk screen display codes (code is explained below) 2)generates key events according to user rquirement
if() block i ported into my code. but result is same. Once the signal is generated .after that its not coming to 2nd thread(signal generation thread). Have put debug prints ,but its not happening Seems its waiting on gtk_main on first thread.
What my code is :
void S1(void)
{
GtkWidget *Win_1;
GtkBuilder *builder;
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "/home/glade/glade1.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "Win_1"));
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK(kp_event), NULL);
gtk_widget_show_all(window);
gtk_main();
}
kp_event()
{
gtk_widget_destroy (window);
S2();
}
S2 is same as S1,only screen item difference.Am calling S2() from keypress handler of S1 & vice versa. Since i have no keyboards attached,need to change two screens base on some user input via sockets or something.
You may need to call gtk_main() just one time, and use gtk_widget_hide() and gtk_window_present(), instead of gtk_widget_destroy(), declaring window1 and window2 as global variables, and creating the two windows at startup. A sample code:
GtkWidget * window1;
GtkWidget * window2;
void S1() {
// create the window
window1 = GTK_WIDGET (gtk_builder_get_object (builder, "Win_1"));
// do not call gtk_main()
}
void S2() {
// create the window
window2 = GTK_WIDGET (gtk_builder_get_object (builder, "Win_2"));
// do not call gtk_main()
}
kp_event_S1() {
gtk_widget_hide(window1);
gtk_window_present(GTK_WINDOW(window2));
}
kp_event_S2() {
gtk_widget_hide(window2);
gtk_window_present(GTK_WINDOW(window1));
}
int main() {
gtk_init();
S1();
S2();
gtk_widget_hide(window2);
gtk_main();
}
If you don't want to use global variables, you can do:
GtkWidget * S1() {
// create the window
GtkWidget * window1 = GTK_WIDGET (gtk_builder_get_object (builder, "Win_1"));
return window1;
}
GtkWidget * S2() {
// create the window
GtkWidget * window2 = GTK_WIDGET (gtk_builder_get_object (builder, "Win_2"));
return window2;
}
gboolean kp_event_S1(GtkWidget * window, GdkEvent e, gpointer user_data) {
gtk_widget_hide(window);
gtk_window_present(GTK_WINDOW(user_data));
}
gboolean kp_event_S2(GtkWidget * window, GdkEvent e, gpointer user_data) {
gtk_widget_hide(window);
gtk_window_present(GTK_WINDOW(user_data));
}
int main() {
gtk_init();
GtkWidget * w1 = S1();
GtkWidget * w2 = S2();
gtk_widget_hide(w2);
g_signal_connect (G_OBJECT (w1), "key-press-event", G_CALLBACK(kp_event_S1), (gpointer)w2);
g_signal_connect (G_OBJECT (w2), "key-press-event", G_CALLBACK(kp_event_S2), (gpointer)w1);
gtk_main();
}

GIO socket-server / -client example

I would like to create a server and client application that communicate via sockets using GIO. GSocketService and GSocketClient seem be perfect for this purpose but unfortunately I couldn't find some tutorial or example code (that a GLib, GIO,... newbie can understand). Does anybody know some good resources or can post example code here?
I finally managed to create both a simple server and client using glib and gio.
My server looks like this:
#include <glib.h>
#include <gio/gio.h>
/* this function will get called everytime a client attempts to connect */
gboolean
incoming_callback (GSocketService *service,
GSocketConnection *connection,
GObject *source_object,
gpointer user_data)
{
g_print("Received Connection from client!\n");
GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
gchar message[1024];
g_input_stream_read (istream,
message,
1024,
NULL,
NULL);
g_print("Message was: \"%s\"\n", message);
return FALSE;
}
int
main (int argc, char **argv)
{
/* initialize glib */
g_type_init();
GError * error = NULL;
/* create the new socketservice */
GSocketService * service = g_socket_service_new ();
/* connect to the port */
g_socket_listener_add_inet_port ((GSocketListener*)service,
1500, /* your port goes here */
NULL,
&error);
/* don't forget to check for errors */
if (error != NULL)
{
g_error (error->message);
}
/* listen to the 'incoming' signal */
g_signal_connect (service,
"incoming",
G_CALLBACK (incoming_callback),
NULL);
/* start the socket service */
g_socket_service_start (service);
/* enter mainloop */
g_print ("Waiting for client!\n");
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
return 0;
}
and this is the corresponding client:
#include <glib.h>
#include <gio/gio.h>
int
main (int argc, char *argv[])
{
/* initialize glib */
g_type_init ();
GError * error = NULL;
/* create a new connection */
GSocketConnection * connection = NULL;
GSocketClient * client = g_socket_client_new();
/* connect to the host */
connection = g_socket_client_connect_to_host (client,
(gchar*)"localhost",
1500, /* your port goes here */
NULL,
&error);
/* don't forget to check for errors */
if (error != NULL)
{
g_error (error->message);
}
else
{
g_print ("Connection successful!\n");
}
/* use the connection */
GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
GOutputStream * ostream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
g_output_stream_write (ostream,
"Hello server!", /* your message goes here */
13, /* length of your message */
NULL,
&error);
/* don't forget to check for errors */
if (error != NULL)
{
g_error (error->message);
}
return 0;
}
Note though, that I am still new to glib, gio and even C, so double check my code before using it.
The callback from incoming should not block, from gio documentation: "The handler must initiate the handling of connection , but may not block; in essence, asynchronous operations must be used."
I had some issue with connection in the async version, it has to be referred by the user or the connection will close after the incoming callback returns.
A full example of a server that does not block, based on the example given before:
#include <gio/gio.h>
#include <glib.h>
#define BLOCK_SIZE 1024
#define PORT 2345
struct ConnData {
GSocketConnection *connection;
char message[BLOCK_SIZE];
};
void message_ready (GObject * source_object,
GAsyncResult *res,
gpointer user_data)
{
GInputStream *istream = G_INPUT_STREAM (source_object);
GError *error = NULL;
struct ConnData *data = user_data;
int count;
count = g_input_stream_read_finish (istream,
res,
&error);
if (count == -1) {
g_error ("Error when receiving message");
if (error != NULL) {
g_error ("%s", error->message);
g_clear_error (&error);
}
}
g_message ("Message was: \"%s\"\n", data->message);
g_object_unref (G_SOCKET_CONNECTION (data->connection));
g_free (data);
}
static gboolean
incoming_callback (GSocketService *service,
GSocketConnection * connection,
GObject * source_object,
gpointer user_data)
{
g_message ("Received Connection from client!\n");
GInputStream *istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
struct ConnData *data = g_new (struct ConnData, 1);
data->connection = g_object_ref (connection);
g_input_stream_read_async (istream,
data->message,
sizeof (data->message),
G_PRIORITY_DEFAULT,
NULL,
message_ready,
data);
return FALSE;
}
int main ()
{
GSocketService *service;
GError *error = NULL;
gboolean ret;
service = g_socket_service_new ();
ret = g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service),
PORT, NULL, &error);
if (ret && error != NULL)
{
g_error ("%s", error->message);
g_clear_error (&error);
return 1;
}
g_signal_connect (service,
"incoming",
G_CALLBACK (incoming_callback),
NULL);
g_socket_service_start (service);
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
/* Stop service when out of the main loop*/
g_socket_service_stop (service);
return 0;
}