When connecting (winsock2) socket i get error 10038

56 Views Asked by At

Hello, i'm coding a client with sockets (i usually do that in unix but i wanted to try in windows) with the winsock2 library. When i'm connecting the socket to an ip i get error 10038. I googled msdn and i found out that i'm doing a socket action on a non-socket. Which if i'm correct means that i'm not creating the socket or "binding" the address and ports to the socket in the right way. Tried my best but can't find any errors in my code. Thanks in advance.

This is the code:

#include <stdio.h>
#include <WinSock2.h>
#include <ws2tcpip.h>





#define okay(msg, ...) printf("[+] " msg "\n", ##__VA_ARGS__)
#define info(msg, ...) printf("[!] " msg "\n", ##__VA_ARGS__)
#define error(msg, ...) printf("[-] " msg "\n", ##__VA_ARGS__)


int main() {

    WSADATA wsa;
    SOCKET cs;
    struct sockaddr_in addr;
    char* ip = "127.0.0.1";
    int port = 4444;


    info("Initializing socket...");
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
        error("Couldn't initialize socket. Error -> %d", WSAGetLastError());
        WSACleanup();
        return EXIT_FAILURE;
    }

    okay("Succesfully initialized socket.");

    info("Creating socket...");
    if (cs = socket(AF_INET, SOCK_STREAM, 0) == INVALID_SOCKET) {
        error("Couldn't create socket. Error -> %d", WSAGetLastError());
        WSACleanup();
        return EXIT_FAILURE;
    }
    okay("Succesfully created socket.");

    addr.sin_family = AF_INET;
    InetPton(AF_INET, ip, &addr.sin_addr.s_addr);
    addr.sin_port = htons(port);

    //info("IP -> %d", addr.sin_addr.s_addr);
    //info("PORT -> %d", addr.sin_port);

    info("Connecting to -> %s:%d", ip, port);
    if (connect(cs, (SOCKADDR *) &addr, sizeof(addr)) != 0) {
        error("Couldn't connect to %s:%d, error -> %d", ip, port, WSAGetLastError());
        WSACleanup();
        return EXIT_FAILURE;
    }
    okay("Connected to %s:%d", ip, port);

    char* msg = "lol";

    int response = send(cs, msg, strlen(msg), 0);

    if(response == SOCKET_ERROR){
        error("Couldn't send message to %s:%d", ip, port);
        info("Closing socket...");
        WSACleanup();
        okay("Succesfully closed socket.");
        return EXIT_FAILURE;
    }

    okay("Succesfully sent %d bytes to %s:%d", response, ip, port);

    info("Closing socket...");
    WSACleanup();
    okay("Succesfully closed socket.");

    return EXIT_SUCCESS;
}
1

There are 1 best solutions below

0
RoGuinart On

The problem is not in the connect() call, but rather in how you store the return value of socket().

 if (cs = socket(AF_INET, SOCK_STREAM, 0) == INVALID_SOCKET)

This does not store the return value of socket() into cs and compare it to INVALID_SOCKET, but rather compares the return value of socket() with INVALID_SOCKET and stores the result of that comparison into cs. This means that, on successful calls, the result is 0 (since the socket is not invalid), and on error it is 1. These values might be valid sockets, but in all likelihood they are not the socket you created.

To solve it, you can either add parentheses or divide the line into two, like so:

// Add parentheses
if ((cs = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
...

or

// Divide the operation into two lines
cs = socket(AF_INET, SOCK_STREAM, 0);
if(cs == INVALID_SOCKET) {
...