parent
afdcfdc9a8
commit
da0d4cc766
1 changed files with 18 additions and 50 deletions
@ -1,63 +1,31 @@ |
|||||||
#include <iostream> |
#include <iostream> |
||||||
#include <string> |
#include <string> |
||||||
|
|
||||||
//C socket includes
|
#include "bakanet.h" |
||||||
#include <netinet/in.h> |
using namespace Bk::Net; |
||||||
#include <stdlib.h> |
|
||||||
#include <sys/socket.h> |
|
||||||
#include <unistd.h> |
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{ |
{ |
||||||
int server_fd; |
bool running = true; |
||||||
ssize_t valread; |
|
||||||
struct sockaddr_in address; |
|
||||||
socklen_t addrlen = sizeof(address); |
|
||||||
|
|
||||||
//Socket creation step
|
|
||||||
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
{ |
|
||||||
perror("socket failed"); |
|
||||||
exit(EXIT_FAILURE); |
|
||||||
} |
|
||||||
|
|
||||||
address.sin_family = AF_INET; |
IpAddress ip("127.0.0.1"); |
||||||
address.sin_addr.s_addr = INADDR_ANY; |
Socket sock(ip, 8080, IpProtocol::TCP); |
||||||
address.sin_port = htons(80); |
running = sock.init() && sock.start(50); |
||||||
|
|
||||||
//Binding step
|
|
||||||
if (bind(server_fd, (struct sockaddr*)&address, |
|
||||||
sizeof(address)) |
|
||||||
< 0)
|
|
||||||
{ |
|
||||||
perror("bind failed"); |
|
||||||
exit(EXIT_FAILURE); |
|
||||||
} |
|
||||||
|
|
||||||
//Listening step
|
|
||||||
if (listen(server_fd, 3) < 0)
|
|
||||||
{ |
|
||||||
perror("listen"); |
|
||||||
exit(EXIT_FAILURE); |
|
||||||
} |
|
||||||
|
|
||||||
std::string msg = "HTTP/1.1 200 OK\r\n" |
std::string msg = "HTTP/1.1 200 OK\r\n" |
||||||
"Content-Type: text/html\r\n\r\n" |
"Content-Type: text/html\r\n\r\n" |
||||||
"<p>Hello World!</p>"; |
"<p>Hello World!</p>"; |
||||||
while (true)
|
std::vector<char> data(msg.begin(), msg.end()); |
||||||
{ |
|
||||||
int socket; |
while (running)
|
||||||
//Accepting connections step
|
{
|
||||||
if ((socket |
Connection conn; |
||||||
= accept(server_fd, (struct sockaddr*)&address, |
if ((conn = sock.ack()) > 0)
|
||||||
&addrlen)) |
|
||||||
> 0)
|
|
||||||
{ |
{ |
||||||
//Sending data step
|
//Sending data step
|
||||||
send(socket, msg.c_str(), msg.length(), 0); |
sock.write(conn, data); |
||||||
close(socket); |
close(conn); |
||||||
} |
} |
||||||
} |
} |
||||||
close(server_fd); |
|
||||||
return 0; |
return 0; |
||||||
} |
} |
Loading…
Reference in New Issue