为什么在 strcpy_s 后我会收到“调试断言失败”的信息?
我刚刚开始学习套接字,我已经得到了这段代码,我必须使端口查找逻辑工作。但问题是我不断收到此运行时错误,我不知道为什么?
// portlookup.cpp
// Given a service name, this program displays the corresponding port number.
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
using namespace std;
int main (int argc, char **argv)
{
char service[80]; // This string contains name of desired service
struct servent *pse; // pointer to service information entry
short port; // Port # (in Network Byte Order) of desired service
if (argc < 2)
{
cout << "Please specify a service." << endl;
}
strcpy_s(service, sizeof(service), argv[1]);
WORD wVersion = 0x0202;
WSADATA wsaData;
int iResult = WSAStartup(wVersion, &wsaData); // Returns zero if successful
if (iResult != 0) {
cout << "Insufficient resources to startup WINSOCK." << endl;
return 0;
}
port = htons( (u_short) atoi(service)); // 1st try to convert string to integer
if (port == 0) { // if that doesn't work, call service function
pse = getservbyname(service,NULL);
if (pse) {
port = pse->s_port;
}
else
{
cout << "Invalid service request." << endl;
return INVALID_SOCKET;
}
}
cout << "Service: " << service << endl;
cout << "Port: " << htons(port) << endl;
}
im just starting to learn about sockets and i have been given this code, and i have to make the port lookup logic work. But the problem is i keep getting this run time error and I dont know why?
// portlookup.cpp
// Given a service name, this program displays the corresponding port number.
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
using namespace std;
int main (int argc, char **argv)
{
char service[80]; // This string contains name of desired service
struct servent *pse; // pointer to service information entry
short port; // Port # (in Network Byte Order) of desired service
if (argc < 2)
{
cout << "Please specify a service." << endl;
}
strcpy_s(service, sizeof(service), argv[1]);
WORD wVersion = 0x0202;
WSADATA wsaData;
int iResult = WSAStartup(wVersion, &wsaData); // Returns zero if successful
if (iResult != 0) {
cout << "Insufficient resources to startup WINSOCK." << endl;
return 0;
}
port = htons( (u_short) atoi(service)); // 1st try to convert string to integer
if (port == 0) { // if that doesn't work, call service function
pse = getservbyname(service,NULL);
if (pse) {
port = pse->s_port;
}
else
{
cout << "Invalid service request." << endl;
return INVALID_SOCKET;
}
}
cout << "Service: " << service << endl;
cout << "Port: " << htons(port) << endl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题似乎是您没有传递命令行,您检查 argc < 2,但是当它<2时2 无论如何你都会执行strcpy_s。
在 Visual Studio 中,转到“项目属性”对话框,从那里转到“调试”页面
并将服务名称添加到命令参数中
并修复参数检查代码
Your problem appears to be that you aren't passing a command line, you check argc < 2, but when it is < 2 you execute the strcpy_s anyway.
In Visual Studio, Got to the Project Properties dialog, from there go to the Debugging page
and add the service name to Command Arguments
And fix your argument checking code
您需要以参数开始您的程序。
strcpy_s(service, sizeof(service),argv[1]);
行假设您已经给了程序 1 参数,该参数将存储在 argv[1] 中。如果您不使用任何参数运行它,argv[1] 将为 NULL,并且您的程序将崩溃。
You need to start your program with an argument. The line
strcpy_s(service, sizeof(service),argv[1]);
assumes you've given the program 1 argument, which will be stored in argv[1].If you don't run it with any arguments, argv[1] will be NULL and your program will crash.
如果未指定参数,请确保退出。
Make sure to exit if no parameter is specified.
还,
also,