Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么在strcpy\u之后调试断言失败?_C++_Sockets_Networking - Fatal编程技术网

C++ 为什么在strcpy\u之后调试断言失败?

C++ 为什么在strcpy\u之后调试断言失败?,c++,sockets,networking,C++,Sockets,Networking,我刚刚开始学习套接字,我已经得到了这段代码,我必须让端口查找逻辑工作。但问题是,我一直在得到这个运行时错误,我不知道为什么 // portlookup.cpp // Given a service name, this program displays the corresponding port number. #include <iostream> #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> us

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

// 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;

}
//portlookup.cpp
//给定服务名称,此程序将显示相应的端口号。
#包括
#pragma注释(lib,“ws2_32.lib”)
#包括
使用名称空间std;
int main(int argc,字符**argv)
{
char service[80];//此字符串包含所需服务的名称
struct servent*pse;//指向服务信息项的指针
短端口;//所需服务的端口#(按网络字节顺序)
如果(argc<2)
{

cout您需要用一个参数启动程序。行
strcpy_s(service,sizeof(service),argv[1]);
假定您已给出程序1参数,该参数将存储在argv[1]中


如果不使用任何参数运行,argv[1]将为NULL,并且程序将崩溃。

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

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

cout您的问题似乎是您没有传递命令行,您检查argc<2,但当argc<2时,您仍然执行strcpy

在VisualStudio中,转到“项目属性”对话框,然后转到“调试”页面 并将服务名称添加到命令参数中

并修复参数检查代码

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.
}
if(argc<2)
{
//也可以

port=htons((u_短)atoi(服务));
...

在第一次使用strcpy之后,它说调试断言失败了。不,我尝试了Strncpy,但它不起作用。注意:端口号的输出可能应该是
ntohs
,因为
htons
将转换为网络字节顺序。但是,在这两个分支中,端口都将以大端字节顺序。您的命令行看起来如何喜欢?我使用Visual C++,当我点击"不调试就开始,然后我得到调试断言。@Richard:不知道。在我的时代,vi是高级编辑器。调试是printf;D@John:你的意思是vi不是高级编辑器?emacs什么时候赢了?@Richard:emacs?我们没有emacs,我们有vi和ed.EXIT_失败,或者1比42生成更好的默认返回码。:@Roger:是吗“你没有正确利用我”有一个标准的退出代码吗
port = htons( (u_short) atoi(service));
...
cout << htons(port);