Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///NET.C
- #include "net.h"
- #include "log.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <pthread.h>
- /*#include <netinet/in.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <time.h>*/
- typedef enum { FALSE, TRUE } bool;
- int netSocket = -1;
- int netDomain = -1;
- int netType = -1;
- int netProtocol = -1;
- struct sockaddr_in netServerAddr;
- /* Domain Types:
- AF_UNIX, AF_LOCAL: Local
- AF_INET: IPv4
- AF_INET6: IPv6 */
- /* Socket Types:
- SOCK_STREAM: Reliable two-way */
- // Creates a Socket
- bool NetworkInit(int domain, int type, int protocol) {
- // Check if Socket is already initiated
- if(netSocket >= 0) {LogError("Socket already initiated!","NetInit"); return FALSE;}
- // Create Socket
- netSocket = socket(domain, type, protocol);
- netDomain = domain;
- netType = type;
- netProtocol = protocol;
- return (netSocket != -1);
- }
- // Resets the Socket
- bool NetworkStop() {
- // Check if Socket is initiated
- if(netSocket < 0) {LogWarning("Can't stop a nonexistant Socket","NetStop"); return FALSE;}
- // Shutdown Socket
- shutdown(netSocket, 2);
- close(netSocket);
- // Reset Socket data
- netSocket = -1;
- netDomain = -1;
- netType = -1;
- netProtocol = -1;
- }
- // Listen on Client
- void NetworkClientListen() {
- int reading = -1;
- char buffer[256];
- pid_t pid = fork();
- if(pid == 0) {
- while(1) {
- reading = recv(netSocket,buffer,sizeof(buffer)-1,0);
- buffer[reading] = '\0';
- if(reading < 0) {
- LogError("Could not read","NetClientListen");
- } else {
- onClientRecieve(buffer);
- }
- usleep(1);
- }
- }
- }
- // Connects the Client
- bool NetworkConnect(char* ip, int port) {
- // Check if Socket is initiated
- if(netSocket < 0) {LogError("Socket not yet initiated","NetConnect"); return FALSE;}
- // Setup Network for connection
- netServerAddr.sin_family = netDomain;
- netServerAddr.sin_port = htons(port);
- netServerAddr.sin_addr.s_addr = inet_addr(ip);
- // Attempt connection
- if(connect(netSocket, (struct sockaddr *)&netServerAddr, sizeof(netServerAddr)) < 0) {
- LogError("Could not connect to Server","NetConnect"); return FALSE;
- }
- // Connect
- NetworkClientListen();
- }
- // Listen on Server - Client Thread
- void *NetworkServerListenOnClient(void *vconnection) {
- int connection;
- connection = (intptr_t)vconnection;
- int reading = -1;
- char buffer[256];
- while(1) {
- reading = recv(connection,buffer,sizeof(buffer)-1,0);
- buffer[reading] = '\0';
- if(reading < 0) { // Client closed
- LogWarning("Client Disconnected","NetServerListenOnClient");
- return NULL;
- } else {
- onServerRecieve(buffer, connection);
- }
- usleep(1);
- }
- }
- // Listen on Server
- void NetworkServerListen() {
- int connection = -1;
- int reading = -1;
- char buffer[256];
- pid_t pid = fork();
- if(pid == 0) {
- while(1) {
- connection = accept(netSocket, (struct sockaddr*)NULL ,NULL);
- if(connection < 0) {
- LogError("Invalid connection","NetServerListen");
- } else {
- // The fork way
- /*pid_t pid2 = fork();
- if(pid2 == 0) {
- while(1) {
- reading = recv(connection,buffer,sizeof(buffer)-1,0);
- buffer[reading] = '\0';
- if(reading < 1) { // Client closed
- LogWarning("Client Disconnected","NetServerListen");
- exit(0);
- } else {
- onServerRecieve(buffer, connection);
- }
- usleep(1);
- }
- }*/
- // The PThread way
- pthread_t idclol;
- if(pthread_create(&idclol, NULL, NetworkServerListenOnClient, (void *)(intptr_t)connection)) {
- LogError("Could not create PThread","NetServerListen");
- }
- }
- usleep(1);
- }
- }
- }
- // Creates a Server
- bool NetworkCreate(int port) {
- // Check if Socket is initiated
- if(netSocket < 0) {LogError("Socket not yet initiated","NetCreate"); return FALSE;}
- if(port < 1025) {LogError("Cannot initialize Port number less than 1025","NetCreate"); return FALSE;}
- // Setup Network for server creation
- netServerAddr.sin_family = netDomain;
- netServerAddr.sin_port = htons(port);
- netServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- // Attempt listen
- bind(netSocket, (struct sockaddr*)&netServerAddr, sizeof(netServerAddr));
- if(listen(netSocket, 3) == -1) {
- LogError("Could not listen on server","NetCreaste"); return FALSE;
- }
- // Listen
- NetworkServerListen();
- }
- // Net String
- char *NetString(char *id, char *data) {
- char *result;
- result = malloc(strlen(id)+strlen(data)+2);
- if(result) {} else {LogWarning("ERROR","ERRRRR");}
- strcpy(result, id);
- strcat(result, "/");
- strcat(result, data);
- return result;
- }
- // Net Integer
- char* NetInt(char *id, int i) {
- char data[100];
- sprintf(data, "%d", i);
- return NetString(id, data);
- }
- // Send from Client to Server
- bool ClientSend(char *umsg) {
- char msg[256];
- strcpy(msg, umsg);
- int sent = send(netSocket, msg, strlen(msg)+1, 0);
- if(sent < 0) {
- LogError("Could not send message","ClientSend"); return FALSE;
- }
- LogWarning("Client sent",msg);
- return TRUE;
- }
- // Send from Server to Client
- bool ServerSend(char *umsg, int connection) {
- char msg[256];
- strcpy(msg, umsg);
- int sent = send(connection, msg, strlen(msg)+1, 0);
- if(sent < 0) {
- LogError("Could not send message to Client","ServerSend"); return FALSE;
- }
- LogWarning("Server sent",msg);
- return TRUE;
- }
- ///SERVER.C
- #include "net.h"
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- int main(void) {
- NetworkInit(AF_INET, SOCK_STREAM, 0);
- NetworkCreate(27015);
- while(1) {
- sleep(1);
- }
- return 0;
- }
- void onServerRecieve(char msg[256], int connection) {
- LogWarning("Server Recieved",msg);
- sleep(2);
- char *r = NetString("ch","aaaaa");
- printf("%s",r);
- printf("%s","\n");
- ServerSend(r,connection);
- }
- // Doesn't happen
- void onClientRecieve(char msg[256]) {}
- ///LOG.C
- #include "log.h"
- #include <stdio.h>
- // Colors!
- #define COLOR_ADD "\x1b[33m"
- #define COLOR_WARNING "\x1b[2m"
- #define COLOR_ERROR "\x1b[91m"
- #define COLOR_RESET "\x1b[0m"
- void LogError(char* error, char* add) {
- printf(COLOR_ERROR);
- printf("%s", "[ERROR] ");
- printf("%s", error);
- printf("%s", COLOR_ADD);
- printf("%s", " - ");
- printf("%s", add);
- printf(COLOR_RESET);
- printf("%s", "\n");
- }
- void LogWarning(char* warning, char* add) {
- printf(COLOR_WARNING);
- printf("%s", "[WARNING] ");
- printf("%s", warning);
- printf("%s", COLOR_ADD);
- printf("%s", " - ");
- printf("%s", add);
- printf(COLOR_RESET);
- printf("%s", "\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment