gpt4 book ai didi

c - 首先通过套接字发送我要发送的文件的大小

转载 作者:行者123 更新时间:2023-11-30 17:34:16 24 4
gpt4 key购买 nike

我想发送一个文件到program2,但首先我想发送该文件的大小。我在 program1 中有这个:

FILE *script;
int j;
int size;
char* codigo;
if (argc > 1){
for (j = 1; j < argc; j++){
if ((script = fopen(argv[j], "r")) == NULL){
perror(argv[j]);
}else{
fseek(script, 0, SEEK_END);
size = ftell(script);
fseek(script, 0, SEEK_SET);
codigo = malloc(sizeof(char)*(size +1));
if(codigo) {
fread(codigo, sizeof(char), size, script);
codigo[size] = '\0';
}
struct addrinfo hints;
struct addrinfo *serverInfo;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

getaddrinfo(ip, puerto, &hints, &serverInfo);


int serverSocket;
serverSocket = socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);

connect(serverSocket, serverInfo->ai_addr, serverInfo->ai_addrlen);
freeaddrinfo(serverInfo);

printf("Conectado al servidor\n");
send(serverSocket, codigo, strlen(codigo) + 1, 0);
close(serverSocket);
}
fclose(script);
free(codigo);
free(ip);
free(puerto);
}
}

program1 中,我将脚本的内容发送到 program2。在 program2 中我有:

#define PACKAGESIZE 100

我为recv定义了包,但是这个包大小可以是可变的,所以我需要program1发送我之后将发送的包的大小。在program2中,当我有尺寸时,我就可以收到包裹并打印它。

最佳答案

使用固定大小的整数作为文件大小,这样双方就知道要交换多少字节来换取文件大小。首先send() 大小,然后根据需要send() 数据。另一方面,recv() 首先计算大小,然后 recv() 根据需要计算数据。

例如(为简洁起见,省略了错误处理,不要忽略错误处理!):

FILE *script;
int j, k, read;
uint32_t size, size2;
char codigo[1024];
if (argc > 1)
{
for (j = 1; j < argc; j++)
{
script = fopen(argv[j], "rb");
if (script == NULL)
{
perror(argv[j]);
continue;
}

fseek(script, 0, SEEK_END);
size = ftell(script);
fseek(script, 0, SEEK_SET);

struct addrinfo hints;
struct addrinfo *serverInfo, *server;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

getaddrinfo(ip, puerto, &hints, &serverInfo);

int serverSocket = -1;

for(server = serverInfo; server != NULL; server = server->ai_next)
{
serverSocket = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
if (serverSocket != -1)
{
if (connect(serverSocket, server->ai_addr, server->ai_addrlen) == 0)
break;
close(serverSocket);
serverSocket = -1;
}
}

freeaddrinfo(serverInfo);

if (serverSocket != -1)
{
printf("Conectado al servidor\n");

size2 = htonl(size);
send(serverSocket, (char*)&size2, sizeof(size2), 0);

for(k = 0; k < size;)
{
read = fread(codigo, sizeof(char), 1024, script);
if (read < 1) break;
send(serverSocket, codigo, read, 0);
k += read;
}

close(serverSocket);
}

fclose(script);
}
}

FILE *script;
int j, read;
uint32_t size;
char codigo[1024];

int clientSocket = accept(...);
script = fopen(..., "wb");

recv(clientSocket, (char*)&size, sizeof(size), 0);
size = ntohl(size);

for(j = 0; j < size;)
{
read = recv(clientSocket, codigo, 1024, 0);
if (read < 1) break;
fwrite(codigo, sizeof(char), read, script);
j += read;
}

fclose(script);
close(clientSocket);

关于c - 首先通过套接字发送我要发送的文件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23435981/

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