Chen Hubert
Chen Hubert

Reputation: 21

C: make a global socket and call from different .c files

I am using VS2010. Is it possible to make SOCKET global so I can call this socket from all .c files in same project? Is possible, how to do it? If not possible, why not? Below are my codes:

global is where I am creating my global socket. I am using it in main.c and send.c.

global.h

extern SOCKET s;
typedef struct
{
    int x;
    double y;
    char z[32];
}Person;

main.c

#include "stdafx.h"
#include "send.f"

SOCKET s;

void printPerson(Person *p)
{
    printf("p.x: %d\n", p->x);
    printf("p.y: %f\n", p->y);
    printf("p.z: %s\n", p->z);
}

void establishConnection()
{
    //extern SOCKET s;
    struct sockaddr_in server;
    //struct addrinfo hints;
    //char *message, server_reply[2000];
    //int recv_size;

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

    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(200);

    if(connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        puts("Connect error");
        Sleep(1000);
        puts("Retrying");
        establishConnection();
    }
    else
        puts("Server Connected\n");
}



int main()
{
    char *name;
    int a;

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

    puts("Initialized.");

    establishConnection();

    {
        Person p1;
        recieveData(&p1);
        printPerson(&p1);
        printf("\n");
    }

    {
        Person p2;
        p2.x = 22;
        p2.y = 2.2;
        name = "C Plus Plus";
        strcpy(p2.z, name);
        printPerson(&p2);
        sendData(&p2);
    }

    scanf("%d", &a);
    return 0;
}

send.f

void sendData(Person*);
void recieveData(Person*);

send.c

#include "stdafx.h"

void recieveData(Person* p)
{
    //extern SOCKET s;
    char bufLen[8];
    int length, XX;
    double YY;
    char *msg, *token;
    char *ZZ;

    if(recv(s, bufLen, 8, 0) < 0)
    {
        puts("Receive failed");
        return;
    }
    length = atoi(bufLen);
    msg = malloc(length + 1);
    memset(msg, 0, length + 1);
    if (recv(s, msg, length, 0) < 0)
    {
        puts("Receive failed");
        return;
    }
    printf("Before first token: %s\n", msg);

    token = strtok(msg, ";");
    printf("After first token: %s\n", token);
    XX = atoi(token);
    printf("XX: %d\n", XX);
    p->x = atoi(token);

    token = strtok(NULL, ";");
    printf("After second token: %s\n", token);
    YY = atof(token);
    printf("YY: %f\n", YY);
    p->y = atof(token);

    token = strtok(NULL, ";");
    printf("After third token: %s\n", token);
    ZZ = malloc(strlen(token));
    memset(ZZ, 0, 32);
    strcpy(ZZ, token);
    printf("ZZ: %s\n", ZZ);

    memset(p->z, 0, 32);
    strcpy(p->z, token);

    free(msg);
    puts("Received!");
}
void sendData(Person* p)
{
    //extern SOCKET s;
    char msg[2000], clen[9];
    int len;

    //char *msg;
    //msg = malloc(2000);

    sprintf(msg, "%d;%f;%s", p->x, p->y, p->z);
    len = strlen(msg);
    sprintf(clen, "%08d", len);
    if (send(s, clen, 8, 0) < 0)
    {
        puts("Send failed");
        return;
    }
    if (send(s, msg, len, 0) < 0)
    {
        puts("Send failed");
        return;
    }
    puts("Sent!\n");
}

stdafx.h

#pragma once
#include <tchar.h>
#include "global.h"

#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library

Upvotes: 0

Views: 233

Answers (1)

melpomene
melpomene

Reputation: 85777

Your global.h is trying to use SOCKET, which is undeclared at that point. You're missing an #include (probably <winsock2.h>) in global.h.

Upvotes: 2

Related Questions