I am learning tutorials online about http client programming using Linux. I found these 2 sample programs, but I am kind of confused on what command line arguments are suppose to be passed to it.
I think for program 1, string [1] should be the IP address of the url, and string [2] should be the user designed output file to store the contents of the url. Is that correct?
for program 2, I am clueless on what command line arguments we are suppose to pass to it.
Does program 2 download the contents of the webpage into another file?
/* http-client.c
*
* Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* whole or in part in accordance to the General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*****************************************************************************/
/*** http-client.c ***/
/*** ***/
/*** This program shows what the HTTP server sends to the client. First, ***/
/*** it opens a TCP socket to the server. Then, it sends the request ***/
/*** "GET <resource> HTTP/1.0\n\n" (the second newline is needed for the ***/
/*** "message-end" message. Lastly it prints out the reply. ***/
/*****************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>
#define MAXBUF 1024
void PANIC(char *msg);
#define PANIC(msg) {perror(msg); abort();}
int main(int Count, char *Strings[])
{ int sockfd, bytes_read;
struct sockaddr_in dest;
char buffer[MAXBUF];
/*---Make sure we have the right number of parameters---*/
if ( Count != 3 )
PANIC("usage: testport <IP-addr> <send-msg>\n");
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
PANIC("Socket");
/*---Initialize server address/port struct---*/
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(80); /*default HTTP Server port */
if ( inet_addr(Strings[1], &dest.sin_addr.s_addr) == 0 )
PANIC(Strings[1]);
/*---Connect to server---*/
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
PANIC("Connect");
sprintf(buffer, "GET %s HTTP/1.0\n\n", Strings[2]);
send(sockfd, buffer, strlen(buffer), 0);
/*---While there's data, read and print it---*/
do
{
bzero(buffer, sizeof(buffer));
bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
if ( bytes_read > 0 )
printf("%s", buffer);
}
while ( bytes_read > 0 );
/*---Clean up---*/
close(sockfd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#define PORT ((u_short)80)
#define BUFFER_SIZE 256
#define REQ_SIZE 60
int main(int argc, char *argv[]){
int sock,i;
int rc;
struct hostent *servhost;
struct sockaddr_in server;
char *hostname ="www.isaa.me";
char *path = "/index.html";
char request[REQ_SIZE] = "GET ";
char buffer[BUFFER_SIZE];
FILE *fin, *stdout;
i = 1;
if(argc > 1){
while(i < argc){
if (!strcmp(argv[i], "-f")){
stdout = fopen(argv[i+1], "w");
if(stdout == NULL){ perror("Check output file!"); exit(1);}
}
if (!strcmp(argv[i], "-u")){
hostname = argv[i+1];
}
if(!strcmp(argv[i], "-p")){
path = argv[i+1];
}
i++;
}
}else{
stdout = stdout;
}
strcat(request, path);
strcat(request, " HTTP/1.0\n\n");
servhost = gethostbyname(hostname);
if (servhost == NULL) {
fprintf(stderr, "%s unknown host\n", hostname);
exit(2);
}
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
memcpy(&server.sin_addr, servhost->h_addr, servhost->h_length);
server.sin_port = htons(PORT);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("connecting stream socket");
exit(1);
}
rc = 0;
rc = write(sock, request, strlen(request)+1);
if(rc<0)
{
perror("cannot send data ");
close(sock);
exit(1);
}
fin = fdopen(sock, "r");
rc = 0;
while(1){
if (fgets (buffer, sizeof(buffer), fin) == NULL) break;
if (!strcmp (buffer, "\r\n")) rc = 1;
if(rc == 1) fprintf (stdout, "%s", buffer);
}
fclose(fin);
if(stdout != stdout) fclose(stdout);
close(sock);
}