gpt4 book ai didi

C: 创建一个全局套接字并从不同的 .c 文件调用

转载 作者:太空宇宙 更新时间:2023-11-04 07:57:58 26 4
gpt4 key购买 nike

我正在使用 VS2010。是否可以使 SOCKET 全局化,以便我可以从同一项目中的所有 .c 文件调用此套接字?可以,怎么做?如果不可能,为什么不呢?以下是我的代码:

global 是我创建全局套接字的地方。我在 main.c 和 send.c 中使用它。

全局.h

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

主.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;
}

发送.f

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

发送.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

最佳答案

你的 global.h正在尝试使用 SOCKET ,此时未声明。你缺少一个 #include (可能是 <winsock2.h> )在 global.h 中.

关于C: 创建一个全局套接字并从不同的 .c 文件调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48751801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com