summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/u_pdsend.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/u_pdsend.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/u_pdsend.c314
1 files changed, 314 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/u_pdsend.c b/apps/plugins/pdbox/PDa/src/u_pdsend.c
new file mode 100644
index 0000000000..9f2f9232bb
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/src/u_pdsend.c
@@ -0,0 +1,314 @@
1/* Copyright (c) 2000 Miller Puckette.
2* For information on usage and redistribution, and for a DISCLAIMER OF ALL
3* WARRANTIES, see the file, "LICENSE.txt," in the Pd distribution. */
4
5/* the "pdsend" command. This is a standalone program that forwards messages
6from its standard input to Pd via the netsend/netreceive ("FUDI") protocol. */
7
8#include <sys/types.h>
9#include <string.h>
10#include <stdio.h>
11#include <errno.h>
12#include <stdlib.h>
13#ifdef UNIX
14#include <unistd.h>
15#include <sys/socket.h>
16#include <netinet/in.h>
17#include <netdb.h>
18#define SOCKET_ERROR -1
19#else
20#include <winsock.h>
21#endif
22
23void sockerror(char *s);
24void x_closesocket(int fd);
25#define BUFSIZE 4096
26
27int main(int argc, char **argv)
28{
29 int sockfd, portno, protocol;
30 struct sockaddr_in server;
31 struct hostent *hp;
32 char *hostname;
33 int nretry = 10;
34#ifdef MSW
35 short version = MAKEWORD(2, 0);
36 WSADATA nobby;
37#endif
38 if (argc < 2 || sscanf(argv[1], "%d", &portno) < 1 || portno <= 0)
39 goto usage;
40 if (argc >= 3)
41 hostname = argv[2];
42 else hostname = "127.0.0.1";
43 if (argc >= 4)
44 {
45 if (!strcmp(argv[3], "tcp"))
46 protocol = SOCK_STREAM;
47 else if (!strcmp(argv[3], "udp"))
48 protocol = SOCK_DGRAM;
49 else goto usage;
50 }
51 else protocol = SOCK_STREAM;
52#ifdef MSW
53 if (WSAStartup(version, &nobby)) sockerror("WSAstartup");
54#endif
55
56 sockfd = socket(AF_INET, protocol, 0);
57 if (sockfd < 0)
58 {
59 sockerror("socket()");
60 exit(1);
61 }
62 /* connect socket using hostname provided in command line */
63 server.sin_family = AF_INET;
64 hp = gethostbyname(hostname);
65 if (hp == 0)
66 {
67 fprintf(stderr, "%s: unknown host\n", hostname);
68 x_closesocket(sockfd);
69 exit(1);
70 }
71 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
72
73 /* assign client port number */
74 server.sin_port = htons((unsigned short)portno);
75
76#if 0 /* try this again for 4.0; this crashed my RH 6.2 machine!) */
77
78 /* try to connect. */
79 for (nretry = 0; nretry < (protocol == SOCK_STREAM ? 10 : 1); nretry++)
80
81 {
82 if (nretry > 0)
83 {
84 sleep (nretry < 5 ? 1 : 5);
85 fprintf(stderr, "retrying...");
86 }
87 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) >= 0)
88 goto connected;
89 sockerror("connect");
90 }
91 x_closesocket(sockfd);
92 exit(1);
93connected: ;
94#else
95 /* try to connect. */
96 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
97 {
98 sockerror("connect");
99 x_closesocket(sockfd);
100 exit(1);
101 }
102#endif
103 /* now loop reading stdin and sending it to socket */
104 while (1)
105 {
106 char buf[BUFSIZE], *bp, nsent, nsend;
107 if (!fgets(buf, BUFSIZE, stdin))
108 break;
109 nsend = strlen(buf);
110 for (bp = buf, nsent = 0; nsent < nsend;)
111 {
112 int res = send(sockfd, buf, nsend-nsent, 0);
113 if (res < 0)
114 {
115 sockerror("send");
116 goto done;
117 }
118 nsent += res;
119 bp += res;
120 }
121 }
122done:
123 if (ferror(stdin))
124 perror("stdin");
125 exit (0);
126usage:
127 fprintf(stderr, "usage: pdsend <portnumber> [host] [udp|tcp]\n");
128 fprintf(stderr, "(default is localhost and tcp)\n");
129 exit(1);
130}
131
132void sockerror(char *s)
133{
134#ifdef MSW
135 int err = WSAGetLastError();
136 if (err == 10054) return;
137 else if (err == 10044)
138 {
139 fprintf(stderr,
140 "Warning: you might not have TCP/IP \"networking\" turned on\n");
141 }
142#endif
143#ifdef UNIX
144 int err = errno;
145#endif
146 fprintf(stderr, "%s: %s (%d)\n", s, strerror(err), err);
147}
148
149void x_closesocket(int fd)
150{
151#ifdef UNIX
152 close(fd);
153#endif
154#ifdef MSW
155 closesocket(fd);
156#endif
157}
158/* Copyright (c) 2000 Miller Puckette.
159* For information on usage and redistribution, and for a DISCLAIMER OF ALL
160* WARRANTIES, see the file, "LICENSE.txt," in the Pd distribution. */
161
162/* the "pdsend" command. This is a standalone program that forwards messages
163from its standard input to Pd via the netsend/netreceive ("FUDI") protocol. */
164
165#include <sys/types.h>
166#include <string.h>
167#include <stdio.h>
168#include <errno.h>
169#include <stdlib.h>
170#ifdef UNIX
171#include <unistd.h>
172#include <sys/socket.h>
173#include <netinet/in.h>
174#include <netdb.h>
175#define SOCKET_ERROR -1
176#else
177#include <winsock.h>
178#endif
179
180void sockerror(char *s);
181void x_closesocket(int fd);
182#define BUFSIZE 4096
183
184int main(int argc, char **argv)
185{
186 int sockfd, portno, protocol;
187 struct sockaddr_in server;
188 struct hostent *hp;
189 char *hostname;
190 int nretry = 10;
191#ifdef MSW
192 short version = MAKEWORD(2, 0);
193 WSADATA nobby;
194#endif
195 if (argc < 2 || sscanf(argv[1], "%d", &portno) < 1 || portno <= 0)
196 goto usage;
197 if (argc >= 3)
198 hostname = argv[2];
199 else hostname = "127.0.0.1";
200 if (argc >= 4)
201 {
202 if (!strcmp(argv[3], "tcp"))
203 protocol = SOCK_STREAM;
204 else if (!strcmp(argv[3], "udp"))
205 protocol = SOCK_DGRAM;
206 else goto usage;
207 }
208 else protocol = SOCK_STREAM;
209#ifdef MSW
210 if (WSAStartup(version, &nobby)) sockerror("WSAstartup");
211#endif
212
213 sockfd = socket(AF_INET, protocol, 0);
214 if (sockfd < 0)
215 {
216 sockerror("socket()");
217 exit(1);
218 }
219 /* connect socket using hostname provided in command line */
220 server.sin_family = AF_INET;
221 hp = gethostbyname(hostname);
222 if (hp == 0)
223 {
224 fprintf(stderr, "%s: unknown host\n", hostname);
225 x_closesocket(sockfd);
226 exit(1);
227 }
228 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
229
230 /* assign client port number */
231 server.sin_port = htons((unsigned short)portno);
232
233#if 0 /* try this again for 4.0; this crashed my RH 6.2 machine!) */
234
235 /* try to connect. */
236 for (nretry = 0; nretry < (protocol == SOCK_STREAM ? 10 : 1); nretry++)
237
238 {
239 if (nretry > 0)
240 {
241 sleep (nretry < 5 ? 1 : 5);
242 fprintf(stderr, "retrying...");
243 }
244 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) >= 0)
245 goto connected;
246 sockerror("connect");
247 }
248 x_closesocket(sockfd);
249 exit(1);
250connected: ;
251#else
252 /* try to connect. */
253 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
254 {
255 sockerror("connect");
256 x_closesocket(sockfd);
257 exit(1);
258 }
259#endif
260 /* now loop reading stdin and sending it to socket */
261 while (1)
262 {
263 char buf[BUFSIZE], *bp, nsent, nsend;
264 if (!fgets(buf, BUFSIZE, stdin))
265 break;
266 nsend = strlen(buf);
267 for (bp = buf, nsent = 0; nsent < nsend;)
268 {
269 int res = send(sockfd, buf, nsend-nsent, 0);
270 if (res < 0)
271 {
272 sockerror("send");
273 goto done;
274 }
275 nsent += res;
276 bp += res;
277 }
278 }
279done:
280 if (ferror(stdin))
281 perror("stdin");
282 exit (0);
283usage:
284 fprintf(stderr, "usage: pdsend <portnumber> [host] [udp|tcp]\n");
285 fprintf(stderr, "(default is localhost and tcp)\n");
286 exit(1);
287}
288
289void sockerror(char *s)
290{
291#ifdef MSW
292 int err = WSAGetLastError();
293 if (err == 10054) return;
294 else if (err == 10044)
295 {
296 fprintf(stderr,
297 "Warning: you might not have TCP/IP \"networking\" turned on\n");
298 }
299#endif
300#ifdef UNIX
301 int err = errno;
302#endif
303 fprintf(stderr, "%s: %s (%d)\n", s, strerror(err), err);
304}
305
306void x_closesocket(int fd)
307{
308#ifdef UNIX
309 close(fd);
310#endif
311#ifdef MSW
312 closesocket(fd);
313#endif
314}