c++ - 启动服务器时出现 std::exception (WinSock2)

标签 c++ visual-c++ winsock2


我是 C++ 的新手,目前正在学习 tutorial如何使用 winsock(2)。
但是我的服务器遇到了问题。我不知道它是怎么发生的。

#include "stdafx.h"
#include <WinSock2.h>
#include <winsock.h>

#pragma comment(lib, "Ws2_32.lib")

using namespace std;

void cmdError(string e = ""){
    cout << "Error:\n" << e << endl;
    chrono::milliseconds t( 3000 );
    this_thread::sleep_for( t );
}

void startServer(int port){
    SOCKET acceptSocket;
    SOCKET connectedSocket;
    SOCKADDR_IN addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(port);
    addr.sin_addr.s_addr=ADDR_ANY;
    memset(&addr, 0, sizeof(SOCKADDR_IN));
    long rc;
    acceptSocket = socket(AF_INET, SOCK_STREAM, 0);
    if(acceptSocket = INVALID_SOCKET){ throw new exception();}
    rc = ::bind(acceptSocket, (SOCKADDR*) &addr, sizeof(SOCKADDR_IN));
    if(rc == SOCKET_ERROR){ throw new exception();}
    rc = listen(acceptSocket, 10);
    if(rc == SOCKET_ERROR){ throw new exception();}
    connectedSocket = accept(acceptSocket, NULL, NULL);
    if(connectedSocket == INVALID_SOCKET){ throw new exception();}
}

void startClient(string ip, int port){
    long rc;
    SOCKADDR_IN addr;
    SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
    if(s == INVALID_SOCKET){ throw new exception();}

    memset(&addr, 0, sizeof(SOCKADDR_IN));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip.c_str());
    rc = connect(s, (SOCKADDR*)&addr, sizeof(SOCKADDR));

    if(s == SOCKET_ERROR){ throw new exception();}
}

int _tmain(int argc, _TCHAR* argv[]){
    WSADATA wsa;
    if(WSAStartup(MAKEWORD(2, 0), &wsa) != 0){
        cmdError();
        return 1;
    }
    string n;
    int port;
    cout << "Please specify the task of this thread. \nEnter 'server' or 'client'." << endl;
    getline(cin, n);
    system("cls");
    cout << "Please specify a port." << endl;
    cin.clear();
    cin.sync();
    cin >> port;
    system("cls");
    if(n.at(0) == 'c' || n.at(0) == 'C'){
        string ip;
        cout << "You started a client application." << endl;
        cout << "Enter the IP address of the server you want to connect to." << endl;
        cin.clear();
        cin.sync();
        getline(cin, ip);
        system("cls");
        cmdError();
        try{
            startClient(ip, port);
        }
        catch(exception e){
            cmdError();
            e.~exception();
            return 1;
        }

        return 0;
    }
    else if(n.at(0) == 's' || n.at(0) == 'S'){
        cout << "You started a server application" << endl;
        try {
            startServer(port);
        }
        catch(exception e){
            e.~exception();
            return 1;
        }
        return 0;
    }
    cmdError();
    return 1;
}

void sendStr(string msg){
}

当我通过应用程序启动我的服务器时,我在 void startServer(int port) 中收到以下错误弹出窗口。

First-chance exception at 0x7667C42D in test.exe: Microsoft C++ exception:
std::exception at memory location 0x001EF5C8.

If there is a handler for this exception, the progrram may be safely continued.

知道为什么会这样吗?

最佳答案

你没有接住你扔的东西。您正在抛出一个 exception * 并且您正在 try catch 一个 exception

你几乎不应该抛出一个 new 异常,你应该只抛出一个异常,然后你应该捕获一个对所述异常的常量引用,例如:

#include <exception>
#include <iostream>

int
x(int y)
{
    throw std::exception();
}

int
main(int argc, char **argv)
{
    try {
        x(2);
    } catch (const std::exception &e) {
        std::cout << "Oops" << std::endl;
    }
    return 0;
}

如果我使用了你的代码,我会因为 std::exception * 类型的未捕获异常而终止程序(注意它是指向 std::exception 的指针,不是 std::exception

另外,我不知道你想用 e.~exception(); 完成什么。它在该代码中没有位置。如果你试图删除你有 newd 的异常,那么你应该 delete 它。像这样显式调用析构函数表明您在理解对象生命周期方面遇到困难;这是 C++ 语言的基础部分。

Charles Bailey's answer to this question goes into some detail on the whys and whats of this also.

关于c++ - 启动服务器时出现 std::exception (WinSock2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536264/

相关文章:

c++ - 完善的转发功能推导出冲突错误

c++ - 在动态链接库 kernel32.dll 中找不到过程入口点 RegCloseKey

c++ - C++中面向对象的客户端套接字应用程序设计

c++ - 我怎样才能让这个循环分配给 vector 的一半而不是整个 vector ?

c++ - CComBSTR::AssignBSTR() 的可能返回值是什么?

c++ - 我是否需要链接到当前库中未使用其功能的外部库?

c++ - 我的代码在终端中没有正确执行

c++ - 尝试控制Philips Hue Lights时,未从桥接器获得任何响应

c - InetPtonW 始终返回 1.0.0.0

c++ - 从类继承,在不同的命名空间中定义