WinSock 函数 - 传递 argc 和 argv 异常

WinSock function - Passing argc & argv exception

我正在尝试设置一个简单的 TCP 客户端。我不希望我的 main() 中有完整的设置和连接代码,所以我试图将它移动到一个函数,但在 VS2015 中不断出现访问冲突错误,所有包含都是正确的。该函数是从 MSDN 直接复制的。

argc = 2, argv[1] = "localhost", PORT = 27015

调用函数:

win32_connect(&argc, &argv, PORT);

接收函数:

win32_connect(int *argc, char** argv[], int port)
{
    ...
    iResult = getaddrinfo(argv[1], port, &hints, &result); <- Access Violation
    ...
}

我只是想不出一个正确的方法来将参数正确地传递给函数,所以它像我在 main() 中拥有整个函数时那样工作

头文件(cHeader):

#define _CRT_SECURE_NO_WARNINGS
#ifndef CHEADER_H
#define CHEADER_H

//- Includes
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>

//- OS specific includes
#ifdef _WIN32
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Mswsock.lib")
    #pragma comment (lib, "AdvApi32.lib")
#endif
....

主要():

int main(int argc, char *argv[])
{
    //- Define variables
    int connected = 1;
    int sockfd;
    char cSend[BUF];
    char cRecv[BUF];

    //- Create socket + Connect to server
    if ((sockfd = win32_connect(argc, argv, PORT)) < 0) {
        printf("Failed to connect to server.");
        exit(1);
    }
    ...
}

cNetwork.c:

#include "cHeader.h"

#ifdef LINUX
    ...irrelevant unix code...
#endif

#ifdef _WIN32
int win32_connect(int argc, char* argv[], int port)
{
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,
        *ptr = NULL,
        hints;
    char *sendbuf = "this is a test";
    char recvbuf[BUF];
    int iResult;
    int recvbuflen = BUF;

    // Validate the parameters
    if (argc != 2) {
        //printf("usage: %s server-name\n", argv[0]);
        return 1;
    }

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo(argv[1], port, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Attempt to connect to an address until one succeeds
    for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {

        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
            ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET) {
            printf("socket failed with error: %ld\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }

        // Connect to server.
        iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
    }

    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }

    return ConnectSocket;
}
#endif

您不需要将指针传递给 argcargv。只需像这样声明函数:

int win32_connect(int argc, char *argv[], int port)

并且可以正常使用argcargv。然后这样称呼它:

win32_connect(argc, argv, PORT);

编辑:

您正在将第二个参数的 int 传递给 getaddrinfo。它需要 const char *。我没有看到 PORT 的定义,但我猜它是一个数值常量。

因此,如果您目前有(例如)这个:

#define PORT 1234

改成这样

#define PORT "1234"

然后像这样定义你的函数:

int win32_connect(int argc, char* argv[], const char *port)