select并行处理的一个简单示例

 select并行处理的一个简单示例

环境 AIX5.5 /C

头文件 public.h

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/types.h>

#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>

客户端 sockclient.c

#include "public.h"

int main( int arg, char ** argv )
{
   struct sockaddr_in sockaddr;
   int                sockfd;
   int                n;
   char               buf[ 1024 ];

   memset( buf, 0x00, sizeof( buf ));

   sockfd = socket( AF_INET, SOCK_STREAM, 0 );
   bzero( &sockaddr, sizeof( sockaddr ));

   sockaddr.sin_family = AF_INET;
   sockaddr.sin_port = htons( 8006 );
   inet_pton( AF_INET, argv[ 1 ], &sockaddr.sin_addr );


   connect( sockfd, ( struct sockaddr * )&sockaddr, sizeof( struct sockaddr_in ));

   fprintf( stdout, "sockfd is %d/n", sockfd );

   while( 1 )
   {
 sleep( 5 );
    write( sockfd, "33", 2 );
    n = read( sockfd, buf, sizeof( buf ));

    fprintf( stderr, "buf: [%s]/n", buf );

   }
   close( sockfd );
  
   exit( 0 );
}

服务器 sockserver.c

#include "public.h"

#define      MAXNTHREADS   20

void * thread_fun( void *arg )
{
   int fd;
   fd_set writefds;
   fd_set readfds;
   fd_set exfds;
   int rc;
   struct timeval tv;
   char   buf[ 1024 ];

   fd = *( int * )arg;
   fprintf( stdout,"in thread_fun is %d/n", fd );

   FD_ZERO( &readfds );
   FD_SET( fd, &readfds);
   tv.tv_sec = 10;
   tv.tv_usec = 0;

   while( 1 )
   {
      FD_ZERO( &readfds );
      FD_SET( fd, &readfds);

      FD_ZERO( &exfds );
      FD_SET( fd, &exfds);
      tv.tv_sec = 10;
      tv.tv_usec = 0;

      fprintf( stdout," thread begin to select/n" );
      rc = select( fd + 1 , &readfds, NULL, &exfds, NULL);

      fprintf( stdout,"thread fd is %d/n",fd );
      fprintf( stdout,"thread rc is %d/n",rc );

      if( rc == 0 )
      {
      continue;
      }
      if( rc < 0 )
      {
          break;
      }
   if( FD_ISSET( fd, &exfds ))
   {
        fprintf( stdout,"fd_isset except is true /n");
        return NULL;
   }

      if( FD_ISSET( fd, &readfds ))
      {
    int nn;
          fprintf( stdout,"fd_isset read is true /n");

    nn =  read( fd, buf, 256 );

          fprintf( stdout,"fd_isset read nn is %d /n", nn );
    if( nn == 0 )
    {
   close( fd );
   return NULL ;
    }

         memcpy( buf, "hello world/n/r", 13 );
       write ( fd, buf, strlen( buf ));
    continue;
      }

      close( fd );
   return NULL;
   }
}

