Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4524738
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-05-07 09:02:44

服务端:
  1. #include<sys/types.h>
  2. #include<sys/socket.h>
  3. #include<stdio.h>
  4. #include<sys/un.h>
  5. #include<unistd.h>
  6. #include<stdlib.h>

  7. int main(int argc,char *argv[])
  8. {
  9.     int server_sockfd,client_sockfd;
  10.     int server_len,client_len;
  11.     struct sockaddr_un server_address;
  12.     struct sockaddr_un client_address;
  13.         
  14.     int i,byte;
  15.     char ch_send,ch_recv;
  16.     unlink("server_socket");  

  17.         server_sockfd=socket(AF_UNIX,SOCK_STREAM,0); // AF_INET IPV4域 AF_UNIX UNIX 域 SOCK_STREAM 可靠/双向
  18.                                                      //面向链接字节流,0 :系统自动选择 协议

  19.     server_address.sun_family=AF_UNIX;
  20.     strcpy(server_address.sun_path,"server_socket");//如果是 AF_UNIX域,需要与一个文件进行绑定 //临时文件                           //会在当前目录下,生成临时文件  server_socket
  21.     server_len=sizeof(server_address);
  22.     //绑定 ip 地址和端口
  23.     bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
  24.     //监听网络,
  25.     listen(server_sockfd,5);
  26.     printf("is watiing for client connect...\n");
  27.     
  28.     //客户端发起连接
  29.     client_len=sizeof(client_address);
  30.     client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,(socklen_t *)&client_len);    //当没有 客户端请求,accept 会阻塞
  31.     while(1)
  32.     {
  33.         if((byte=read(client_sockfd,&ch_recv,1))==-1)
  34.         {
  35.             perror("read");
  36.             exit(EXIT_FAILURE);
  37.         }
  38.         printf("receive a character from client is : %c \n",ch_recv);
  39.         //sleep(1);
  40.         
  41.     //    printf("input a charater to send :\n");
  42.         ch_send=getchar();
  43.         if((byte=write(client_sockfd,&ch_send,1))==-1)
  44.         {
  45.             perror("write");
  46.             exit(EXIT_FAILURE);
  47.         }
  48.         //printf("");
  49.         
  50.       }
  51.     close(client_sockfd);
  52.     unlink("server socket");
  53.     return 0;

  54. }/**/


客户端:

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <stdio.h>
  4. #include <sys/un.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>

  7. int main(int argc,char *argv[])
  8. {
  9.     int sockfd;
  10.     int len;
  11.     struct sockaddr_un address;
  12.     int i,byte;
  13.     char ch_recv,ch_send;

  14.     sockfd=socket(PF_UNIX,SOCK_STREAM,0);
  15.     
  16.     address.sun_family=AF_UNIX;
  17.     strcpy(address.sun_path,"server_socket");
  18.     len=sizeof(address);
  19.     connect(sockfd,(struct sockaddr*)&address,len);    
  20.     
  21.     while(1)
  22.     {
  23.     //    printf("intpur a charater to send:\n");
  24.         ch_send=getchar();
  25.         write(sockfd,&ch_send,1);
  26.         byte=read(sockfd,&ch_recv,1);
  27.         printf("receiver charater from server is %c:\n\n",ch_recv);
  28.     }
  29.         
  30.     close(sockfd);
  31.     return 0;
  32. }


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