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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型