免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 774 | 回复: 0
打印 上一主题 下一主题

Question of Socket build by style FileNamespace!!!!!!!!!!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-10-10 16:12 |只看该作者 |倒序浏览
when i test theses functions in the code below, it report an error like :
Transport endpoint is not connected
who can tell me why ? and how to correct it? thanks!

  1. #define PATH "/tmp/DC/data"
  2. #ifdef HAVE_CONFIG_H
  3. #include <config.h>;
  4. #endif

  5. #include <stdio.h>;
  6. #include <stdlib.h>;
  7. #include <stddef.h>;
  8. #include <errno.h>;
  9. #include <stdlib.h>;
  10. #include <sys/socket.h>;
  11. #include <sys/un.h>;

  12. void  write_to_server (int filedes)
  13. {
  14.   int nbytes;
  15.   char *message = "Hey Guy!";
  16.   nbytes = write (filedes,message , 2);
  17.   if (nbytes < 0)
  18.     {
  19.       perror ("write:::::");
  20.       unlink(PATH);
  21.       exit (EXIT_FAILURE);
  22.     }
  23. }



  24. int make_named_socket (const char *filename)
  25. {
  26.   struct sockaddr_un name;
  27.   int sock;
  28.   size_t size;

  29.   /* Create the socket. */

  30.   sock = socket (PF_UNIX, SOCK_DGRAM, 0);
  31.   if (sock < 0)
  32.     {
  33.       perror ("socket");
  34.       unlink(PATH);
  35.       exit (EXIT_FAILURE);
  36.     }

  37.   /* Bind a name to the socket. */
  38. //  unlink(PATH);
  39.   name.sun_family = AF_FILE;
  40.   strcpy (name.sun_path, filename);

  41.   /* The size of the address is
  42.      the offset of the start of the filename,
  43.      plus its length,
  44.      plus one for the terminating null byte. */
  45.   size = (offsetof (struct sockaddr_un, sun_path)
  46.           + strlen (name.sun_path) + 1);

  47.   if (bind (sock, (struct sockaddr *) &name, size) < 0)
  48.     {
  49.       perror ("bind");
  50.       unlink(PATH);     
  51.       exit (EXIT_FAILURE);
  52.     }

  53.   return sock;
  54. }



  55. int main(int argc, char *argv[])
  56. {
  57.   int sock_fd;

  58.   static char *path = PATH;

  59.   sock_fd = make_named_socket(path);
  60.   write_to_server (sock_fd);

  61.   return EXIT_SUCCESS;
  62. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP