ClientTCP-为什么我无法打开下载的文件? [英] ClientTCP - Why can't I open downloaded file?

查看:84
本文介绍了ClientTCP-为什么我无法打开下载的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个ClientTCP.该程序应下载一个文件.我尝试下载此图片[url] http://s.codeproject.com/App_Themes/Std/Img/logo225x90.gif [/url].当我单击该文件时,我看到一条消息,无法加载图像"logo225x90.gif".因为文件的重量为7.2KB,所以在我下载它时,它的大小为6.5KB.问题出在哪儿??代码如下:

I wrote a ClientTCP. The program should download a file. I tried to download this image [url]http://s.codeproject.com/App_Themes/Std/Img/logo225x90.gif[/url]. When I click on the file I see a message can''t load an image "logo225x90.gif". It''s werd becouse weight of the file is 7.2KB, when I download it b firefox it''s 6,5KB. Where is the problem?? Here''s the code:

#include <stdio.h>
#include <stdlib.h> //exit
#include <sys/socket.h>
#include <string.h>

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#define BUFOR 512

int main(int argc, char *argv[])
{
	int sockfd;
	int recvbytes = 0;
	int sentbytes = 0;
	struct sockaddr_in dest ;
	struct hostent *he;
	char sendbuf[128];
	char recvbuf[BUFOR];
	int recvbuflen = BUFOR;
	int headerlength = 0;
	char *division;
	FILE *fp;

	char  *servername = argv[1], *filepath = argv[2], *filename = argv[3];

	if (argc != 4) 
        {
		printf("wpisz: %s  <server> <path> <file_name>\n", argv[0]);
		exit(1);
	}
	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
		{
		perror("socket");
		exit(1);
		}

	if ((he=gethostbyname(argv[1])) == NULL) // pobierz informacje o ho´scie
		{ 
		perror("gethostbyname");
		exit(1);
		}

	dest.sin_family = AF_INET;
	dest.sin_port = htons(80);
	dest.sin_addr = *((struct in_addr *)he->h_addr);
	memset(dest.sin_zero, 0, sizeof(dest.sin_zero));    

	if (connect(sockfd, (struct sockaddr *)&dest,sizeof(dest)) == -1) 
		{
		perror("connect");
		exit(1);
		}

	printf("Connected to server :- %s\n",servername);

	// Open a file that stores the data received from server
	fp = fopen(filename, "wb");
	if (fp == NULL) 
	{
		perror("Can't open a file\n");
		exit(1);
	}

	memset(sendbuf, 0 ,sizeof(sendbuf));
	sprintf(sendbuf, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", filepath, servername);

	sentbytes = send(sockfd, sendbuf, sizeof(sendbuf), 0);
	if (sentbytes == -1) {
		perror("send() failed");
		exit(1);
	}

	recvbytes = recv(sockfd, recvbuf, recvbuflen, 0);
	if (recvbytes == -1) {
		perror("recv() failed");
		exit(1);
	}

	division = strstr(recvbuf, "\r\n"); 
	if (division != NULL  && division<recvbuf+recvbytes)>
	{
		headerlength = division - recvbuf + 2; 	
		fwrite(recvbuf + headerlength, 1, recvbytes - headerlength, fp);
	} 
	else 
	{  
		fwrite(recvbuf, 1, recvbytes, fp);
	}
	do 
	{
		recvbytes = recv(sockfd, recvbuf, recvbuflen, 0);
		if (recvbytes >0) 
		{
			fwrite(recvbuf, 1, recvbytes, fp);
		} 
		else if (recvbytes == -1)
		 {
				perror("recv()");
				exit(1);
		 }
	} while(recvbytes > 0);

	fclose(fp);
	printf("File downloade as: %s\n",filename);
	close(sockfd);
	return 0;
}
</file_name></path></server>

推荐答案

服务器将图像包装在HTTP标头中. 当我运行代码并在十六进制编辑器中打开结果时,我得到
The server wraps the image in a HTTP header.
When i run the code and open the result in a hex editor I get
Content-Type: image/gif
Last-Modified: Thu, 30 Sep 2010 21:38:26 GMT
Accept-Ranges: bytes
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Mon, 17 Jan 2011 05:39:34 GMT
Content-Length: 6695

[GIF file]



其中[GIF file]是您要追求的部分
我的每个换行符都由服务器上的\ r \ n表示,但是您也应该能够只处理\ n



Where [GIF file] is the part that you are after
Each of my line breaks are represented by a \r\n from the server, but you should be able to handle just \n as well


这篇关于ClientTCP-为什么我无法打开下载的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