WinSock功能 - 传递argc& argv例外

时间:2015-09-11 00:12:59

标签: c winsock getaddrinfo

我正在尝试设置一个简单的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

1 个答案:

答案 0 :(得分:3)

您无需将指针传递给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)