mercredi 30 janvier 2019

Cant compile Windows Socket in C

i've made in my windows desktop, a c socket code, but when i compile, it gives me erros, i dnt know how to proceed, can someone help me? i am working in windows 7, but also tried this same code in windows 10, but i got the same errors, here is the c code, i also tried in c++ but got the same errors, c code below:

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string.h>
#pragma comment (lib, "Ws2_32.lib")

#define PORT 5555
#define HOST "192.168.1.30"
#define MAX_L 4096
int main(void){
    char bfs[MAX_L], bfr[MAX_L];
    int sockfd;
    socklen_t sin_size = sizeof(struct sockaddr_in);
    struct sockaddr_in target_addr;
    struct WSAData wsa;
    WSAStartup(MAKEWORD(2,2),&wsa);

    sockfd = socket(PF_INET, SOCK_STREAM, 0);

    target_addr.sin_port = htons(PORT);
    target_addr.sin_family = AF_INET;
    target_addr.sin_addr.s_addr = inet_addr(HOST);

    connect(sockfd, (struct sockaddr *)&target_addr, sin_size);

    while(1){

        gets(bfs);
        send(sockfd, &bfs, MAX_L, 0);
        recv(sockfd, &bfr, MAX_L, 0);
        printf("%s\n", bfr);


    }

    closesocket((SOCKET) sockfd);

    WSACleanup();


}

error:

gcc -o csocketcode csocketcode.c -lwinsock2

Aucun commentaire:

Enregistrer un commentaire