-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.c
50 lines (40 loc) · 1.38 KB
/
client.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* YOU WILL HAVE TO CHANGE THESE TWO LINES TO MATCH YOUR CONFIG */
#define PORT 8181 /* Port number as an integer - web server default is 80 */
#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 */
#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);
}