server_tcp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "error.h"
  12. #include "server.h"
  13. /* --- Extern --- */
  14. extern int serrno;
  15. /* --- Fonctions privées --- */
  16. void server_bind_tcp(Server this, int port) {
  17. /* Declaration variable */
  18. int tmp;
  19. /* Bind */
  20. this->serv.sin_family = AF_INET;
  21. this->serv.sin_port = htons(port);
  22. this->serv.sin_addr.s_addr = htonl(INADDR_ANY);
  23. tmp = bind(this->socket, (struct sockaddr*) &this->serv, sizeof (struct sockaddr_in));
  24. if (tmp == ERR) {
  25. free(this);
  26. serrno = SEBIND;
  27. addperror("Impossible de bind la socket");
  28. }
  29. /* Listen */
  30. tmp = listen(this->socket, SOMAXCONN);
  31. if (tmp == ERR) {
  32. free(this);
  33. serrno = SEBIND;
  34. addperror("Impossible d'ecouter");
  35. }
  36. }
  37. void server_accept_tcp(Server this) {
  38. /* Accept */
  39. this->socket_client = accept(this->socket, NULL, NULL);
  40. if (this->socket_client == ERR) {
  41. serrno = SEACCEPT;
  42. addperror("Impossible d'accepter");
  43. }
  44. }
  45. ssize_t server_receive_tcp(Server this, char* buf, size_t size) {
  46. int tmp;
  47. /* Lecture message */
  48. memset(buf, 0, size);
  49. tmp = read(this->socket_client, buf, size);
  50. if (tmp == ERR) {
  51. serrno = SERECEIVE;
  52. addperror("Impossible de récupèrer les données");
  53. return ERR;
  54. }
  55. return tmp;
  56. }
  57. void server_send_tcp(Server this, char* msg) {
  58. int tmp;
  59. /* Envoi message */
  60. tmp = write(this->socket_client, msg, strlen(msg) * sizeof (char));
  61. if (tmp == ERR) {
  62. serrno = SESEND;
  63. addperror("Impossible d'envoyer les données");
  64. }
  65. }
  66. /* --- Fonctions publiques --- */
  67. Server server_create_tcp() {
  68. /* Declaration variable */
  69. Server this;
  70. int tmp, option;
  71. /* Creation socket */
  72. this = malloc(sizeof (struct server));
  73. this->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  74. if (this->socket == ERR) {
  75. free(this);
  76. serrno = SESOCKET;
  77. addperror("Impossible de créer la socket");
  78. return NULL;
  79. }
  80. this->addr = sizeof (struct sockaddr_in);
  81. memset(&this->serv, 0, sizeof (struct sockaddr_in));
  82. /* Indique la possibilité de réutiliser la socket même si elle est en TIME_WAIT */
  83. option = 1;
  84. tmp = setsockopt(this->socket, SOL_SOCKET, SO_REUSEADDR, &option, sizeof (option));
  85. if (tmp == -1) {
  86. free(this);
  87. serrno = SESOCKET;
  88. addperror("Impossible de modifier les options de la socket");
  89. return NULL;
  90. }
  91. /* Lien fonctions */
  92. this->server_bind = server_bind_tcp;
  93. this->server_receive = server_receive_tcp;
  94. this->server_send = server_send_tcp;
  95. this->server_accept = server_accept_tcp;
  96. /* Type de serveur */
  97. this->type = SERV_TCP;
  98. /* Retour */
  99. return this;
  100. }
  101. void server_close_client(Server this) {
  102. /* Que pour TCP */
  103. if (this->type != SERV_TCP) {
  104. return;
  105. }
  106. /* Ferme */
  107. if (close(this->socket_client) == ERR) {
  108. serrno = SECLOSE;
  109. addperror("Impossible de fermer la socket");
  110. }
  111. }