Advertisement

02.10.2008 at 06:13AM PST, ID: 23151288
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Errors in reading a binary file from socket - C for linux

Tags: C, Linux / ANSI C, Socket Programming -  HTTP Client Server in Linux C
I'm writing a simple Client program in C using sockets to transfer a file from a server also written in C which is running ( and tested correctly). The problem is, I'm able to transfer text or ASCII files from it, but not the binary files like images etc. Though after sending an mp3 file, my music player was able to play it, but I guess it must be the player's capabilities rather than my program's correctness - because i know that at least jpg and bmp files are not correctly being displayed ( actually not displayed at all :) )

I don't understand what wrong I'm doing here. Can somebody tell me?



//Here's the code:






1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
 
#define BUFFER_LENGTH 256
 
 
//Function Declarations:
unsigned long findFileSize(char* header);
char* removeHeader(char *string);
void error(char *msg);
 
 
 
int main(int argc, char*argv[])
{
	int s;
	int portnum;
	struct sockaddr_in sa;
	struct hostent *server;
	int n;
	char buffer[BUFFER_LENGTH], buffer2[BUFFER_LENGTH];
	char filename[100];
	unsigned long fileSize=0, sizeAlreadyRead=0;
	FILE *fp; 
	
 
	
	if (argc < 4) 
	{
	       fprintf(stderr,"Usage: %s <hostname> <port> <filename>\n", argv[0]);
	       exit(1);
	}
	
	
	//get host and port
	server = gethostbyname(argv[1]);
	portnum = atoi(argv[2]);
	if(portnum==0) error("Incorrect port Number given. Please give a correct input. ");
	
	
	//parse file name
	strcpy(filename, argv[3]);
	
	fp=fopen(filename, "wb");
	
	//create client socket
	s = socket(AF_INET, SOCK_STREAM, 0);
	if(s<0) error("Error opening socket");
	
	
	//Set Values
	bzero((char*)&sa, sizeof(sa));
	sa.sin_family = AF_INET;
	sa.sin_port = htons(portnum);
	
	
	//copy servers' address into the socket address. 
	bcopy((char*)server->h_addr_list[0], (char*)&sa.sin_addr.s_addr, server->h_length);
	
	
	//Try connecting to the Socket
	if(connect(s, (struct sockaddr*)&sa,sizeof(sa)) <0) error("Error connecting to socket");
	
	
	//printf("Enter the message to send to socket.");
	
	//Clear the buffer
	bzero(buffer, sizeof(buffer));
	
	
	//Create the request for file
	sprintf(buffer, "Get /%s HTTP/1.1 \n\n", filename);
	
	
	//fgets(buffer,sizeof(buffer)-1, stdin);
 
	
	
	//Send request on the socket
	n = write(s,buffer, strlen(buffer));
	if(n<0) error("Cannot write to socket.");
	
	
		//Clear buffer to read data
		bzero(buffer, sizeof(buffer));
		bzero(buffer2, sizeof(buffer2));
		
		//Read data from the socket for the first time
		n=read(s, buffer, sizeof(buffer)-1);
		if(n==-1) error("Socket read error. ");
		
		//get the file size from the header
		fileSize = findFileSize((char*)buffer);
		
		//printf("File Size read = %lu Bytes\n", fileSize);
		printf("The message read from the socket is: %s \n\n", buffer);
		
		//Remove header from the message to write into file 
		strcpy(buffer2, removeHeader((char*)buffer));
		
		printf("Message without the header: %s \n", buffer2);
		
		printf("Size of reduced message= %d", strlen(buffer2));
 
		
		sizeAlreadyRead = strlen(buffer2);
		fwrite(buffer2, sizeof(char), strlen(buffer2), fp);
		
		while(sizeAlreadyRead < fileSize-BUFFER_LENGTH + 1)
		{
			bzero(buffer, sizeof(buffer));
			n=read(s, buffer, sizeof(buffer)-1);
			if(n==-1) error("Socket read error. ");
			
			
			
			sizeAlreadyRead += sizeof(buffer)-1;
			printf("Size already read: %lu\n", sizeAlreadyRead);
			fwrite(buffer, sizeof(char), sizeof(buffer)-1, fp);
			
		}
		
		// Write the remaining bytes - which are less than BUFFER_LENGTH
		printf("Now transfering last amount of data. %lu",fileSize-sizeAlreadyRead);
		bzero(buffer, sizeof(buffer));
		n=read(s, buffer, fileSize-sizeAlreadyRead+1);
		if(n==-1) error("Socket read error. ");
		
		//printf("The message read from the socket is: %s\n", buffer);
		
		//sizeAlreadyRead += sizeof(buffer);
		fwrite(buffer, sizeof(char), fileSize-sizeAlreadyRead+1, fp);		
		
		
	
	fclose(fp);
	printf("File saved successfully!! ");
	if(n<0) error("Cannot write to socket.");
	//printf("The message read from the socket is: %s", buffer);
	
	return 0;
	
}
 
