Page 16 of 21
Chapter 19
Page 324
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.s_addr = 0x22d8b85d;
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
return -1;
char header[] = "GET /index.html HTTP/1.1\r\nHost:example.org\r\n\r\n";
int n = write(sockfd, header, strlen(header));
char buffer[2048];
n = read(sockfd, buffer, 2048);
printf("%s", buffer);
return (EXIT_SUCCESS);
}
Page 326
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
int main(int argc, char **argv)
{
struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *servinfo;
int status = getaddrinfo("www.example.com", "80", &hints, &servinfo);
int sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
char header[] = "GET /index.html HTTP/1.1\r\nHost:example.org\r\n\r\n";
int n = write(sockfd, header, strlen(header));
char buffer[2048];
n = read(sockfd, buffer, 2048);
printf("%s", buffer);
return (EXIT_SUCCESS);
}
Page 331
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
int main(int argc, char **argv)
{
struct addrinfo hints, *server;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL, "80", &hints, &server);
int sockfd = socket(server->ai_family, server->ai_socktype | SOCK_NONBLOCK, server->ai_protocol);
bind(sockfd, server->ai_addr, server->ai_addrlen);
listen(sockfd, 10);
char buffer[2048];
char html[] = "<html><head><title>Temperature</title></head><body><p>{\"humidity\":81%, \"airtemperature\":23.5C}</p></body></html>\r\n";
char headers[1024] = {0};
char Status[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nServer:CPi\r\n";
char Date[100];
time_t now = time(NULL);
struct tm *t = gmtime(&now);
strftime(Date, sizeof(Date), "Date: %a, %d %b %Y %k:%M:%S %Z\r\n", t);
char ContLen[100] = {0};
snprintf(ContLen, sizeof ContLen, "Content-Length:%d \r\n", strlen(html));
snprintf(headers, sizeof headers, "%s%s%s\r\n", Status, Date, ContLen);
char data[2048] = {0};
snprintf(data, sizeof data, "%s%s", headers, html);
for (;;)
{
struct sockaddr_storage client_addr;
socklen_t addr_size = sizeof client_addr;
int client_fd = accept(sockfd, (struct sockaddr *)&client_addr, &addr_size);
if (client_fd > 0)
{
int n = read(client_fd, buffer, 2048);
printf("%s", buffer);
fflush(stdout);
n = write(client_fd, data, strlen(data));
close(client_fd);
}
}
return (EXIT_SUCCESS);
}