Search code examples
cwinapiwinsock

WIN32 send data in socket function arg error


working on a program using WIN32 API i want the program to take data from functions that gather certain info in the system and pass argument to DbgPrint function as char and i want that char to be sent to that ip and port how to get the job done without any errors in function args
my code is just like:

#include <winsock2.h>
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
void socketfnt (auto *ipaddr, auto *portn, char*msg){
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;

printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
    printf("Failed. Error Code : %d",WSAGetLastError());
    return 1;
}

printf("Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
    printf("Could not create socket : %d" , WSAGetLastError());
}

printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr(ipaddr);
server.sin_family = AF_INET;
server.sin_port = htons( portn );

//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
    puts("connect error");
    return 1;
}

puts("Connected");
message = msg;
if( send(s , message , strlen(message) , 0) < 0)
{
    puts("Send failed");
    return 1;
}
puts("Data Send\n");

return 0;
}


void DbgPrint(char *msg){
socketfnt("192.168.1.6","4444", msg);
}

compiler show errors like

fff.c:6:23: error: storage class specified for parameter 'ipaddr'
fff.c:6:37: warning: type defaults to 'int' in declaration of 'portn' [- 
Wimplicit-int]
void socketfnt (auto *ipaddr, auto *portn, char*msg){

can you guys show me how to pass arguments to socketfnt function in a right way ,regards


Solution

  • You can't use auto for parameters to non-lambda functions. You need to use proper data types.

    You are also leaking resources.

    Try this instead:

    #include <winsock2.h>
    #include <Windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void socketfnt (const char *ipaddr, uint16_t portn, const char *msg)
    {
        WSADATA wsa;
        SOCKET s;
        struct sockaddr_in server;
    
        puts("\nInitializing Winsock...");
    
        int ret = WSAStartup(MAKEWORD(2,2), &wsa);
        if (ret != 0)
        {
            printf("Failed. Error Code : %d\n", ret);
            return 1;
        }
    
        puts("Initialized.\n");
    
        //Create a socket
        s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (s == INVALID_SOCKET)
        {
            printf("Could not create socket : %d\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }
    
        puts(Socket created.\n");
    
        memset(&server, 0, sizeof(server));
        server.sin_addr.s_addr = inet_addr(ipaddr);
        server.sin_family = AF_INET;
        server.sin_port = htons(portn);
    
        if (server.sin_addr.s_addr == INADDR_NONE)
        {
            puts("Invalid IP address.\n");
            closesocket(s);
            WSACleanup();
            return 1;
        }
    
        //Connect to remote server
        if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
        {
            printf("Could not connect socket : %d\n", WSAGetLastError());
            closesocket(s);
            WSACleanup();
            return 1;
        }
    
        puts("Connected\n");
    
        int size = strlen(msg);
        while (size > 0)
        {
            ret = send(s, msg, size, 0);
            if (ret < 0)
            {
                printf("Could not send data : %d\n", WSAGetLastError());
                closesocket(s);
                WSACleanup();
                return 1;
            }
            msg += ret;
            size -= ret;
        }
    
        puts("Data Send\n");
    
        closesocket(s);
        WSACleanup();
    
        return 0;
    }
    
    void DbgPrint(const char *msg)
    {
        socketfnt("192.168.1.6", 4444, msg);
    }