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