forked from Matthias-Wandel/imgcomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_udp.c
executable file
·182 lines (151 loc) · 5.11 KB
/
send_udp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//----------------------------------------------------------------------------------
// Module for sending UDP notifcations to stepper program,
// for aming heater, fan, or cap shooter at kids and squirrels.
//----------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/net.h>
#include <netdb.h>
#include <unistd.h>
#include <memory.h>
#include <sys/select.h>
#include <time.h>
#include "imgcomp.h"
typedef unsigned char BYTE;
typedef unsigned char UCHAR;
typedef unsigned short USHORT;
typedef unsigned long ULONG;
typedef int SOCKET;
typedef int BOOL;
#define FALSE 0
#define TRUE 1
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
typedef fd_set FD_SET;
typedef struct timeval TIMEVAL;
#define h_addr h_addr_list[0]
#define MAGIC_ID 0xf581 // Magic value to identify pings from this program.
#define MAGIC_PORTNUM 7777 // Magic port number for UDP pings (this is the port Joe picked for his app)
#define UDP_MAGIC 0x46c1
//-------------------------------------------------------------------------------------
// Structure for a test UDP packet.
typedef struct {
int Ident;
int Level;
int xpos;
int ypos;
int IsAdjust;
int Motion;
}Udp_t;
//-------------------------------------------------------------------------------------
#define STATUS_FAILED 0xFFFF
struct sockaddr_in dest;
static SOCKET sockUDP;
//--------------------------------------------------------------------------
// Form and send a UDP packet
//--------------------------------------------------------------------------
void SendUDP(int x, int y, int level, int motion)
{
int wrote;
int datasize;
Udp_t Buf;
if (!dest.sin_port){
fprintf(stderr, "UDP not initialized\n");
return;
}
memset(&Buf, 0, sizeof(Buf));
Buf.Ident = UDP_MAGIC;
Buf.Level = level;
Buf.Motion = motion;
Buf.xpos = x;
Buf.ypos = y;
Buf.IsAdjust = 0;
datasize = sizeof(Udp_t);
wrote = sendto(sockUDP,(char *)&Buf, datasize, 0,(struct sockaddr*)&dest, sizeof(struct sockaddr_in));
if (wrote == SOCKET_ERROR){
perror("UDP sendto failed");
#ifndef _WIN32
printf("On Linux, This eventually happens if the destination port is unreacable and Uping is not root\n");
#endif
exit(-1);
}
if (wrote < datasize ) {
fprintf(stdout,"Wrote %d bytes of %d\n",wrote, datasize);
}
//printf("Sent UDP packet, x=%d\n",x);
}
//--------------------------------------------------------------------------
// Main
//--------------------------------------------------------------------------
int InitUDP(char * HostName)
{
#ifdef _WIN32
WSADATA wsaData;
#endif
struct hostent * hp;
unsigned int addr=0;
unsigned short PortNum = MAGIC_PORTNUM; // Use a different port, leave this for listener
//-------------------------------------------------------------------
// Resolve the remote address
printf("Init UDP to %s",HostName);
memset(&dest,0,sizeof(struct sockaddr_in));
if (HostName){
hp = gethostbyname(HostName);
if (hp == NULL){
// If address is not a host name, assume its a x.x.x.x format address.
addr = inet_addr(HostName);
if (addr == INADDR_NONE) {
// Not a number format address either. Give up.
fprintf(stderr,"Unable to resolve %s\n",HostName);
exit(-1);
}
dest.sin_addr.s_addr = addr;
dest.sin_family = AF_INET;
}else{
// Resolved to a host name.
memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);
dest.sin_family = hp->h_addrtype;
}
}else{
// No host name specified. Use loopback address by default.
printf("No host name specified. Using 'loopback' as target\n");
addr = inet_addr("127.0.0.1");
dest.sin_addr.s_addr = addr;
dest.sin_family = AF_INET;
}
dest.sin_port = htons(PortNum);
//-------------------------------------------------------------------
// Open socket for reception of regular UDP packets.
{
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(PortNum+2);
sockUDP = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockUDP == INVALID_SOCKET){
perror("couldn't create UDP socket");
exit(-1);
}
if (bind(sockUDP,(struct sockaddr*)&local,sizeof(local) ) == SOCKET_ERROR) {
#ifdef _WIN32
int err;
err = WSAGetLastError();
if (err != WSAEADDRINUSE){
perror("bind() failed with error");
WSACleanup();
exit(-1);
}else{
printf("Listen port already in use\n");
}
#else
perror("bind failed");
exit(-1);
#endif
}
}
return 0;
}