Recent Changes - Search:

Home Page



Other tools


edit SideBar

C

Client.C History

Hide minor edits - Show changes to markup

July 26, 2016, at 07:54 AM by 127.0.0.1 -
Added lines 1-15:

client.c - a very crude web browser!

client.c fetches a single file and dumps it on the screen - so hopefully it is text. Hard coded are

  • the PORT = 8181
  • the IP_ADDRESS = 192.168.0.8
  • the GET request for file /index.html

Compile with

  • cc client.c -o client

Run with:

  • ./client

Added lines 17-32:
  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <unistd.h>
  4. include <string.h>
  5. include <sys/types.h>
  6. include <sys/socket.h>
  7. include <netinet/in.h>
  8. include <arpa/inet.h>

/* YOU WILL HAVE TO CHANGE THESE TWO LINES TO MATCH YOUR CONFIG */

  1. define PORT 8181 /* Port number as an integer - web server default is 80 */
  2. define IP_ADDRESS "192.168.0.8" /* IP Address as a string */

char *command = "GET /index.html HTTP/1.0 \r\n\r\n" ; /* Note: spaces are delimiters and VERY important */

July 26, 2016, at 07:48 AM by 127.0.0.1 -
Added lines 1-36:
#define BUFSIZE 8196

pexit(char * msg)
{
	perror(msg);
	exit(1);
}

main()
{
int i,sockfd;
char buffer[BUFSIZE];
static struct sockaddr_in serv_addr;

	printf("client trying to connect to %s and port %d\n",IP_ADDRESS,PORT);
	if((sockfd = socket(AF_INET, SOCK_STREAM,0)) <0) 
		pexit("socket() failed");

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
	serv_addr.sin_port = htons(PORT);

	/* Connect tot he socket offered by the web server */
	if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <0) 
		pexit("connect() failed");

	/* Now the sockfd can be used to communicate to the server the GET request */
	printf("Send bytes=%d %s\n",strlen(command), command);
	write(sockfd, command, strlen(command));

	/* This displays the raw HTML file (if index.html) as received by the browser */
	while( (i=read(sockfd,buffer,BUFSIZE)) > 0)
		write(1,buffer,i);
}
Edit - History - Print - Recent Changes - Search
Page last modified on July 26, 2016, at 07:54 AM