int main( int argc, char ** argv )
{
 
 struct sockaddr_in  sockaddr;
 struct sockaddr_in  clientsockaddr;
 int                 sockfd;
 int                 clientfd;
 socklen_t           len;
 int                 n;
 char                buf[ 1024 ];
 pthread_t         pthread[ MAXNTHREADS ];
 int                 t_id[ MAXNTHREADS ];
 int                 nthreads;
 int                 sock_fds[ MAXNTHREADS ][ 2 ];
 fd_set fds;
 int rc;
    struct timeval tv;

    nthreads = 0;
 memset( buf, 0x00, sizeof( buf ));

 sockfd = socket( AF_INET, SOCK_STREAM, 0 );

 sockaddr.sin_family = AF_INET;
 sockaddr.sin_port   = htons( 8006 );
 sockaddr.sin_addr.s_addr = htonl( INADDR_ANY );

 bind( sockfd, ( struct sockaddr * )&sockaddr, sizeof( sockaddr ));
 
    listen( sockfd, 10 );

    pthread_setconcurrency( MAXNTHREADS );

 FD_ZERO( &fds );
 FD_SET( sockfd, &fds );
 tv.tv_sec = 10;
 tv.tv_usec = 0;

    while( 1 )
 {
    fprintf( stdout, "begin to select/n" );
    FD_ZERO( &fds );
    FD_SET( sockfd, &fds );

       rc = select( sockfd + 1, &fds, NULL, NULL, NULL );

    if( rc == 0 )
    {
    fprintf( stdout, "rc == 0 /n" );
    continue;
    }
    if( rc < 0 )
    {
    fprintf( stdout, "rc < 0 /n" );
    }

    if( FD_ISSET( sockfd, &fds ))
    {
       fprintf( stdout, "begin to accept client socket/n" );
       clientfd = accept( sockfd, ( struct sockaddr * )&clientsockaddr, &len);

          if( clientfd > 0 )
          {
    int rc;
             fprintf( stdout,"create thread/n" );
    rc = pthread_create( &pthread[ nthreads ], NULL, thread_fun, &clientfd );

             if( rc == 0 )
    {
                fprintf( stdout,"main thread is %d/n", pthread[ nthreads ] );

                sock_fds[ nthreads ][ 0 ] = clientfd;
                sock_fds[ nthreads ][ 1 ] = pthread[ nthreads ];

       nthreads++;
                sock_fds[ nthreads ][ 0 ] = -1 ;
    }

     }

     }
 }
}

makefile


IFLAGS= -I $HOME

CC=cc -lpthread

EXE=sockclient
EXE1=sockserver

OBJECT=$(EXE)(sockclient.o)
OBJECT1=$(EXE1)(sockserver.o)

all:$(EXE) $(EXE1)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 第1章 Python 处理 cassandra 升级后的回滚脚本 第 2 章 多套方案来提高 python web 框架的并发处理能力 第 3 章 python 写报警程序中的声音实现 winsound 第 4 章 一个脚本讲述 python 语言的基础规范,适合初学者 第 5 章 python 计算文件的行数和读取某一行内容的实现方法 第 6 章 python 中用 string.maketrans 和 translate 巧妙替换字符串 第 7 章 python linecache 模块读取文件用法详解 第 8 章 python 调用 zabbix 的 api 接口添加主机、查询组、主机、模板 第 9 章 python+Django 实现 Nagios 自动化添加监控项目 第 10 章 通过 python 和 websocket 构建实时通信系统[扩展 saltstack 监控] 第 11 章 关于 B+tree (附 python 模拟代码) 第 12 章 Python 编写的 socket 服务器和客户端 第 13 章 python 之 MySQLdb 库的使用 第 14 章 python 监控文件或目录变化 第 15 章 Mongodb 千万级数据在 python 下的综合压力测试及应用探讨 第 16 章 通过 memcached 实现领号排队功能及 python 队列实例. 第 17 章 python 之利用 PIL 库实现页面的图片验证码及缩略图 第 18 章 如何将 Mac OS X10.9 下的 Python2.7 升级到最新的 Python3.3 第 19 章 使用 python 构建基于 hadoop 的 mapreduce 日志分析平台 第 20 章 报警监控平台扩展功能 url 回调的设计及应用 [python 语言] 第 21 章 服务端 socket 开发之多线程和 gevent 框架并发测试[python 语言] 第 22 章 利用 pypy 提高 python 脚本的执行速度及测试性能 第 23 章 python 实现 select 和 epoll 模型 socket 网络编程 第 24 章 对 Python-memcache 分布式散列和调用的实现 第 25 章 Parallel Python 实现程序的并行多 cpu 多核利用【pp 模块】 第 26 章 关于 python 调用 zabbix api 接口的自动化实例 [结合 saltstack] 第 27 章 Python 批量更新 nginx 配置文件 第 28 章 Python 通过 amqp 消息队列协议中的 Qpid 实现数据通信 第 29 章 python simplejson 模块浅谈 第 30 章 python Howto 之 logging 模块 第 31 章 Python FAQ3-python 中 的原始(raw)字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值