Last active
October 5, 2018 10:56
-
-
Save pplanel/0b64135e5da5b935613a251055154d73 to your computer and use it in GitHub Desktop.
My first project in C
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
#include <stdio.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include "l_logging.h" | |
#define LOG_FILE "server.log" | |
int init_logging() | |
{ | |
int file = open(LOG_FILE, O_CREAT | O_RDWR | O_APPEND, 0777); | |
if(file == -1) | |
{ | |
perror("[error][logging] error creating file"); | |
} | |
return file; | |
} | |
void | |
close_logging(int log_fd) | |
{ | |
if(log_fd > -1){ | |
close(log_fd); | |
} | |
} | |
void | |
l_info(char *message) | |
{ | |
int log_fd = init_logging(LOG_FILE); | |
write(log_fd, message, strlen(message)); | |
close_logging(&log_fd); | |
} |
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
int init_logging(char *pathname); | |
void close_logging(int log_fd); | |
void l_info(char *message); |
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
#include <unistd.h> | |
#include "server.h" | |
#define PORT 8081 | |
#define MAX_CONN 10 | |
int main(int argc, char *argv[]) | |
{ | |
struct server srv_local = make_server(PORT, MAX_CONN); | |
close(srv_local.s_socket_fd); | |
return 0; | |
} |
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
#include <stdio.h> | |
#include <netinet/tcp.h> | |
#include <netinet/in.h> | |
#include <netinet/ip.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include "server.h" | |
#include "l_logging.h" | |
#define errExit(msg) do { perror(msg); printf("\n"); exit(EXIT_FAILURE); } while(0) | |
struct server make_server(int port, int maxConn) | |
{ | |
struct server srv; | |
bzero(&srv, sizeof(struct server)); | |
// Create TCP socket | |
srv.s_socket_fd = socket(AF_INET, SOCK_STREAM, 0); | |
if(srv.s_socket_fd == -1){ | |
errExit("[socket] failed to create socket."); | |
} | |
// Bind it | |
srv.address.sin_family = AF_INET; | |
srv.address.sin_port = htons(port); | |
if(inet_aton("127.0.0.1", &srv.address.sin_addr) == 0) | |
{ | |
errExit("[error][make_server] address invalid"); | |
} | |
int yes = 1; | |
setsockopt(srv.s_socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); | |
// Listen | |
int l_ret = listen(srv.s_socket_fd, maxConn); | |
if(l_ret == -1){ | |
errExit("[listen] failed to reserve addr"); | |
} | |
l_info("[make_server] succeeded"); | |
return srv; | |
} | |
void server_loop(struct server *server){ | |
} |
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
#include <netinet/tcp.h> | |
#include <netinet/in.h> | |
#include <netinet/ip.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
struct server { | |
int s_socket_fd; | |
struct sockaddr_in address; | |
}; | |
struct peer{ | |
int s_fd; | |
struct sockaddr_in remote_addr; | |
}; | |
struct server make_server(int port, int maxConn); | |
void server_loop(struct server *server); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment