server_udp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * File: server_udp.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. boolean server_bind(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. return false;
  29. }
  30. return true;
  31. }
  32. ssize_t server_receive_udp(Server this, char* buf, size_t size) {
  33. /* Declaration variable */
  34. int tmp;
  35. /* Reception */
  36. memset(buf, 0, size);
  37. tmp = recvfrom(this->socket, buf, size, 0, (struct sockaddr*) &this->client, &this->addr);
  38. if (tmp == ERR) {
  39. serrno = SERECEIVE;
  40. addperror("Impossible de récupèrer les données");
  41. return ERR;
  42. }
  43. /* Retour */
  44. return tmp;
  45. }
  46. boolean server_send_udp(Server this, char* msg) {
  47. /* Declaration variable */
  48. int tmp;
  49. /* Envoi */
  50. tmp = sendto(this->socket, msg, strlen(msg) * sizeof (char), 0, (struct sockaddr*) &this->client, sizeof (struct sockaddr_in));
  51. if (tmp == ERR) {
  52. serrno = SESEND;
  53. addperror("Impossible d'envoyer les données");
  54. return false;
  55. }
  56. return true;
  57. }
  58. /* --- Fonctions publiques --- */
  59. Server server_create_udp() {
  60. /* Declaration variable */
  61. Server this;
  62. /* Creation socket */
  63. this = malloc(sizeof (struct server));
  64. this->socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  65. if (this->socket == ERR) {
  66. free(this);
  67. serrno = SESOCKET;
  68. addperror("Impossible de créer la socket");
  69. return NULL;
  70. }
  71. this->addr = sizeof (struct sockaddr_in);
  72. memset(&this->serv, 0, sizeof (struct sockaddr_in));
  73. /* Lien fonctions */
  74. this->server_bind = server_bind;
  75. this->server_receive = server_receive_udp;
  76. this->server_send = server_send_udp;
  77. this->server_accept = NULL;
  78. /* Type de serveur */
  79. this->type = SERV_UDP;
  80. /* Retour */
  81. return this;
  82. }
  83. void server_close(Server this) {
  84. /* Ferme */
  85. if (close(this->socket) == ERR) {
  86. serrno = SECLOSE;
  87. addperror("Impossible de fermer la socket");
  88. }
  89. /* Supprime */
  90. free(this);
  91. }
  92. Server server_clone(Server this){
  93. /* Creation nouveau serveur */
  94. Server new = malloc(sizeof(struct server));
  95. /* Copie valeur */
  96. new->type = this->type;
  97. new->socket = this->socket;
  98. new->socket_client = this->socket_client;
  99. new->serv = this->serv;
  100. new->client = this->client;
  101. new->addr = this->addr;
  102. new->server_bind = this->server_bind;
  103. new->server_receive = this->server_receive;
  104. new->server_send = this->server_send;
  105. new->server_accept = this->server_accept;
  106. /* Retourne nouveau serveur */
  107. return new;
  108. }