我正在试图弄清楚如何使用C将文件从我的服务器发送到客户端。我是C的新手,所以不确定我有多么错误:
代码段服务器:
while (1) {
len = sizeof(clientAddr);
sock = accept(serverSock, (struct sockaddr *) &clientAddr, &len);
file = fopen(filename, "rb");
while(!feof(file)) {
int flen = fread(buffer, 1, sizeof(buffer), file);
int sz = 0;
while (sz < flen) {
int sent = send(sock, &buffer[sz], flen-sz, 0);
sz += sent;
}
printf("Sent file!");
}
fclose(file);
close(sock);
}
来自客户端的代码段
int status = connect(sock, (struct sockaddr *)&Address, sizeof(Address));
int flen;
file = fopen(filename, "wb");
while(flen = recv(sock, buffer, sizeof(buffer), 0) > 0) {
bzero(buffer, sizeof(buffer));
fwrite(buffer,1, flen, file);
}
基本上,我从服务器收到“Segmentation Fault(core dump)”错误。有没有关于尝试这样做的指南?感谢任何帮助,谢谢!