forked from cbsheng/tinyhttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleclient_modified.c
More file actions
53 lines (47 loc) · 1.14 KB
/
Copy pathsimpleclient_modified.c
File metadata and controls
53 lines (47 loc) · 1.14 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUFSIZE 1024
#define quest_string_fmt "GET /%s HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Close\r\n\r\n"
int main(int argc, char *argv[])
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(4000);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if (result == -1)
{
perror("oops: client1");
exit(1);
}
char buf[1024];
if(argc == 2)
sprintf(buf,quest_string_fmt,argv[1]);
else
sprintf(buf,quest_string_fmt,"");
if(write(sockfd,buf,strlen(buf)) == -1){
perror("write");
exit(1);
}
ssize_t i=0;
while(1){
i=read(sockfd,buf,BUFSIZE-1);
if(i <= 0)
break;
buf[i]='\0';
printf(buf);
}
close(sockfd);
exit(0);
}