ポートに接続された別のソケットにテキスト ファイルまたは画像ファイルを送信する簡単なソケット プログラムを作成しています。ただし、ファイルのサイズもクライアントソケットに送信して、受信するバイト数を認識したいと考えています。
また、ファイル自体の代わりに特定のバイト数を送信できるものを実装したいと考えています。たとえば、送信したいファイルが 14,003 バイトで、400 バイトを送信したい場合、400 バイトしか送信されません。
私はこのようなものを実装しています:
#include <stdio.h>
int main(int argc, char* argv[]) {
FILE *fp;
char* file = "text.txt";
int offset = 40;
int sendSize = 5;
int fileSize = 0;
if ((fp = fopen(file, "r")) == NULL) {
printf("Error: Cannot open the file!\n");
return 1;
} else {
/* Seek from offset into the file */
//fseek(fp, 0L, SEEK_END);
fseek(fp, offset, sendSize + offset); // seek to sendSize
fileSize = ftell(fp); // get current file pointer
//fseek(fp, 0, SEEK_SET); // seek back to beginning of file
}
printf("The size is: %d", fileSize);
}
offset
sendSize
ファイルに 40 バイトを入力し、残りのバイトを他のプログラムに送信します。
0
の代わりにの出力が得られ5
ます。この背後にある理由はありますか?