unsigned long findFileSize(char* header)
{
	unsigned long size=0;
	
	//find out the position of the substring "Content-Length: " to know the file size. 
	char* pos = strstr(header, "Content-Length: ");
	//printf("%s", pos);
	sscanf(pos, "%*s %lu", &size);
	//printf("The file size is %lu\n ", size);
	return size; 
	
}
 
char* removeHeader(char *string)
{
	char* res;
	char *pos=strstr(string, "Connection: close");
	
	//19 here is the length of "Connection: close", which needs to be counted on after the location it is found at
	//to get the remaining string. 
	int size = strlen(pos+19);
	res = (char*)malloc(size*sizeof(char));
	strcpy(res, pos+19);
	return res; 
}
 
 
void error(char *msg)
{
	perror(msg);
	exit(1);
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: piyush_soni
Solution Provided By: Infinity08
Participating Experts: 2
Solution Grade: A
Views: 63
Translate:
Loading Advertisement...
02.10.2008 at 07:18AM PST, ID: 20861337

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.10.2008 at 07:22AM PST, ID: 20861348

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.10.2008 at 07:25AM PST, ID: 20861357

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.10.2008 at 07:26AM PST, ID: 20861360

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.10.2008 at 07:54AM PST, ID: 20861449

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.10.2008 at 07:55AM PST, ID: 20861450

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.10.2008 at 02:44PM PST, ID: 20863013

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.13.2008 at 03:48AM PST, ID: 20883357

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.13.2008 at 03:50AM PST, ID: 20883363

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.10.2008 at 07:18AM PST, ID: 20861337

Rank: Sage

1) This :

        sprintf(buffer, "Get /%s HTTP/1.1 \n\n", filename);

    should be :

        sprintf(buffer, "Get /%s HTTP/1.1\r\nHost: %s\r\n\r\n", filename, argv[1]);

    to be conform the HTTP/1.1 standard. The host HAS to be specified, and lines need to be terminated with a CR/LF pair, not just an LF.


2) Instead of write :

        n = write(s,buffer, strlen(buffer));

    I'd use send :

        n = write(s, buffer, strlen(buffer), 0);

    and instead of read :

        n=read(s, buffer, sizeof(buffer)-1);

    I'd use recv :

        n = recv(s, buffer, sizeof(buffer), 0);

    Note that I used the entire buffer ... No sense in appending a null byte, especially if it's binary data. Instead of the NULL byte, you can use the double \r\n at the end of the headers to detect the end of the headers and the start of the data.


3) 256 bytes is a bit small for a receive buffer ... Better make that a bit bigger, like 4096 bytes.


4) your removeHeader function allocates memory which is never freed ... That's a memory leak !! Also, as said in point 2), instead of removeHeader, you'd better use the double \r\n to detect the start of the data.


5) you use strlen to calculate the length of the buffer. You shouldn't do that. Use the value returned by recv to know how many bytes were received, as well as the value from the Content-Length header. In case of binary data for example, it might contain zero bytes, so strlen will NOT see the entire data.



I didn't look much further, because the above are already a few problems that would cause the behavior you see. Fix all of them, and try again. If there are still problems, let me know, and I'll look further ;)
Accepted Solution
 
02.10.2008 at 07:22AM PST, ID: 20861348

Rank: Guru

Observations: -

1. (line 174) The malloc'd memory returned by removeHeader() is never free'd so this is a memory leak.
2. (line 95) The return value from read is the amount of data actually read. Apart from checking for error you just assume the amount you've read is what you expected to read.
3. (line 50) No check to see if the file open was successful
4. (line 123) Subtract n and not sizeof(buffer) -1 as you might not have read this much
5. (line 125) The amount you write should be n (the amount you read) and not sizeof(buffer), which may be greater so you'll write rubbish
6. (line 138) Same as point 5 abive

Above is a summary of some obvious issues. I think the root cause of your problem is the assumptions you are making about how much you've actually read vs. how much you write. Although in an ideal world what you request is what you'll get the reality is different. To be robust you need to make proper use of the return value from read. In essence, you are making too many assumptions (such as not checking files have been opened successfully). Can I suggest you look at resolving these issues and then if you still have a problem repost the code with the changes and we can take it from there. Meanwhile I look through the code in more detail and see if I can spot any other issues.

