⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 echoclient-unix.c

📁 GNet是一个简单的网络库。它是目标定向的
💻 C
字号:
/* Unix Socket Echo client * Copyright (C) 2001  Mark Ferlatte * Adapted from echoclient.c, Copyright (C) 2000  David Helder * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <glib.h>#define GNET_EXPERIMENTAL 1#include <gnet.h>	/* Or <gnet/gnet.h> when installed. */typedef enum { NORMAL, ASYNC} ClientType;static void usage (int status);static void normal_echoclient (gchar* path);static void async_echoclient (gchar* path);intmain(int argc, char** argv){  ClientType client_type = NORMAL;  gnet_init ();  if (argc != 2 && argc != 3)    usage(EXIT_FAILURE);  if (argc == 3) {    if (strcmp(argv[1], "--async") == 0)      client_type = ASYNC;    else      usage(EXIT_FAILURE);  }  switch (client_type) {  case NORMAL:    g_print ("Normal echo client running\n");    normal_echoclient(argv[argc - 1]);    break;  case ASYNC:    g_print ("Asynchronous echo client running (UNFINISHED)\n");    async_echoclient(argv[argc - 1]);    break;  default:    g_assert_not_reached();  }  return 0;}static voidusage (int status){	g_print ("usage: echoclient [(nothing)|--async] <path>\n");	exit(status);}static voidnormal_echoclient(gchar* path){  GUnixSocket *socket = NULL;  GIOChannel* iochannel = NULL;  gchar buffer[1024];  guint n;  GIOError e = G_IO_ERROR_NONE;  g_assert(path != NULL);  /* Create the socket */  socket = gnet_unix_socket_new(path);  g_assert (socket != NULL);  /* Get the IOChannel */  iochannel = gnet_unix_socket_get_iochannel(socket);  g_assert (iochannel != NULL);  while (fgets(buffer, sizeof(buffer), stdin) != 0) {    n = strlen(buffer);    e = gnet_io_channel_writen(iochannel, buffer, n, &n);    if (e != G_IO_ERROR_NONE)      break;    e = gnet_io_channel_readn(iochannel, buffer, n, &n);    if (e != G_IO_ERROR_NONE)      break;    fwrite(buffer, n, 1, stdout);  }  if (e != G_IO_ERROR_NONE)     g_print ("IO error (%d)\n", e);  return;}static voidasync_echoclient(gchar *path){  g_print("Sorry, there is no async echoclient yet.\n");  return;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -