Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4053790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:29:31+00:00 2026-05-20T14:29:31+00:00

folks please see following code : #include <gio/gio.h> #include <sys/socket.h> //socket(); #include <netinet/in.h> //sockaddr_in

  • 0

folks please see following code :

#include <gio/gio.h>
#include <sys/socket.h> //socket();
#include <netinet/in.h> //sockaddr_in
#include <arpa/inet.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define RECV_SIZE_MODE 1024 // mmmm

    typedef struct _unit_
    {
        gint sockmax;
        fd_set client,client_tmp;
    } _unit;



gboolean network_deal_api(GIOChannel *in,GIOCondition condition, _unit *unit)
{
    struct sockaddr_storage income;

            unit->client_tmp = unit->client;


    gint i;

    //enum client
    gchar ip[INET_ADDRSTRLEN];
    guint port;

    //convert
    gint insock = g_io_channel_unix_get_fd(in);


    //prepare for new income
    socklen_t income_len = sizeof(income);
    gint newsock;

    //something come in
    char buffer_in[RECV_SIZE_MODE] = {0}; 
    gint recv_sizing;

    switch(condition)
    {
        case G_IO_IN:


        for(i=0; i <= unit->sockmax; i++)
        {

          if(FD_ISSET(i,&unit->client_tmp)) //something coming through handler inside client_tmp, we'll see if the socket has suppose to be someone own
          {
              printf("process on socket number : %d\n",i);
                    if(i == insock) // if handler of socket(); then create new accept handler, if these is a handler of accept();, then else which mean 'incoming data'
                    {
                        newsock = accept(insock,(struct sockaddr*)&income, &income_len);
                        if(newsock == -1)
                        {
                            printf("failure once getting new socket\n");
                            return FALSE;
                        }
                        else
                        {
                            FD_SET(newsock, &unit->client);
                            if(newsock > unit->sockmax)
                            {
                                unit->sockmax = newsock;
                            }

                        inet_ntop(income.ss_family,&((struct sockaddr_in*)&income)->sin_addr,ip,INET_ADDRSTRLEN);
                        port = htons(((struct sockaddr_in*)&income)->sin_port);
                        printf("connected to : %s:%d on socket : %d\n",ip,port,newsock);
                        }



                    }
                    else
                    {


                        if(recv_sizing = recv(i,buffer_in,RECV_SIZE_MODE,0) <= 0)
                        {
                            if(recv_sizing == 0)
                            {
                                printf("socket %d hung up\n",i);
                            }
                            if(recv_sizing == -1)
                            {
                                perror("recv");
                            }
                            close(i);
                            FD_CLR(i,&unit->client);
                        }
                        buffer_in[recv_sizing-1] = '\0';
                        printf("data in : %s length : %d\n",buffer_in,strlen(buffer_in));

                    }
            }
        }
        break;

    }
}

int main()
{


    _unit *unit = (_unit*)malloc(sizeof(_unit));

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

    GIOChannel *in_handle;
    guint in_handle_watching;       // whether the watching event intend to remove

     FD_ZERO(&unit->client); // initiate
     FD_ZERO(&unit->client_tmp);






    //let construct a server

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

    //binding
    memset(&my,0,sizeof(my));
    my.sin_addr.s_addr = INADDR_ANY;
    my.sin_family      = AF_INET;
    my.sin_port        = htons(1111);
    bind(rootsock,(struct sockaddr*)&my,sizeof(my));

    //set the queue, let say for commonly situation 10
    listen(rootsock,10);

    FD_SET(rootsock,&unit->client);

    unit->sockmax = rootsock; //so far its this one


     in_handle = g_io_channel_unix_new(rootsock); // handed socket to channel

    in_handle_watching = g_io_add_watch(in_handle,G_IO_IN|G_IO_OUT,(GIOFunc)network_deal_api,unit);

    printf("server listening on : %d\n",ntohs(my.sin_port));
    printf("look forward for any new income connection\n");

    GMainLoop *loop = g_main_loop_new(NULL,FALSE);
    g_main_loop_run(loop);

    free(unit);

    return 0;
    }

and compiled and run :

$ gcc -o multiplechat_gio multiplechat_gio.c `pkg-config --libs --cflags gio-2.0`
$ ./multiplechat_gio 
  server listening on : 1111
  look forward for any new income connection

someone telnet into it :

$ telnet localhost 1111
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
tes
aja
deh
^C

and why the server is not showing the packet (and show packet once another people connect to the server only) ?

$ ./multiplechat_gio 
server listening on : 1111
look forward for any new income connection
process on socket number : 3
connected to : 127.0.0.1:48814 on socket : 4

whats wrong then ?
anyone ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:29:31+00:00Added an answer on May 20, 2026 at 2:29 pm

    I’m afraid you’re not understanding what GIO is and does, and are attempting to mix it with a traditional select() loop. In your current code, GIO is only ever looking at the original file descriptor you gave it in g_io_add_watch(in_handle,G_IO_IN|G_IO_OUT,(GIOFunc)network_deal_api,unit);. It doesn’t use fd_set structures.

    You have to use the GIO API when you accept each connection creating a new GIOChannel with g_io_channel_unix_new() and adding it using g_io_add_watch().

    GIO is maintaining the set and doing all the dirty work for you in its event loop, notifying you when a GIOChannel has the condition you specify in the g_io_add_watch() call.

    (Edited to make more clear)

    Also note – the last argument to g_io_add_watch() is supposed to be used to hold data specific to the IOChannel added (you would malloc() it when you accept the connection) . This is where you stick connection specific data, like a username, or previous state, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

folks, if you dont mind please see following code : #include <glib.h> #include <gio/gio.h>
Folks I am surprised to see my code working, that I dont have any
Update: fixed delta calculations in code, still the issue remains Folks, could you please
Folks, Please does anyone know how to show a Form from an otherwise invisible
I'm a bit of a WPF newbie, so go easy on me please folks!
Folks, i have the following problem. My website uses an iframe to select an
Folks, please advice is there are any free available package(s) for OAuth? I need
Folks, Please, what's a good way of writing really big XML documents (upto say
Hey folks, this question can't be too complicated. Please provide a solution to at
Folks could someone please share the ways to debug a Stored Proc ? It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.