基于Linux的socket编程模板(7)

来源:未知 责任编辑:责任编辑 发表时间:2014-01-20 07:53 点击:

#include <sys/socket.h> 
#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
 
int add_new_tcp_process_thr( ServerEnv *envp ) 

    pthread_t tcpThr; 
 
    if( pthread_create( &tcpThr,NULL,tcpServerThrFxn,envp ) ) 
    { 
        printf("tcp thread create fail!\n"); 
        return -1; 
    } 
 
    printf("tcp thread has been created!\n"); 
 
    return 0; 

 
int save_user_name( char * pUsername ) 

    printf("ok,user name saved,username=%s\n",pUsername); 
 
    return 0; 

 
int save_user_age( int age ) 

    printf("ok,user age saved,userage=%d\n",age); 
 
    return 0; 

 
void * tcpServerThrFxn( void * arg ) 

    ServerEnv * envp = (ServerEnv *)arg; 
    int socketfd = envp->m_hSocket; 
    int returnBytes; 
 
    ServerPack sPack; 
    ReturnPack rPack; 
    memcpy(rPack.cmdHeader,TCP_CMD_HEADER_STR,TCP_CMD_HEADER_LEN); 
 
    while(1) 
    { 
          // read cmd from client 
          returnBytes = recv(socketfd,(uint8_t *)&sPack,SERVER_PACK_LEN,0); 
          if( returnBytes == SERVER_PACK_LEN ) 
          { 
              //printf("ok,recv %d bytes! \n",SERVER_PACK_LEN); 
          } 
          else if( returnBytes <= 0 && errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN ) 
          { 
              printf("disconnected or error occur! errno=%d\n ",errno); 
              break; 
          } 
          else
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • /etc/fstab 详解
  • nginx主主集群
  •  LVS-NAT和LVS-DR模式的实现详解
  •  RHCE考试之磁盘配额
  • 学习笔记:ACL详解
  • suse linux双网卡双网关配置
  •  LVM逻辑卷管理器
  •  Nginx主主负载均衡架构
  • ubuntu学习Mysql

快速直达

操作系统导航

LinuxWindows虚拟机
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1