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 4014766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:32:11+00:00 2026-05-20T09:32:11+00:00

folks, if you dont mind please see following code : #include <glib.h> #include <gio/gio.h>

  • 0

folks,

if you dont mind please see following code :

#include <glib.h>
#include <gio/gio.h> // gio channel

#include <sys/socket.h> //socket();
#include <netdb.h> // structure

#include <stdio.h> // printf

void deal(GIOChannel *in, GIOCondition condition, gpointer data)
{
    struct sockaddr_storage income;

    int insock = g_io_channel_unix_get_fd(in);
    socklen_t income_len = sizeof(income);
    int newsock = accept(insock, (struct sockaddr*)&income, &income_len );
    if(newsock == -1)
    {
        printf("failure on newsock\n");
    }

    char buff[128];

    int recv_total = 0;
    int recv_byte = 128;
    int recv_sizing;

    while (recv_total < recv_byte ){

    recv_sizing = recv(newsock,buff + recv_total,recv_byte,0);

    // breaking if recv_sizing = -1 assuming as error, 0 assuming as lost communication from client suddenly
    if(recv_sizing < 0 || recv_sizing == 0)
     {
         printf("connection lost or error while recv(); [ just guess ] number : %d \n",recv_sizing);
         break;
    }

    recv_byte -= recv_sizing;
    recv_total += recv_sizing;


    }


    buff[recv_total] = '\0';
    //recv_sizing = recv(newsock,buff,recv_byte,0);
    printf("data : %s\n",buff);

    close(newsock); // close immediate and look for another some1 new


}

int main()
{
    GIOChannel *in;


    struct sockaddr_in my;
    my.sin_addr.s_addr = INADDR_ANY;
    my.sin_family      = AF_INET;
    my.sin_port        = htons(3000);

    //socket initiate root socket
    int rsock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    //allow re-use address
    setsockopt(rsock,SOL_SOCKET,SO_REUSEADDR,(int*)1,sizeof(int));

    //binding
    bind(rsock,(struct sockaddr*)&my,sizeof(my));

    //listen
    listen(rsock,10);


    in = g_io_channel_unix_new(rsock);

    g_io_add_watch(in, G_IO_IN | G_IO_OUT | G_IO_HUP, (GIOFunc) deal, NULL);


    GMainLoop *loop = g_main_loop_new(NULL,FALSE); // pengganti while(1) ato gtk_main_loop
    g_main_loop_run(loop);
    return 0;
}

and it get compiled :

$ gcc -o dengersocket_glib dengersocket_glib.c `pkg-config --cflags --libs glib-2.0`

and now listening and look forward any packet data from client
and the client send the following packet :

$ echo wew | nc -v localhost 3000 
nc: connect to localhost port 3000 (tcp) failed: Connection refused
Connection to localhost 3000 port [tcp/*] succeeded!

and now the server receive following weird packet :

$ ./dengersocket_glib 
data : �o=

and my question is, where is the fault on my code ?,

1.how to get the proper packet and every single client could connect to the server ? [solved]

2.the passing data is solved, but still just could accept only one client, how to get more than one client?

  • 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-20T09:32:12+00:00Added an answer on May 20, 2026 at 9:32 am
    int recv_total;
    

    should be

    int recv_total = 0;
    

    With the random garbage value your recv_total has due to lack of initialization, you’ll also get random garbage data in buf unless recv_total just happened to be <128, and the first char in the buffer will be garbage unless recv_total happened to be 0.

    EDIT:
    Also, your accept call is wrong, you cast a size to void * but are supposed to pass a pointer to a socklen_t which should contain and receive the size of the sockaddr.

    socklen_t ss = sizeof(income);
    accept(..., &ss);
    

    Then, check the return value from accept, see that you got a valid socket.

    if (newsock == -1) {
      printf("...");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

folks, here ma code : #include <glib.h> #include <gio/gio.h> // gio channel #include <sys/socket.h>
folks, see following code : gboolean auto_refresh_host_list(bahan *unit) { //assuming unit->setting->handle_conn has initiated and
What do folks here see as the relative strengths and weaknesses of Git, Mercurial,
I wanted to see if folks were using decimal for financial applications instead of
I wanted to see what folks thought were the best way to help junior
Ok, folks. I've never encountered this before and it boggles the mind and is
Hey folks, beneath is a piece of code i used for a school assignment.
Folks, I need a solution that allows drag-and-drop cell over the table. The cells
Folks, we all know that IP blacklisting doesn't work - spammers can come in
Folks, I'm working on a little piece of rich client software that I'd like

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.