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.c158
1 files changed, 0 insertions, 158 deletions
diff --git a/apps/plugins/pdbox/PDa/src/u_pdsend.c b/apps/plugins/pdbox/PDa/src/u_pdsend.c
deleted file mode 100644
index 4fe714d70f..0000000000
--- a/apps/plugins/pdbox/PDa/src/u_pdsend.c
+++ /dev/null
@@ -1,158 +0,0 @@
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