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

EPOLL做高并发服务器,接收时出现数据丢失
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <time.h>

#include <fcntl.h>

#include <sys/mman.h>

#include <string.h>

#include <strings.h>

#include <errno.h>

#include <sys/epoll.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <netdb.h>

#include <pthread.h>

#include <iostream>

#include <assert.h>

#include <boost/circular_buffer.hpp>
#include <functional>
#include <algorithm>
#include <boost/lexical_cast.hpp>


using namespace std;



#define LISTENQ 5

#define OPEN_MAX 1024

#define SERV_PORT 10000

#define MAX_LINE 1024

#define INFTIM -1



#define MAXEVENTS 1000

boost::circular_buffer<string> cb(500);
boost::circular_buffer<string>::iterator it;//申明一个迭代器it

char buf[1024];

void srv_echo(int clientfd)

{

       bzero(buf,1024);

       int r=recv(clientfd,buf,sizeof(buf),0);

       if(r>0)

       {

          //处理数据

          buf[r]='\0';

          printf("来自客户端的数据:%s\n",buf);
          cb.push_back(buf);

       }else if(r==0){

                //没有可用的数据

          printf("客户退出\n");
          int sum=cb.size();
          for(it=cb.begin();it!=cb.end();it++)
          {
            cout<<*it<<endl;
          }

       }

       else if(r==-1)

       {

          //网络中断

          printf("网络故障\n");

          exit(-1);

       }

}

pthread_t t1,t2;//创建两个线程t1 t2

pthread_mutex_t m;

pthread_cond_t cond;



//线程的回调函数

void *run1(void *s)

{

  pthread_mutex_lock(&m);

  printf("我是线程1\n");

  srv_echo((int)s);

  pthread_mutex_unlock(&m);

}<