开发者社区> 问答> 正文

阿里云服务器收不到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)

展开
收起
xfz 2014-08-13 12:36:41 10518 0
2 条回答
写回答
取消 提交回答
  • Re阿里云服务器收不到websocket握手消息
    同求啊啊啊
    2014-09-15 11:38:08
    赞同 展开评论 打赏
  • 这个问题是不是太高大上了???



    PS:“免费”网站自动备份工具,多备份,点我签名就是啦!
    2014-08-13 13:48:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS计算与存储分离架构实践 立即下载