UPDATE - Still having error with PIPE
This commit is contained in:
parent
aa70c8d7ba
commit
f40433694c
@ -1,10 +1,10 @@
|
||||
//----- Include files ---------------------------------------------------------
|
||||
#include <stdio.h> // Needed for printf()
|
||||
#include <stdlib.h> // Needed for exit()
|
||||
#include <string.h> // Needed for strcpy() and strlen()
|
||||
#include <fcntl.h> // Needed for file i/o constants
|
||||
#include <sys/stat.h> // Needed for file i/o constants
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
|
||||
#include <sys/types.h> //
|
||||
#include <netinet/in.h> //
|
||||
@ -24,8 +24,8 @@
|
||||
#define PORT_NUM 9080 // Port number for a Web server
|
||||
|
||||
//----- Function prototypes -------------------------------------------------
|
||||
void BrowserToProxy(int server_s, int client_s);
|
||||
void ProxyToServer(int server_s, int client_s);
|
||||
void BrowserToServer(int browser_s, int server_s);
|
||||
void ServerToBrowser(int server_s, int browser_s);
|
||||
|
||||
//===== modeule main ========================================================
|
||||
int main(void)
|
||||
@ -44,7 +44,7 @@ int main(void)
|
||||
proxy_s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (proxy_s == -1)
|
||||
{
|
||||
printf("ERROR - Unable to create socket on server\n");
|
||||
std::cerr << "ERROR - Unable to create socket on server" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
proxy_addr.sin_family = AF_INET;
|
||||
@ -53,7 +53,7 @@ int main(void)
|
||||
int bindRet = bind(proxy_s, (struct sockaddr *)&proxy_addr, sizeof(proxy_addr));
|
||||
if (bindRet == -1)
|
||||
{
|
||||
printf("ERROR - Unable to bind socket\n");
|
||||
std::cerr << "ERROR - Unable to bind socket" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
// Listen for connections and then accept
|
||||
@ -67,56 +67,40 @@ int main(void)
|
||||
browser_s = accept(proxy_s, (struct sockaddr *)&browser_addr, &addr_len);
|
||||
if (browser_s == -1)
|
||||
{
|
||||
perror("ERROR - Unable to create socket to client\n");
|
||||
std::cerr << "ERROR - Unable to create socket to client" << std::endl;
|
||||
continue;
|
||||
}
|
||||
std::cout << "Got here" << std::endl;
|
||||
unsigned int server_s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
connect(server_s, (struct sockaddr *)&server_addr, addr_len);
|
||||
|
||||
std::async(std::launch::async, BrowserToProxy, browser_s, server_s);
|
||||
std::async(std::launch::async, ProxyToServer, server_s, browser_s);
|
||||
|
||||
std::async(std::launch::async, BrowserToServer, browser_s, server_s);
|
||||
std::async(std::launch::async, ServerToBrowser, server_s, browser_s);
|
||||
}
|
||||
close(browser_s);
|
||||
close(proxy_s);
|
||||
return (1);
|
||||
}
|
||||
|
||||
void BrowserToProxy(int server_s, int client_s) {
|
||||
void BrowserToServer(int browser_s, int server_s) {
|
||||
char in_buf[BUF_SIZE]; // Input buffer for GET resquest
|
||||
char out_buf[BUF_SIZE]; // Output buffer for HTML response
|
||||
char *file_name; // File name
|
||||
unsigned int fh; // File handle
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
unsigned int retcode; // Return code
|
||||
|
||||
// Receive the GET request from the Web browser
|
||||
// Pass GET along from browser to server
|
||||
do {
|
||||
buf_len = recv(server_s, in_buf, BUF_SIZE, 0);
|
||||
send(client_s, in_buf, buf_len, 0);
|
||||
buf_len = recv(browser_s, in_buf, BUF_SIZE, 0);
|
||||
send(server_s, in_buf, buf_len, 0);
|
||||
} while (buf_len != 0);
|
||||
}
|
||||
|
||||
void ProxyToServer(int server_s, int client_s) {
|
||||
/*
|
||||
|
||||
loop {
|
||||
recv from browser
|
||||
send to server
|
||||
}
|
||||
|
||||
*/
|
||||
char in_buf[BUF_SIZE]; // Input buffer for GET resquest
|
||||
void ServerToBrowser(int server_s, int browser_s) {
|
||||
char out_buf[BUF_SIZE]; // Output buffer for HTML response
|
||||
char *file_name; // File name
|
||||
unsigned int fh; // File handle
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
unsigned int retcode; // Return code
|
||||
|
||||
// Receive the GET request from the Web browser
|
||||
// Pass response along from server to browser
|
||||
do {
|
||||
buf_len = recv(server_s, in_buf, BUF_SIZE, 0);
|
||||
send(client_s, in_buf, buf_len, 0);
|
||||
buf_len = recv(server_s, out_buf, BUF_SIZE, 0);
|
||||
send(browser_s, out_buf, buf_len, 0);
|
||||
} while (buf_len != 0);
|
||||
close(server_s);
|
||||
close(client_s);
|
||||
close(browser_s);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user