我编写了简单的客户端-服务器应用程序,使用 TCP 套接字将文件从一台机器复制到另一台机器。
在同一台机器上复制文件在验证文件的 md5sum 时成功,但在复制到远程机器时同样失败:(
本地副本
$>my_copy file.tar.gz root@127.0.0.1:/home/viswesn/file.tar.gz
$>md5sum 文件.tar.gz
199b341684f528012e44dbf13512c5fc
$>md5sum /home/viswesn/file.tar.gz
199b341684f528012e44dbf13512c5fc
远程复制
$>my_copy file.tar.gz root@blrlapx12:/home/viswesn/file.tar.gz
$>md5sum 文件.tar.gz
199b341684f528012e44dbf13512c5fc
$>md5sim /home/viswesn/file.tar.gz
d4cbf92a9d2ed632e429c69334c6bf7a
服务器端的代码
int sendFile(int sock, FILE *fp, long int size) {
int rc = -1;
char dir[DIRSIZE + 1] = {'\0'};
long int nsend = 0;
int nread = DIRSIZE;
int wc = -1;
nleft = size;
while (!feof(fp)) {
rc = fread(dir, sizeof(char), nread, fp);
nsend += rc;
if (rc > 0) {
printf("Sending %ld of %ld bytes\r", nsend, size);
wc = write(sock, dir, rc);
if (wc != rc) {
printf("failed to write to sock %d %s\n", sock, strerror(errno));
goto end;
}
}
bzero(dir, rc + 1);
}
printf("\n");
rc = 0;
end:
if (sock) {
close(sock);
}
return (rc);
}
客户端代码
int getFile(int sock, char *filename, long int startOffSet, long int size) {
char dir[DIRSIZE + 1] = {'\0'};
int rc = -1;
FILE *fp = NULL;
int cnt = 0;
int nread = DIRSIZE;
long int nrecv = 0;
int wc = 0;
long int nleft = size;
fp = fopen(filename, "w");
if (fp == NULL) {
printf("unable to open file %s %s\n", filename, strerror(errno));
} else {
printf("open file %s success\n", filename);
}
while(nleft > 0) {
if (nleft < nread) {
nread = nleft;
}
cnt = read(sock, dir, nread);
if (cnt <= 0) {
goto end;
}
nleft -= cnt;
nrecv += cnt;
dir[cnt] = '\0';
wc = write(fp, dir, cnt);
if (wc != cnt) {
printf("\nFailed to write to %d", fileno(fp));
break;
}
printf("Writing %d - [Recv : %ld] / [ Total : %ld] bytes\r", cnt, nrecv, size);
}
if (nrecv != size) {
printf("\nFailed to get file data %ld/%ld - diff of %ld\n", nrecv, size, size - nrecv);
goto end;
}
printf("\n");
rc = 0;
end:
if (fp != NULL) {
/* close descriptor for file that was sent */
printf("Closing file descriptor %d\n", fileno(fp));
fclose(fp);
}
return (rc);
}