为什么在 strcpy_s 后我会收到“调试断言失败”的信息?

发布于 2024-08-20 16:22:51 字数 1532 浏览 6 评论 0原文

我刚刚开始学习套接字,我已经得到了这段代码,我必须使端口查找逻辑工作。但问题是我不断收到此运行时错误,我不知道为什么?

// 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 技术交流群。

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

发布评论

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

评论(4

醉酒的小男人 2024-08-27 16:22:51

您的问题似乎是您没有传递命令行,您检查 argc < 2,但是当它<2时2 无论如何你都会执行strcpy_s。

在 Visual Studio 中,转到“项目属性”对话框,从那里转到“调试”页面
并将服务名称添加到命令参数中

并修复参数检查代码

if (argc < 2)
{
    //cout << "Please specify a service." << endl;
    cerr << "error: no service specified." << endl;
    return EXIT_FAILURE; // return some non-zero value to indicate failure.
}

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

if (argc < 2)
{
    //cout << "Please specify a service." << endl;
    cerr << "error: no service specified." << endl;
    return EXIT_FAILURE; // return some non-zero value to indicate failure.
}
偏爱你一生 2024-08-27 16:22:51

您需要以参数开始您的程序。 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.

长亭外,古道边 2024-08-27 16:22:51

如果未指定参数,请确保退出。

if (argc < 2) 
{ 
    cout << "Please specify a service." << endl; 
    return 0; // exit! 
} 

Make sure to exit if no parameter is specified.

if (argc < 2) 
{ 
    cout << "Please specify a service." << endl; 
    return 0; // exit! 
} 
戈亓 2024-08-27 16:22:51

还,

port = htons( (u_short) atoi(service));
...
cout << htons(port);

also,

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