server_tcp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * File: server_tcp.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 14 novembre 2018
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include "error.h"
  13. #include "server.h"
  14. /* --- Extern --- */
  15. extern int serrno;
  16. extern int errno;
  17. /* --- Fonctions privées --- */
  18. boolean server_bind_tcp(Server this, int port) {
  19. /* Declaration variable */
  20. int tmp;
  21. /* Bind */
  22. this->serv.sin_family = AF_INET;
  23. this->serv.sin_port = htons(port);
  24. this->serv.sin_addr.s_addr = htonl(INADDR_ANY);
  25. tmp = bind(this->socket, (struct sockaddr*) &this->serv, sizeof (struct sockaddr_in));
  26. if (tmp == ERR) {
  27. free(this);
  28. serrno = SEBIND;
  29. addperror("Impossible de bind la socket");
  30. return false;
  31. }
  32. /* Listen */
  33. tmp = listen(this->socket, SOMAXCONN);
  34. if (tmp == ERR) {
  35. free(this);
  36. serrno = SEBIND;
  37. addperror("Impossible d'ecouter");
  38. return false;
  39. }
  40. return true;
  41. }
  42. boolean server_accept_tcp(Server this) {
  43. /* Accept */
  44. this->socket_client = accept(this->socket, NULL, NULL);
  45. if (this->socket_client == ERR) {
  46. serrno = SEACCEPT;
  47. addperror("Impossible d'accepter");
  48. return false;
  49. }
  50. return true;
  51. }
  52. ssize_t server_receive_tcp(Server this, char* buf, size_t size) {
  53. int tmp;
  54. /* Lecture message */
  55. memset(buf, 0, size);
  56. tmp = read(this->socket_client, buf, size);
  57. if (tmp == ERR) {
  58. serrno = SERECEIVE;
  59. addperror("Impossible de récupèrer les données");
  60. return ERR;
  61. }
  62. //Si on continue sans avoir modifier buffer (et donc un read non bloquant) signe d'une deconnexion
  63. if(buf[0] == '\0'){
  64. serrno = SEABORT;
  65. adderror("Aucune données émise par le client");
  66. return ERR;
  67. }
  68. return tmp;
  69. }
  70. boolean server_send_tcp(Server this, char* msg) {
  71. int tmp;
  72. /* Envoi message */
  73. tmp = write(this->socket_client, msg, strlen(msg) * sizeof (char));
  74. if (tmp == ERR) {
  75. //Si connexion coupé (et SIGPIPE)
  76. if(errno == EPIPE){
  77. serrno = SEABORT;
  78. } else {
  79. serrno = SESEND;
  80. }
  81. addperror("Impossible d'envoyer les données");
  82. return false;
  83. }
  84. return true;
  85. }
  86. /* --- Fonctions publiques --- */
  87. Server server_create_tcp() {
  88. /* Declaration variable */
  89. Server this;
  90. int tmp, option;
  91. /* Creation socket */
  92. this = malloc(sizeof (struct server));
  93. this->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  94. if (this->socket == ERR) {
  95. free(this);
  96. serrno = SESOCKET;
  97. addperror("Impossible de créer la socket");
  98. return NULL;
  99. }
  100. this->addr = sizeof (struct sockaddr_in);
  101. memset(&this->serv, 0, sizeof (struct sockaddr_in));
  102. /* Indique la possibilité de réutiliser la socket même si elle est en TIME_WAIT */
  103. option = 1;
  104. tmp = setsockopt(this->socket, SOL_SOCKET, SO_REUSEADDR, &option, sizeof (option));
  105. if (tmp == -1) {
  106. free(this);
  107. serrno = SESOCKET;
  108. addperror("Impossible de modifier les options de la socket");
  109. return NULL;
  110. }
  111. /* Lien fonctions */
  112. this->server_bind = server_bind_tcp;
  113. this->server_receive = server_receive_tcp;
  114. this->server_send = server_send_tcp;
  115. this->server_accept = server_accept_tcp;
  116. /* Type de serveur */
  117. this->type = SERV_TCP;
  118. /* Retour */
  119. return this;
  120. }
  121. boolean server_close_client(Server this) {
  122. /* Que pour TCP */
  123. if (this->type != SERV_TCP) {
  124. serrno = SETYPE;
  125. adderror("Type de la socket incorrect");
  126. return false;
  127. }
  128. /* Ferme */
  129. if (close(this->socket_client) == ERR) {
  130. serrno = SECLOSE;
  131. addperror("Impossible de fermer la socket");
  132. return false;
  133. }
  134. return true;
  135. }