-Rx.
Assisted Solution
 
02.10.2008 at 07:25AM PST, ID: 20861357

Rank: Sage

>>     I'd use send :
>>
>>         n = write(s, buffer, strlen(buffer), 0);

should have been :

        n = send(s, buffer, strlen(buffer), 0);

of course lol
 
02.10.2008 at 07:26AM PST, ID: 20861360

Rank: Guru

EE could really do with a code review mechanism... like CodeStriker :)
 
02.10.2008 at 07:54AM PST, ID: 20861449

Rank: Guru

Why is this cast necessary?
I have gone through you code and annotated it with comments where I have spotted obvious issues of possible refactoring opportunities. Obviously, I can't try/test this code so I've just done the best I can by reading through it. Please note: -

(a) some of my assumptions may be wrong, so let's just say I pointed out what looks like something you need to consider
(b) I'm not claiming that the issues I've found are either a complete list or the cause of your problem -- they were just, ostensibly, obvious issues for you to look at
(c) I note that I8 has also spotted other additional things that you should also look at too.

Please feel free to discuss any of the comments/suggestions/observations I've made.

I hope this helps you.

-Rx.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
 
#define BUFFER_LENGTH 256 //<--- RX: This isn't much of a buffer, I'd go for at least 1024 bytes
 
 
//Function Declarations:
unsigned long findFileSize(char* header);
char* removeHeader(char *string);
void error(char *msg);
 
 
 
int main(int argc, char*argv[])
{
	int s;
	int portnum;
	struct sockaddr_in sa;
	struct hostent *server;
	int n;
	char buffer[BUFFER_LENGTH], buffer2[BUFFER_LENGTH];
	char filename[100];
	unsigned long fileSize=0, sizeAlreadyRead=0;
	FILE *fp; 
 
 
 
	if (argc < 4) 
	{
		fprintf(stderr,"Usage: %s <hostname> <port> <filename>\n", argv[0]);
		exit(1);
	}
 
 
	//get host and port
	server = gethostbyname(argv[1]); //<--- RX: This can fail (returns NULL), you need to check for this
	portnum = atoi(argv[2]);
	if(portnum==0) error("Incorrect port Number given. Please give a correct input. ");
 
 
	//parse file name
	strcpy(filename, argv[3]); //<--- RX: This is a potential buffer overrrun if argv[3] size > filename size
 
	fp=fopen(filename, "wb"); //<--- RX: (a) did it open (b) have you just overwritten an important file?
 
	//create client socket
	s = socket(AF_INET, SOCK_STREAM, 0);
	if(s<0) error("Error opening socket");
 
 
	//Set Values
	bzero((char*)&sa, sizeof(sa)); //<--- RX: param 1 of bzero is void * so the cast is unnecessary
	sa.sin_family = AF_INET;
	sa.sin_port = htons(portnum);
 
 
	//copy servers' address into the socket address. 
	bcopy((char*)server->h_addr_list[0], (char*)&sa.sin_addr.s_addr, server->h_length); //<--- RX: Unnecessary casts as params 1 and 2 are void *
 
 
	//Try connecting to the Socket
	if(connect(s, (struct sockaddr*)&sa,sizeof(sa)) <0) error("Error connecting to socket");
 
 
	//printf("Enter the message to send to socket.");
 
	//Clear the buffer
	bzero(buffer, sizeof(buffer));
 
 
	//Create the request for file
	sprintf(buffer, "Get /%s HTTP/1.1 \n\n", filename);
 
 
	//fgets(buffer,sizeof(buffer)-1, stdin);
 
 
 
	//Send request on the socket
	n = write(s,buffer, strlen(buffer));
	if(n<0) error("Cannot write to socket.");
 
 
	//Clear buffer to read data
	bzero(buffer, sizeof(buffer));
	bzero(buffer2, sizeof(buffer2));
 
	//Read data from the socket for the first time
	n=read(s, buffer, sizeof(buffer)-1); //<--- RX: Why -1? This is a buffer not a string -- below you use it as a string but it is not so you may end up with buffer overruns
	if(n==-1) error("Socket read error. ");
 
	//get the file size from the header
	fileSize = findFileSize((char*)buffer); //<--- RX: How do you know you've read enough, you never check n to see?
 
	//printf("File Size read = %lu Bytes\n", fileSize);
	printf("The message read from the socket is: %s \n\n", buffer); //<--- RX: Assumes buffer is null terminated; what if it's not?
 
	//Remove header from the message to write into file 
	strcpy(buffer2, removeHeader((char*)buffer)); //<--- RX: Isn't this cast unnecessary as param2 is a char*?
 
	printf("Message without the header: %s \n", buffer2); //<--- RX: Is buffer2 null terminated?
 
	printf("Size of reduced message= %d", strlen(buffer2)); //<--- RX: Is buffer2 null terminated?
 
 
	sizeAlreadyRead = strlen(buffer2); //<--- RX: Is buffer2 null terminated? You already calculated strlen above!
	fwrite(buffer2, sizeof(char), strlen(buffer2), fp); //<--- RX: Callin strlen again is unnecessarily cost, you have sizeAlreadyRead available
 
	while(sizeAlreadyRead < fileSize-BUFFER_LENGTH + 1) //<--- RX: Im a little unsure why you need -1/+1 with these buffers
	{
		bzero(buffer, sizeof(buffer));
		n=read(s, buffer, sizeof(buffer)-1);
		if(n==-1) error("Socket read error. ");
 
 
 
		sizeAlreadyRead += sizeof(buffer)-1; //<--- RX: += n would be safer since n may not be sizeof(buffer)
		printf("Size already read: %lu\n", sizeAlreadyRead);
		fwrite(buffer, sizeof(char), sizeof(buffer)-1, fp); //<--- RX: write n and not sizeof(buffer) -1 since the two may not be the same
 
	}
 
	// Write the remaining bytes - which are less than BUFFER_LENGTH
	printf("Now transfering last amount of data. %lu",fileSize-sizeAlreadyRead);
	bzero(buffer, sizeof(buffer));
	n=read(s, buffer, fileSize-sizeAlreadyRead+1);
	if(n==-1) error("Socket read error. ");
 
	//printf("The message read from the socket is: %s\n", buffer);
 
	//sizeAlreadyRead += sizeof(buffer);
	fwrite(buffer, sizeof(char), fileSize-sizeAlreadyRead+1, fp); //<--- RX: Again you assume n and amount read are the same, they may not be. You should have dome all this int he loop above until there was no more left to read!
 
 
 
	fclose(fp);
	printf("File saved successfully!! ");
	if(n<0) error("Cannot write to socket."); //<--- RX: What's this doing here?
	//printf("The message read from the socket is: %s", buffer);
 
	return 0;
 
}
 
