xfzztd 2014-08-13 04:27
浏览 4330

阿里云服务器收不到websocket握手消息

代码如下,这段代码在我的vm虚拟机里可以正常运行,服务端能收到握手消息。但把代码放到阿里云服务器上时就,能收到accept请求,就是没触发read事件。有没谁遇到过相同的问题,求解啊!
是不是阿里云服务器的环境哪有问题,还是有哪个库有问题。

服务端代码:

    for (n = 0; n < nfds; ++n)
    {
        if (events[n].data.fd == listenfd) 
        {
            connfd = accept(listenfd, (struct sockaddr *)&cliaddr,&socklen);
            if (connfd < 0) 
            {
                perror("accept error");
                continue;
            }

            sprintf(buf, "accept form %s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
            printf("%d:%s", ++acceptCount, buf);

            if (curfds >= MAXEPOLLSIZE) {
                fprintf(stderr, "too many connection, more than %d\n", MAXEPOLLSIZE);
                close(connfd);
                continue;
            } 
            if (setnonblocking(connfd) < 0) {
                perror("setnonblocking error");
            }
            ev.events = EPOLLIN | EPOLLET;
            ev.data.fd = connfd;
            if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, connfd, &ev) < 0)
            {
                fprintf(stderr, "add socket '%d' to epoll failed: %s\n", connfd, strerror(errno));
                return -1;
            }
            curfds++;
            continue;
        } 
        // 处理客户端请求
        if (handle(events[n].data.fd) < 0) {
            epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);
            curfds--;


        }
    }
}
int handle(int connfd) {
    int nread;
    char buf[MAXLINE];
    nread = read(connfd, buf, MAXLINE);//读取客户端socket流

    if (nread == 0) {
        printf("client close the connection\n");
        close(connfd);
        return -1;
    } 
    if (nread < 0) {
        perror("read error");
        close(connfd);
        return -1;
    }    
    buf[nread] = '\0';
    printf("received:%s\n",buf);
    write(connfd, buf, nread);//响应客户端  
    return 0;
}

客户端就是new WebSocket(url)

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算