Paparazzi UAS  v5.17_devel-24-g2ae834f
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
socket.c
Go to the documentation of this file.
1 /* Sample UDP server */
2 
3 #include <arpa/inet.h>
4 #include <netinet/in.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "socket.h"
12 
13 
14 #define SOCKET_PORT 32000
15 
16 static int socket_fd;
17 static struct sockaddr_in socket_server, socket_client;
18 
19 void socket_init(int is_server)
20 {
21  // Initialize socket
22  if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
23  perror("Socket: socket");
24  exit(1);
25  }
26 
27  bzero(&socket_server, sizeof(socket_server));
28  socket_server.sin_family = AF_INET;
29  socket_server.sin_port = htons(SOCKET_PORT);
30  //inet_aton("192.168.1.69", &socket_server.sin_addr);
31  inet_aton("127.0.0.1", &socket_server.sin_addr);
32 
33  if (is_server) {
34  if (bind(socket_fd, (struct sockaddr *)&socket_server, sizeof(socket_server)) != 0) {
35  perror("Socket: bind");
36  exit(1);
37  }
38  }
39 }
40 
41 int socket_recv(char *buffer, int len)
42 {
43  socklen_t slen = sizeof(socket_server);
44  return recvfrom(socket_fd, buffer, len, MSG_DONTWAIT, (struct sockaddr *)&socket_server, &slen);
45 }
46 
47 void socket_send(char *buffer, int len)
48 {
49  socklen_t slen = sizeof(socket_server);
50  sendto(socket_fd, buffer, len, MSG_DONTWAIT, (struct sockaddr *)&socket_server, slen);
51 }
void socket_send(char *buffer, int len)
Definition: socket.c:47
void socket_init(int is_server)
Definition: socket.c:19
static int socket_fd
Definition: socket.c:16
int socket_recv(char *buffer, int len)
Definition: socket.c:41
static struct sockaddr_in socket_server socket_client
Definition: socket.c:17
#define SOCKET_PORT
Definition: socket.c:14