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