首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >winsock服务器故障排除

winsock服务器故障排除
EN

Stack Overflow用户
提问于 2010-11-22 22:12:55
回答 1查看 161关注 0票数 0

我已经编写并编译了一个服务器程序,用于在屏幕上回显客户端应用程序键入的任何内容,代码如下所示。

代码语言:javascript
复制
/*Server */
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;

const int winsock_version = 2;
#define PORT "3490"
#define MAX_NUM_CONNECTIONS 10

int main(void){

    WSADATA wsadata;

    if (WSAStartup(MAKEWORD(winsock_version,0),&wsadata) == 0){
        cout<<"-WSAStartup Initialized." << endl;

        struct addrinfo hints,*res;
        int sock_fd;

        memset(&hints,0,sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_PASSIVE;

        if (getaddrinfo(NULL,PORT,&hints,&res) != 0){
            cout<<"-Call to getaddress was unsucceful." << endl;
        }

        if( (sock_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) == -1){
            cout<<"-Unable to Create socket." << endl;
        }

        if ( (bind(sock_fd, res->ai_addr, res->ai_addrlen)) != -1 ){
            cout<<"-binding successful." << endl;
        }

        if ( (listen(sock_fd,MAX_NUM_CONNECTIONS)) != -1){
            cout<<"-Listening for incoming connections." << endl;
        }
        //-------------------------------------------------------------

        struct sockaddr_storage incming_info;
        socklen_t sin_size;
        sin_size = sizeof incming_info;

        int new_fd;

        new_fd = accept(sock_fd, (struct sockaddr*)&incming_info,&sin_size);
        if (new_fd == -1){
            cout<<"-Accepting error." << endl;
        }
        if(new_fd == INVALID_SOCKET){
            cout<<"-INVALID SOCKET ERROR." << endl;
        }
        closesocket(new_fd);
        //-------------------------------------------------------------

        cout<<"Connected?" << endl;

        while(true){
            int ret_val;
            char buffer[128];
            ret_val = recv(new_fd,buffer,sizeof(buffer),0);
            if(ret_val == -1){
                cout<<"Receiving Error." << endl;
                break;
            }else if(ret_val == 0){
                cout<<"Connection has been closed!." << endl;
                break;
            }else{
                cout<<"---Server Echoeing what was recieved from client---" << endl;
                cout<<"Server: " << buffer << endl;

            }
            closesocket(new_fd);
        }
        cout<<"-Closing connection" << endl;

    }else{
        cout<<"-WSAStartup Initialization failed." << endl;
        if(WSACleanup()!=0){
            cout<<"-WSACleanup Successful." << endl;
        }else{
            cout<<"-WSACleanup Failed." << endl;
        }
    }
    return 0;
}

整个程序编译并运行,但是当我尝试使用putty并连接到端口3490上的localhost时,服务器死机了,并打印出"Receiving Error“。导致此问题的原因是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-22 22:16:47

在将新套接字接受到new_fd之后,立即对其调用closesocket (如下所示)。您不应该这样做,直到您完成接收。如果您想在此时关闭侦听器套接字,则应改为调用closesocket(sock_fd)

代码语言:javascript
复制
  int new_fd;

    new_fd = accept(sock_fd, (struct sockaddr*)&incming_info,&sin_size);
    if (new_fd == -1){
        cout<<"-Accepting error." << endl;
    }
    if(new_fd == INVALID_SOCKET){
        cout<<"-INVALID SOCKET ERROR." << endl;
    }
    closesocket(new_fd);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4246246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档