2 enjoy524 enjoy524 于 2016.03.16 14:06 提问

epoll异步服务端程序,客户端采用多线程访问,服务端总是返回errno 9和107 2C

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include

using namespace std;

#define MAX_EVENTS 65535
#define SERVER_PORT 8887
#define LISTEN_BACKLOG 2048

char send_buf[64] = {0};

int setnonblocking(int& sockfd)
{
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1)
{
return -1;
}
return 0;
}

void* recv_send_data(void *data)
{
int _socket = *(int *)data;
char recvBuf[16] = {0};
int recv_len = -1;
int res = 1;
while(res)
{
recv_len = recv(_socket, recvBuf, 13, 0);
if(recv_len < 0)
{
if(errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
{
continue;
}
else
{
cout << "recv error! errno: " << errno << endl;
break;
}
}
else if(recv_len == 0) //对端已正常关闭
{
res = 0;
}

    if(recv_len == sizeof(recvBuf))
    {
        res = 1;
    }
    else
    {
        res = 0;
    }
}

if(recv_len > 0)
{
    send(_socket, send_buf, strlen(send_buf), 0);
}
close(_socket);

}

int main()
{
signal(SIGPIPE,SIG_IGN);
sprintf(send_buf, "%s", "Hello world!");
struct rlimit rt;
rt.rlim_max = rt.rlim_cur = 1048576;
int epollFd = epoll_create(MAX_EVENTS);
setrlimit(RLIMIT_NOFILE, &rt);

/*******创建服务端socket,绑定、监听*******/
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if(server_socket < 0)
{
    cout << "create server socket failed!" << endl;
    return -1;
}
setnonblocking(server_socket);
int opt = 1;
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if(bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)))
{
    cout << "server bind failed!" << endl; 
    return -1;
}

if(listen(server_socket, LISTEN_BACKLOG))
{
    cout << "server listen failed" << endl;
    return -1;
}

/**************向epollFd添加fd监听事件**************/
struct epoll_event server_ev;
server_ev.events = EPOLLIN | EPOLLET;
server_ev.data.fd = server_socket;
if(-1 == epoll_ctl(epollFd, EPOLL_CTL_ADD, server_socket, &server_ev))
{
    cout << "epoll_ctl server socket failed" << endl;
    return -1;
}

while(true)
{
    struct epoll_event events[MAX_EVENTS];
    int nfds = epoll_wait(epollFd, events, MAX_EVENTS, -1);
    if(nfds < 0)
    {
        cout << "epoll_wait failed" << endl;
        return -1;
    }
    for(int i = 0; i < nfds; ++i)
    {
        if(events[i].data.fd == server_socket)
        {
            struct sockaddr_in clientAddr;
            socklen_t length = sizeof(clientAddr);
            int remote_socket = accept(events[i].data.fd, (struct sockaddr*)&clientAddr, &length);
            if(remote_socket < 0)
            {
                cout << "accept socket failed!" << endl;
                continue;
            }
            cout << "socket connect successfully" << endl;
            setnonblocking(remote_socket);
            struct epoll_event client_ev;
            client_ev.data.fd = remote_socket;
            client_ev.events = EPOLLIN | EPOLLET;
            if(-1 == epoll_ctl(epollFd, EPOLL_CTL_ADD, remote_socket, &client_ev))
            {
                cout << "epoll_ctl client socket failed" << endl;
                return -1;
            }
        }
        else
        {
            pthread_t thread;
            pthread_attr_t attr;
            pthread_attr_init(&attr);
            pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
            pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

            if(pthread_create(&thread, &attr, recv_send_data, (void*)&(events[i].data.fd)))
            {
                cout << "create thread failed" << endl;
                return -1;
            }
        }
    }
}
close(server_socket);
cout << "abort" << endl;
return 0;

}

1个回答

mianbaohebg
mianbaohebg   2016.12.17 22:58

如何修改驱动参数使之变成竖屏。通过修改注册表只能把显示调整成竖屏,即LCD旋转90度,但是触摸点的位置还是原来的。所以真能通过调整驱动来实

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!