Advertisement
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.
Your Input Matters 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! |
||
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);
}
|
| Microsoft |
| Apple |
| Internet |
| Gamers |
| Digital Living |
| Virus & Spyware |
| Hardware |
| Software |
| ITPro |
| Developer |
| Storage |
| OS |
| Database |
| Security |
| Programming |
| Web Development |
| Networking |
| Other |
| Community Support |
| 02.10.2008 at 07:18AM PST, ID: 20861337 |
| 02.10.2008 at 07:22AM PST, ID: 20861348 |
| 02.10.2008 at 07:25AM PST, ID: 20861357 |
| 02.10.2008 at 07:26AM PST, ID: 20861360 |
| 02.10.2008 at 07:54AM PST, ID: 20861449 |
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);
}
|
| 02.10.2008 at 07:55AM PST, ID: 20861450 |
| 02.10.2008 at 02:44PM PST, ID: 20863013 |
| 02.13.2008 at 03:48AM PST, ID: 20883357 |
| 02.13.2008 at 03:50AM PST, ID: 20883363 |