日期:2014-05-16  浏览次数:20885 次

【下一页】linux(部分)

【下一页】linux(部分)
2012年01月09日
  一个简单的本地客户
  #include
  #include
  #include
  #include
  #include
  Int main(0
  {
  Intsockfd;
  Intlen;
  Structsockaddr_un address;
  Int result;
  Char ch =’A’;
  Sockfd = socket(AF_UNIX, SOCK_STAREAM, 0);
  Address.sun_family = AF_UNIX;
  Strcpy(address.sun_path, “server_socket”);
  Len = sizeof(address);
  Result = connect(sockfd, (struct sockaddr*)&address ,len);
  If(result = = -1) {
  Perror(“oops:client1”);
  Exir(1);
  }
  Write(sockfd, &ch, 1);
  Read(sockfd, &ch, 1);
  Printf(“char from server = %c\n”,ch);
  Close(sockfd);
  Exit(0);
  }
  一个简单的本地服务器
  #include
  #include
  #include
  #include
  #include
  Int main(0
  {
  Intserver_sockfd,client_sockfd;
  Intserver_len, client_len;
  Structsockaddr_un server_address;
  Structsockaddr_un client_address;
  Unlink(“server_socket”);
  Server_sockfd = socket(AF_UNIX,SOCK_STREAM, 0);
  Server_address.sun_family =AF_UNIX;
  Strcpy(server_address.sun_path, “server_socket”);
  Server_len = sizeof(server_address);
  Bind(server_sockfd, (struct sockaddr*)&server_address, server_len);
  Listen(server_sockfd, 5);
  While(1) {
  Charch;
  Printf(“serverwiting\n”);
  Client_len= sizeof(client_address);
  Client_sockfd= accept(server_sockfd,
  (struct sockaddr*)&client_address,&client_len);
  Read(client_sockfd,&ch,1);
  Ch++;
  Write(client_sockfd,&ch,1);
  Close(client_sockfd);
  }
  }