Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2143224
  • 博文数量: 391
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4156
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(391)

文章存档

2020年(6)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-11-20 17:00:56

#include
#include


gchar *buffer;


gboolean
network_read(GIOChannel *source,
            GIOCondition cond,
            gpointer data)
{
  GString *s = g_string_new(NULL);
    //error should be inited with NULL
  GError *error=NULL;
  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);
    g_object_unref(data);
    return FALSE;
    }
  else if (ret == G_IO_STATUS_EOF){
        g_print("finished\n");
        return FALSE;
    }else{
    g_print("Got: %s\n", s->str);


    //you should return this
    return TRUE;
    }


}


gboolean
new_connection(GSocketService *service,
              GSocketConnection *connection,
              GObject *source_object,
              gpointer user_data)
{
    // tell glib not to disconnect
    g_object_ref(connection);
  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);
  GIOChannel *channel = g_io_channel_unix_new(fd);
  //g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, NULL);
  g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, connection);
  return TRUE;
}


int main(int argc, char **argv) {
  GSocketService *service = g_socket_service_new();
  GInetAddress *address = g_inet_address_new_from_string("127.0.0.1");
  GSocketAddress *socket_address = g_inet_socket_address_new(address, 4000);
  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);


  GMainLoop *loop = g_main_loop_new(NULL, FALSE);
  g_main_loop_run(loop);
}
TCP客户端代码实例:
#include
#include
#include
#include


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


    GError *error = NULL;


    GSocketClient * client = g_socket_client_new();


    //GSocketConnection * connection = g_socket_client_connect_to_host (client,"10.9.27.15",5000,NULL,&error);
    GSocketConnection * connection = g_socket_client_connect_to_host (client,"localhost",9100,NULL,&error);




    if (error){
        g_error(error->message);
    }else{
        g_message("Connection ok");
    }


    guint8 buffer[]={170, 255, 255, 1, 204, 125, 128};
    guint8 incoming_buff[100]={0};


    GInputStream * in_stream = g_io_stream_get_input_stream(G_IO_STREAM(connection));
    GOutputStream * out_stream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
    g_output_stream_write(out_stream, buffer, 7, NULL, &error);
    int incoming_num = g_input_stream_read(in_stream,incoming_buff,100,NULL,&error);


    if(error)
        g_error(error->message);
    else{
        int i=0; 
        printf("incoming:\n");
        for(;i             printf("%hhX ",incoming_buff[i]);
        printf("\n");


        //g_message(incoming_buff);
    }


    return TRUE;


}


阅读(812) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~