Bibliotheques TP RT  1.0
Bibliotheque de support pour TP/RT
server.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 dimercur
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
26 #include "server.h"
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 
35 #define NB_CONNECTION_MAX 1
36 
37 int socketFD = -1;
38 int clientID = -1;
39 
40 int openServer(int port) {
41  struct sockaddr_in server;
42 
43  socketFD = socket(AF_INET, SOCK_STREAM, 0);
44  if (socketFD < 0) {
45  perror("Can not create socket");
46  exit(-1);
47  }
48 
49  server.sin_addr.s_addr = INADDR_ANY;
50  server.sin_family = AF_INET;
51  server.sin_port = htons(port);
52 
53  if (bind(socketFD, (struct sockaddr *) &server, sizeof (server)) < 0) {
54  perror("Can not bind socket");
55  exit(-1);
56  }
57 
58  listen(socketFD, NB_CONNECTION_MAX);
59 
60  return socketFD;
61 }
62 
63 int closeServer() {
64  close(socketFD);
65 
66  socketFD = -1;
67 
68  return 0;
69 }
70 
71 int acceptClient() {
72  struct sockaddr_in client;
73  int c = sizeof (struct sockaddr_in);
74 
75  clientID = accept(socketFD, (struct sockaddr *) &client, (socklen_t*) & c);
76 
77  if (clientID < 0) {
78  perror("Accept failed in acceptClient");
79  exit(-1);
80  }
81 
82  return clientID;
83 }
84 
85 int sendDataToServer(char *data, int length) {
86  return sendDataToServerForClient(clientID, data, length);
87 }
88 
89 int sendDataToServerForClient(int client, char *data, int length) {
90  if (client >= 0)
91  return write(client, (void*)data, length);
92  else return 0;
93 }
94 
95 int receiveDataFromServer(char *data, int size) {
96  return receiveDataFromServerFromClient(clientID, data, size);
97 }
98 
99 int receiveDataFromServerFromClient(int client, char *data, int size) {
100  char length = 0;
101 
102  if (client > 0) {
103  if ((length = recv(client, (void*)data, size, 0)) > 0) {
104  data[length] = 0;
105  }
106  }
107 
108  return length;
109 }
110 
111 
112 
113 
int receiveDataFromServerFromClient(int client, char *data, int size)
Definition: server.cpp:99
int closeServer()
Definition: server.cpp:63
#define NB_CONNECTION_MAX
Definition: server.cpp:35
int clientID
Definition: server.cpp:38
int receiveDataFromServer(char *data, int size)
Definition: server.cpp:95
int openServer(int port)
Definition: server.cpp:40
int acceptClient()
Definition: server.cpp:71
int socketFD
Definition: server.cpp:37
int sendDataToServerForClient(int client, char *data, int length)
Definition: server.cpp:89
int sendDataToServer(char *data, int length)
Definition: server.cpp:85