std::启动服务器时发生异常(WinSock2)
std::exception while starting server (WinSock2)
我是C++的新手,目前正在学习如何使用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 *
,却试图接住一个exception
。
您几乎不应该抛出new
异常,您应该只抛出一个异常,然后您应该捕获对所述异常的const引用,例如:
#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();
实现什么。它在代码中没有位置。如果你试图删除new
d的异常,那么你应该delete
d。像这样对析构函数的显式调用表明你很难理解对象的生命周期;它是CCD_ 11语言的基本组成部分。
查尔斯·贝利对这个问题的回答也详细说明了这件事的原因。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- Winsock2 + OpenSSL in C++:我如何捕获它们的异常
- std::启动服务器时发生异常(WinSock2)
- 如何检测 winsock2 中的异常断开连接
- 静态库调用错误:异常:"A null reference pointer was passed to the stub."(使用 winsock2 Win32)