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.c156
1 files changed, 0 insertions, 156 deletions
diff --git a/apps/plugins/pdbox/PDa/src/u_pdsend.c b/apps/plugins/pdbox/PDa/src/u_pdsend.c
index 9f2f9232bb..4fe714d70f 100644
--- a/apps/plugins/pdbox/PDa/src/u_pdsend.c
+++ b/apps/plugins/pdbox/PDa/src/u_pdsend.c
@@ -155,160 +155,4 @@ void x_closesocket(int fd)
155 closesocket(fd); 155 closesocket(fd);
156#endif 156#endif
157} 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 158
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}