server.cpp
Go to the documentation of this file.
/*
* Copyright (C) 2018 dimercur
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "server.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NB_CONNECTION_MAX 1
int socketFD = -1;
int clientID = -1;
int openServer(int port) {
struct sockaddr_in server;
socketFD = socket(AF_INET, SOCK_STREAM, 0);
if (socketFD < 0) {
perror("Can not create socket");
exit(-1);
}
server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
server.sin_port = htons(port);
if (bind(socketFD, (struct sockaddr *) &server, sizeof (server)) < 0) {
perror("Can not bind socket");
exit(-1);
}
listen(socketFD, NB_CONNECTION_MAX);
return socketFD;
}
int closeServer() {
close(socketFD);
socketFD = -1;
return 0;
}
int acceptClient() {
struct sockaddr_in client;
int c = sizeof (struct sockaddr_in);
clientID = accept(socketFD, (struct sockaddr *) &client, (socklen_t*) & c);
if (clientID < 0) {
perror("Accept failed in acceptClient");
exit(-1);
}
return clientID;
}
int sendDataToServer(char *data, int length) {
return sendDataToServerForClient(clientID, data, length);
}
int sendDataToServerForClient(int client, char *data, int length) {
if (client >= 0)
return write(client, (void*)data, length);
else return 0;
}
int receiveDataFromServer(char *data, int size) {
return receiveDataFromServerFromClient(clientID, data, size);
}
int receiveDataFromServerFromClient(int client, char *data, int size) {
char length = 0;
if (client > 0) {
if ((length = recv(client, (void*)data, size, 0)) > 0) {
data[length] = 0;
}
}
return length;
}
receiveDataFromServerFromClient
int receiveDataFromServerFromClient(int client, char *data, int size)
Definition: server.cpp:99
closeServer
int closeServer()
Definition: server.cpp:63
NB_CONNECTION_MAX
#define NB_CONNECTION_MAX
Definition: server.cpp:35
clientID
int clientID
Definition: server.cpp:38
server.h
receiveDataFromServer
int receiveDataFromServer(char *data, int size)
Definition: server.cpp:95
openServer
int openServer(int port)
Definition: server.cpp:40
acceptClient
int acceptClient()
Definition: server.cpp:71
socketFD
int socketFD
Definition: server.cpp:37
sendDataToServerForClient
int sendDataToServerForClient(int client, char *data, int length)
Definition: server.cpp:89
sendDataToServer
int sendDataToServer(char *data, int length)
Definition: server.cpp:85