gpt4 book ai didi

c++ - ServerSocket 抛出 InvalidArgumentException,但文档没有说明原因。为什么?

转载 作者:行者123 更新时间:2023-11-28 04:15:11 26 4
gpt4 key购买 nike

我正在使用 Poco 创建网络服务器。我在使用 ServerSocket 库时遇到错误。这是重现错误的最少代码。

#include <iostream>
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketAddress.h"
#define PORT (unsigned short) 3000
int main()
{
Poco::Net::ServerSocket x;
x.bind(PORT);
Poco::Net::StreamSocket conn;
Poco::Net::SocketAddress clientAddr;
try {
conn = x.acceptConnection(clientAddr);
}
catch (Poco::InvalidArgumentException e) {
printf("Oh no! %s\n", e.displayText().c_str());
return 1;
}
printf("Huzzah!");
return 0;
}

我试图查看 [文档] ( https://pocoproject.org/docs/Poco.Net.ServerSocket.html#25093 ) 以了解错误,但它甚至没有将此函数列为抛出此错误。我也试过函数的无参数版本,它仍然抛出这个异常,(这表明它不是函数而是子函数抛出错误)。为什么?我该如何修复或解决它?

最佳答案

正如 WhozCraig 所说,问题不在于将其置于收听状态。代码应该是

#include <iostream>
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketAddress.h"
#define PORT (unsigned short) 3000
int main()
{
Poco::Net::ServerSocket x;
x.bind(PORT);
x.listen(1); // or number of acceptable connections
Poco::Net::StreamSocket conn;
Poco::Net::SocketAddress clientAddr;
try {
conn = x.acceptConnection(clientAddr);
}
catch (const Poco::InvalidArgumentException& e) {
printf("Oh no! %s\n", e.displayText().c_str());
return 1;
}
printf("Huzzah!");
return 0;
}

关于c++ - ServerSocket 抛出 InvalidArgumentException,但文档没有说明原因。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56828166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com