Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7688164
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: LINUX

2011-01-10 11:16:24

  1. #include <stdio.h>

  2. #include <sys/types.h>

  3. #include <sys/socket.h>

  4. #include <sys/un.h>

  5. #include <errno.h>

  6. #include <unistd.h>

  7. #include <signal.h>

  8. #include <sys/wait.h>

  9. #include <netdb.h>

  10. #include <pthread.h>



  11. //线程执行函数负责读写


  12. void *thr_fn(void *arg)

  13. {

  14.        int size,j;

  15.     char recv_buf[1024];

  16.        int *parg=(int *)arg;

  17.        int new_fd=*parg;

  18.        printf("new_fd=%d\n",new_fd);

  19.        while((size=read(new_fd,recv_buf,1024))>0)

  20.        {

  21.               if(recv_buf[0]=='@')

  22.                      break;

  23.               printf("Message from client(%d): %s\n",size,recv_buf);

  24.               for(j=0;j<size;j++)

  25.                      recv_buf[j]=toupper(recv_buf[j]);

  26.               write(new_fd,recv_buf,size);

  27.        }

  28.        close(new_fd);

  29.        return 0;

  30. }





  31. int main(int argc,char *argv[])

  32. {

  33.     socklen_t clt_addr_len;

  34.        int listen_fd;

  35.        int com_fd;

  36.        int ret;

  37.        int i;

  38.        static char recv_buf[1024];

  39.        int len;

  40.        int port;

  41.        pthread_t tid;



  42.        struct sockaddr_in clt_addr;

  43.        struct sockaddr_in srv_addr;



  44.        //服务器端运行时要给出端口信息,该端口为监听端口


  45.        if(argc!=2)

  46.        {

  47.            printf("Usage:%s port\n",argv[0]);

  48.               return 1;

  49.        }



  50.        //获得输入的端口


  51.        port=atoi(argv[1]);



  52.        //创建套接字用于服务器的监听


  53.        listen_fd=socket(PF_INET,SOCK_STREAM,0);

  54.        if(listen_fd<0)

  55.        {

  56.            perror("cannot create listening socket");

  57.               return 1;

  58.        }



  59.        //填充关于服务器的套节字信息


  60.        memset(&srv_addr,0,sizeof(srv_addr));

  61.        srv_addr.sin_family=AF_INET;

  62.        srv_addr.sin_addr.s_addr=htonl(INADDR_ANY);

  63.        srv_addr.sin_port=htons(port);





  64.        //将服务器和套节字绑定


  65.        ret=bind(listen_fd,(struct sockaddr *)&srv_addr,sizeof(srv_addr));

  66.        if(ret==-1)

  67.        {

  68.            perror("cannot bind server socket");

  69.               close(listen_fd);

  70.               return 1;

  71.        }



  72.        //监听指定端口,连接5个客户端


  73.        ret=listen(listen_fd,5);

  74.        if(ret==-1)

  75.        {

  76.            perror("cannot listen the client connect request");

  77.               close(listen_fd);

  78.               return 1;

  79.        }

  80.        //对每个连接来的客户端创建一个线程,单独与其进行通信


  81.        //首先调用read函数读取客户端发送来的信息


  82.        //将其转换成大写后发送回客户端


  83.        //当输入“@”时,程序退出


  84.        while(1)

  85.        {

  86.            len=sizeof(clt_addr);

  87.               com_fd=accept(listen_fd,(struct sockaddr *)&clt_addr,&len);

  88.               if(com_fd<0)

  89.               {

  90.                   if(errno==EINTR)

  91.                      {

  92.                          continue;

  93.                      }

  94.                      else

  95.                      {

  96.                          perror("cannot accept client connect request");

  97.                             close(listen_fd);

  98.                             return 1;

  99.                      }

  100.               }

  101.               printf("com_fd=%d\n",com_fd);//打印建立连接的客户端产生的套节字


  102.               if((pthread_create(&tid,NULL,thr_fn,&com_fd))==-1)

  103.               {

  104.                   perror("pthread_create error");

  105.                      close(listen_fd);

  106.                      close(com_fd);

  107.                      return 1;

  108.               }

  109.        }

  110.        return 0;

  111. }





  112. #include <stdio.h>

  113. #include <sys/types.h>

  114. #include <sys/socket.h>

  115. #include <sys/un.h>

  116. #include <netdb.h>

  117. #include <unistd.h>



  118. int main(int argc,char *argv[])

  119. {

  120.     int connect_fd;

  121.        int ret;

  122.        char snd_buf[1024];

  123.        int i;

  124.        int port;

  125.        int len;



  126.        static struct sockaddr_in srv_addr;



  127.        //客户端运行需要给出具体的连接地址和端口


  128.        if(argc!=3)

  129.        {

  130.            printf("Usage: %s server_ip_address port\n",argv[0]);

  131.               return 1;

  132.        }



  133.        //获得输入的端口


  134.        port=atoi(argv[2]);



  135.        //创建套节字用于客户端的连接


  136.        connect_fd=socket(PF_INET,SOCK_STREAM,0);

  137.        if(connect_fd<0)

  138.        {

  139.            perror("cannot create communication socket");

  140.               return 1;

  141.        }



  142.        //填充关于服务器的套节字信息


  143.        memset(&srv_addr,0,sizeof(srv_addr));

  144.        srv_addr.sin_family=AF_INET;

  145.        srv_addr.sin_addr.s_addr=inet_addr(argv[1]);

  146.        srv_addr.sin_port=htons(port);



  147.        //连接指定的服务器


  148.        ret=connect(connect_fd,(struct sockaddr *)&srv_addr,sizeof(srv_addr));

  149.        if(ret==-1)

  150.        {

  151.            perror("cannot connect to the server");

  152.               close(connect_fd);

  153.               return 1;

  154.        }



  155.        memset(snd_buf,0,1024);

  156.        //用户输入信息后,程序将输入的信息通过套接字发送给服务器


  157.        //然后调用read函数从服务器中读取发送来的信息


  158.        //当输入“@”时,程序退出


  159.        while(1)

  160.        {

  161.            write(STDOUT_FILENO,"input message:",14);

  162.               len=read(STDIN_FILENO,snd_buf,1024);

  163.               if(len>0)

  164.                      write(connect_fd,snd_buf,len);

  165.               len=read(connect_fd,snd_buf,len);

  166.               if(len>0)

  167.                      printf("Message form server: %s\n",snd_buf);

  168.               if(snd_buf[0]=='@')

  169.                      break;

  170.        }

  171.        close(connect_fd);

  172.        return 0;

  173. }
阅读(1922) | 评论(0) | 转发(4) |
0

上一篇:samba没访问权限

下一篇:UDP程序设计

给主人留下些什么吧!~~