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
 |     IpAddress ip("127.0.0.1"); | ||||||
| 	if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
 |     Socket sock(ip, 8080, IpProtocol::TCP); | ||||||
| 	{ |     running = sock.init() && sock.start(50); | ||||||
|         perror("socket failed"); |  | ||||||
|         exit(EXIT_FAILURE); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     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); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     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()); | ||||||
|  | 
 | ||||||
|  |     while (running) 
 | ||||||
|     {   
 |     {   
 | ||||||
|     	int socket; |         Connection conn; | ||||||
|     	//Accepting connections step
 |         if ((conn = sock.ack()) > 0) 
 | ||||||
|     	if ((socket |  | ||||||
|          = accept(server_fd, (struct sockaddr*)&address, |  | ||||||
|                   &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