Last active
November 8, 2017 13:11
-
-
Save ikariiin/6860c0aa541a2c65ba88473d2feb4cf8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Server.h: In member function ‘void Server::start()’: | |
Server.h:48:7: error: ‘::inet_ntoa’ has not been declared | |
::inet_ntoa(remote_addr.sin_addr), | |
^ | |
Server.h:49:7: error: ‘::nhtons’ has not been declared | |
::nhtons(remote_addr.sin_port) | |
^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See: https://stackoverflow.com/a/37435682/4332216 | |
#define _BSD_SOURCE | |
#include <iostream> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include "Server.h" | |
int main () { | |
Server server("Fuckturd"); | |
server.start(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef sockaddr_in sockaddr_in_t; | |
typedef sockaddr sockaddr_t; | |
class Server { | |
private: | |
int socketfd, newsocketfd, port; | |
socklen_t remote_addr_length; | |
char buffer[1024]; | |
sockaddr_in_t serv_addr, remote_addr; | |
void err(std::string message) { | |
std::cout << message << "\n"; | |
exit(1); | |
} | |
public: | |
Server(std::string stroke) { | |
socketfd = socket(AF_INET, SOCK_STREAM, 0); | |
if(socketfd < 0) { | |
err("Error creating INET socket."); | |
memset(&serv_addr, 0, sizeof(serv_addr)); | |
} | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_addr.s_addr = INADDR_ANY; | |
serv_addr.sin_port = htons(8080); | |
int bind_result = bind(socketfd, (sockaddr_t *) &serv_addr, sizeof(serv_addr)); | |
if(bind_result < 0) { | |
err("Error binding address to socket."); | |
} | |
} | |
void start() { | |
listen(socketfd, 2); | |
remote_addr_length = sizeof(remote_addr); | |
memset(&newsocketfd, 0, sizeof(newsocketfd)); | |
newsocketfd = accept(socketfd, (sockaddr_t *) &remote_addr, &remote_addr_length); | |
if(newsocketfd < 0) { | |
err("Error accepting incoming request"); | |
} | |
// Let's log an incoming request. | |
printf("Incoming Request From %s:%d\n", | |
::inet_ntoa(remote_addr.sin_addr), | |
::nhtons(remote_addr.sin_port) | |
); | |
memset(&buffer, 0, sizeof(buffer)); | |
int read_result = read(newsocketfd, buffer, 1023); | |
if(read_result < 0) { | |
err("Error reading data from remote."); | |
} | |
printf("Incoming Headers: %s\n", buffer); | |
close(newsocketfd); | |
close(socketfd); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment