简体   繁体   中英

WinSock function - Passing argc & argv exception

I'm trying to setup a simple TCP client. I don't want the full setup and connect code in my main() so I'm trying to move it to a function but keep getting a Access Violation Error in VS2015, all includes are correct. The function is a straight copy from MSDN.

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

Calling function:

win32_connect(&argc, &argv, PORT);

Receiving function:

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

I just can't figure out a proper way to pass the arguments to the function properly so it works as it does when I have the whole function in main()

Header file (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
....

main():

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

You don't need to pass pointers to argc and argv . Just declare the function like this:

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

And you can use argc and argv normally. Then call it like this:

win32_connect(argc, argv, PORT);

EDIT:

You're passing an int for the second parameter to getaddrinfo . It expects a const char * . I don't see a definition for PORT , but I'm guessing it's a numerical constant.

So if you currently have (for example) this:

#define PORT 1234

Change it to this

#define PORT "1234"

Then define your function like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM