开发者

gsocket (server mode) getting error?

开发者 https://www.devze.com 2023-02-11 06:33 出处:网络
i\'ve encoutered some issues again.. the script : #include <stdio.h> #include <stdlib.h> #include <gio/gio.h>

i've encoutered some issues again..

the script :

#include <stdio.h>
#include <stdlib.h>
#include <gio/gio.h>
#include <netdb.h>
/*
 * 
 */

static GSocket *sock;
    static GError *err = NULL;

void klwar()
{
    g_socket_close(so开发者_开发知识库ck,&err);
}

int main(int argc, char** argv) {


    GSocketAddress *gsockaddr;
    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(1221);
    sockaddr.sin_addr.s_addr = INADDR_ANY;

    g_type_init();
    gsockaddr = g_socket_address_new_from_native(&sockaddr,sizeof(sockaddr));

    sock = g_socket_new(G_SOCKET_FAMILY_IPV4,G_SOCKET_TYPE_STREAM,G_SOCKET_PROTOCOL_TCP,&err);

    g_socket_bind(sock,gsockaddr,TRUE,&err);

    g_socket_listen(sock,&err);

    char buff[123];

    atexit(klwar); //avoiding 

    GSocket *ja;


    while(ja = g_socket_accept(sock,NULL,&err))
    {

    gssize a = g_socket_receive(ja,buff,123,NULL,&err);
        printf("data : %s\n",buff);

    }

    return (EXIT_SUCCESS);
}

here, i intend to make TCP connection and Local Area Network, but again, i must use gnome library like GSOCKET, then it compiled, run as server, but when im tryin to send a message from 'telnet', it crashed, how to make these things work ?

and ultimately, are there any proper documentation for GSocket ?


When you receive data with g_socket_receive (), it puts the raw data in buf. Now, printf is for printing null-terminated ('\0') strings; your data in buf however is not null-terminated and makes printf crash.

See http://library.gnome.org/devel//gio/stable/GSocket.html for GSocket API reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消