|
/* |
|
Quick and dirty blocking http1.0 client |
|
can be built with the same script as http_server |
|
*/ |
|
#define _GNU_SOURCE |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <sched.h> |
|
#include <unistd.h> |
|
#include <sys/times.h> |
|
|
|
#include <stdint.h> |
|
#include <string.h> |
|
#include <stdarg.h> |
|
#include <errno.h> |
|
#include <netinet/in.h> |
|
#include <termios.h> |
|
#include <sys/epoll.h> |
|
|
|
#include <sys/types.h> |
|
#include <sys/socket.h> |
|
#include <arpa/inet.h> |
|
|
|
#include <netdb.h> |
|
#ifndef __linux__ |
|
#ifdef __FreeBSD__ |
|
#include <sys/socket.h> |
|
#else |
|
#include <net/socket.h> |
|
#endif |
|
#endif |
|
|
|
#include <sys/time.h> |
|
#include "anssock_intf.h" |
|
#include "ans_errno.h" |
|
|
|
|
|
#define BUFFER_SIZE 5000 |
|
#define BUFSIZE 5000 |
|
#define MAX_EVENTS 512 |
|
#define MAX_CPUS 8 |
|
|
|
#define read anssock_read |
|
#define write anssock_write |
|
#define socket anssock_socket |
|
#define connect anssock_connect |
|
#define epoll_create anssock_epoll_create |
|
#define epoll_ctl anssock_epoll_ctl |
|
#define error(x) printf(x) |
|
|
|
char *http_req = "GET / HTTP/1.0\r\n" |
|
"Connection: close\r\n" |
|
"User-Agent: test-tcp-stack\r\n" |
|
"Accept: */*\r\n" |
|
"\r\n"; |
|
|
|
int waitfd(int sockfd) { |
|
int epoll_fd; |
|
int nfds; |
|
epoll_fd = epoll_create(MAX_EVENTS); |
|
if (epoll_fd == -1) |
|
{ |
|
printf("epoll_create failed \n"); |
|
return 1; |
|
} |
|
|
|
struct epoll_event ev; |
|
struct epoll_event events[MAX_EVENTS]; |
|
ev.events = EPOLLIN | EPOLLET; |
|
ev.data.fd = sockfd; |
|
|
|
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockfd, &ev) == -1) |
|
{ |
|
printf("epll_ctl:server_sockfd register failed"); |
|
close(sockfd); |
|
close(epoll_fd); |
|
return 1; |
|
} |
|
|
|
nfds = anssock_epoll_wait(epoll_fd, events, MAX_EVENTS, -1); |
|
if (nfds == -1) { |
|
printf("start epoll_wait failed"); |
|
close(sockfd); |
|
close(epoll_fd); |
|
return 1; |
|
} |
|
} |
|
|
|
int main(int argc, char *argv[]) |
|
{ |
|
int sockfd, portno, n; |
|
struct sockaddr_in serveraddr; |
|
struct hostent *server; |
|
char *hostname; |
|
char buf[BUFSIZE]; |
|
|
|
int ret; |
|
ret = anssock_init(NULL); |
|
if (ret != 0) |
|
printf("init sock failed \n"); |
|
int core =0; |
|
/*initialize thread bind cpu*/ |
|
cpu_set_t cpus; |
|
|
|
CPU_ZERO(&cpus); |
|
CPU_SET((unsigned)core, &cpus); |
|
sched_setaffinity(0, sizeof(cpus), &cpus); |
|
|
|
/* check command line arguments */ |
|
if (argc != 3) { |
|
fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]); |
|
exit(0); |
|
} |
|
hostname = argv[1]; |
|
portno = atoi(argv[2]); |
|
|
|
|
|
/* socket: create the socket */ |
|
sockfd = socket(AF_INET, SOCK_STREAM, 0); |
|
if (sockfd < 0) |
|
printf("ERROR opening socket"); |
|
|
|
|
|
/* gethostbyname: get the server's DNS entry */ |
|
server = gethostbyname(hostname); |
|
if (server == NULL) { |
|
fprintf(stderr,"ERROR, no such host as %s\n", hostname); |
|
exit(0); |
|
} |
|
|
|
/* build the server's Internet address */ |
|
bzero((char *) &serveraddr, sizeof(serveraddr)); |
|
serveraddr.sin_family = AF_INET; |
|
bcopy((char *)server->h_addr, |
|
(char *)&serveraddr.sin_addr.s_addr, server->h_length); |
|
serveraddr.sin_port = htons(portno); |
|
|
|
/* connect: create a connection with the server */ |
|
if (connect(sockfd, &serveraddr, sizeof(serveraddr)) < 0) |
|
error("ERROR connecting"); |
|
|
|
strcpy(buf, http_req); |
|
|
|
/* send the message line to the server */ |
|
n = write(sockfd, buf, strlen(buf)); |
|
if (n < 0) |
|
error("ERROR writing to socket"); |
|
|
|
/* print the server's reply */ |
|
bzero(buf, BUFSIZE); |
|
|
|
waitfd(sockfd); |
|
n = read(sockfd, buf, 100); |
|
if (n < 0) |
|
error("ERROR reading from socket"); |
|
printf("Echo from server: %s", buf); |
|
close(sockfd); |
|
return 0; |
|
} |