winsock服务器故障排除

发布于 2024-10-04 03:14:28 字数 3048 浏览 3 评论 0原文

我编写并编译了一个服务器程序,无论客户端应用程序输入什么内容,该程序都会在屏幕上回显,代码如下。

/*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 上的本地主机时,服务器崩溃并打印出“接收错误”。是什么导致了这个问题?

Ive written and compiled a server program that is meant to echo onto the the screen whatever the client application types in, the code is as below.

/*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;
}

The whole thing compiles and runs, however when I try to use putty and connect to localhost on port 3490 the server dies and prints out "Receiving Error". what is causing this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓬勃野心 2024-10-11 03:14:28

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

  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);

Immediately after you accept the new socket into new_fd, you call closesocket on it (below). You should not do that until you are done receiving. If you want to close the listener socket at this point, you should be calling closesocket(sock_fd) instead.

  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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文