我試圖通過我的家庭網絡收到一個文件,但沒有取得太大的成功。我只能收到約。提供此代碼的10個字符並且無法取得很大進展。我在服務器端的發送函數工作正常,並且99%確定這段代碼中有些事情我不理解。如果有人能夠善意地解釋我做錯了什麼,我該怎麼做纔會非常感激!我知道正確的錯誤代碼和檢查應該被添加,但只是爲了保持一切整潔,我已經包括了繁重的提升代碼。通過網絡接收C文件
//********************** CLIENT CODE BELOW(RECEIVE FILE)**
#include "StartConnection.h"
#include <pthread.h>
#include <errno.h>
#define FILENAME "/Users/name/Desktop/holaS.txt"
#define LENGTH (512)
// port number where to port in application
#define PORT_NUMBER (2005)
int clientSocket;
char buffer[LENGTH];
struct sockaddr_in serverAddr;
socklen_t addr_size;
void setup(){
clientSocket = socket(PF_INET, SOCK_STREAM, 0); //socket create
serverAddr.sin_family = AF_INET; // settings
serverAddr.sin_port = htons(PORT_NUMBER); // port connection#
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");//ip localhost
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
// bind
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
/* FOR SENDING TEXT TO CONSOLE/REVEIVE
recv(clientSocket, buffer, 1024, 0);
Print the received message
printf("Data received: %s",buffer);
*/
rec_file(); // our entry
close(clientSocket);
}
char rec_file() {
recv(clientSocket, buffer, 512, 0);
int file_size = 0;
ssize_t len;
int remain_data = 0;
file_size = atoi(buffer); //512
FILE *file;
file = fopen(FILENAME,"ab+");
if(file == NULL){
printf("File Failed To Write");
}
remain_data = file_size;
//
while(((len = recv(clientSocket,buffer, buffer, 0)) >0) && (remain_data > 0)){
fwrite(buffer, sizeof(char), len,file);
}
return 0;
}
//*********** SERVER CODE (SEND FILEBELOW*******************************************
#define PORT_NUMBER (2005)
int welcomeSocket, newSocket;
char buffer[512];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
void start_server(){
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0); // create socket
serverAddr.sin_family = AF_INET; //type
serverAddr.sin_port = htons(PORT_NUMBER);
// localhost ip
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
// bind address
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
// wait for connection max 5 connections
if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");
addr_size = sizeof serverStorage; // accept <=5
newSocket = accept(welcomeSocket, (struct sockaddr *&serverStorage, &addr_size);
printf("\n OK PRINT MESSAGE");
// char info[100];
// scanf("%s",&info);
// strcpy(buffer,info); // for writing text to client
// send(newSocket,buffer,13,0);
send_file(); // or entry function for our file send
close(welcomeSocket);
}
void send_file(){
/*
char file_name[100];
printf("Please Print Name of your File You Wish To Send");
scanf("%s",&file_name);
*/
FILE *f = fopen("/Users/name/Desktop/numbers.txt", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); //same as rewind(f);
char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);
printf("\n Sending File");
send(newSocket,string,13,0);
fclose(f);
printf("%s",string);
string[fsize] = 0;
free(string);
}
代碼缺少'buffer'和'clientSocket'的聲明和初始化。另外,在'while'中,你使用緩衝一次作爲緩衝一次來給出長度。如果緩衝區是一個數組,我認爲你需要'sizeof buffer'。除了@KamiKaze之外還有 –
;你確定服務器工作正常嗎?你從服務器發送的第一件東西是什麼?它是一個字符串「10」? – user1810087
您需要展示如何聲明'buffer'。你使用它的方式看起來很奇怪,而且很可能是錯誤的。特別是第二個'recv'呼叫奇怪。在那裏你傳入'buffer'作爲長度參數(size_t)。也許你想通過512?順便說一句:你有沒有試圖做一個簡單的'file_size'打印?你也不檢查第一個'recv'的返回值。 – 4386427