unsigned long findFileSize(char* header)
{
	unsigned long size=0;
 
	//find out the position of the substring "Content-Length: " to know the file size. 
	char* pos = strstr(header, "Content-Length: "); //<--- RX: THis can return null, you need to test for this
	//printf("%s", pos);
	sscanf(pos, "%*s %lu", &size); //<--- RX: What if pos is null?
	//printf("The file size is %lu\n ", size);
	return size;  //<--- RX: What fi you didn't find size?
 
}
 
char* removeHeader(char *string)
{
	char* res;
	char *pos=strstr(string, "Connection: close"); //<--- RX: This can return null
 
	//19 here is the length of "Connection: close", which needs to be counted on after the location it is found at
	//to get the remaining string. 
	int size = strlen(pos+19); //<--- RX: This will crash if pos is null
	res = (char*)malloc(size*sizeof(char)); //<--- RX: This leaks!
	strcpy(res, pos+19); //<--- RX: You assume pos+19 is null terminated -- is it?
	return res;  //<--- RX: Returns memory that leaks!
}
 
 
void error(char *msg)
{
	perror(msg);
	exit(1);
}
Open in New Window
 
02.10.2008 at 07:55AM PST, ID: 20861450

Rank: Guru

>> Why is this cast necessary?
Ignore that above... it was an accidental copy/paste fopar :)
 
02.10.2008 at 02:44PM PST, ID: 20863013
Thank you friends, I was finally able to solve the problem with your tips. :)
 
02.13.2008 at 03:48AM PST, ID: 20883357
Re-opened by Asker request.

Vee_Mod
Experts Exchange Moderator
 
02.13.2008 at 03:50AM PST, ID: 20883363

Rank: Guru

Hi piyush_soni,

This has been reopened (thanks VM) so you can close it as you originally intended.

Thanks.

-Rx.
 
 
02.10.2008 at 02:50PM PST, ID: 20863034
Evilrix, I wanted to accept your solution as "Assisted Solution" but mistakingly I forgot to click 'Accept multiple solutions" it seems.... There is no way to revert back and accept your one as the supporting solution.  Any idea to do the same? :)  Thanks for your efforts !
 
 
02.10.2008 at 11:30PM PST, ID: 20864533
Hi piyush_soni,

I have requested the Q be reopened so you can close it as you intended.

Thanks.

-Rx.
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628