Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / u_pdsend.c
blob9f2f9232bbb889c18341cca0a9746d5e4cef46bc
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. */
5 /* the "pdsend" command. This is a standalone program that forwards messages
6 from its standard input to Pd via the netsend/netreceive ("FUDI") protocol. */
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
23 void sockerror(char *s);
24 void x_closesocket(int fd);
25 #define BUFSIZE 4096
27 int main(int argc, char **argv)
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)
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;
51 else protocol = SOCK_STREAM;
52 #ifdef MSW
53 if (WSAStartup(version, &nobby)) sockerror("WSAstartup");
54 #endif
56 sockfd = socket(AF_INET, protocol, 0);
57 if (sockfd < 0)
59 sockerror("socket()");
60 exit(1);
62 /* connect socket using hostname provided in command line */
63 server.sin_family = AF_INET;
64 hp = gethostbyname(hostname);
65 if (hp == 0)
67 fprintf(stderr, "%s: unknown host\n", hostname);
68 x_closesocket(sockfd);
69 exit(1);
71 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
73 /* assign client port number */
74 server.sin_port = htons((unsigned short)portno);
76 #if 0 /* try this again for 4.0; this crashed my RH 6.2 machine!) */
78 /* try to connect. */
79 for (nretry = 0; nretry < (protocol == SOCK_STREAM ? 10 : 1); nretry++)
82 if (nretry > 0)
84 sleep (nretry < 5 ? 1 : 5);
85 fprintf(stderr, "retrying...");
87 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) >= 0)
88 goto connected;
89 sockerror("connect");
91 x_closesocket(sockfd);
92 exit(1);
93 connected: ;
94 #else
95 /* try to connect. */
96 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
98 sockerror("connect");
99 x_closesocket(sockfd);
100 exit(1);
102 #endif
103 /* now loop reading stdin and sending it to socket */
104 while (1)
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;)
112 int res = send(sockfd, buf, nsend-nsent, 0);
113 if (res < 0)
115 sockerror("send");
116 goto done;
118 nsent += res;
119 bp += res;
122 done:
123 if (ferror(stdin))
124 perror("stdin");
125 exit (0);
126 usage:
127 fprintf(stderr, "usage: pdsend <portnumber> [host] [udp|tcp]\n");
128 fprintf(stderr, "(default is localhost and tcp)\n");
129 exit(1);
132 void sockerror(char *s)
134 #ifdef MSW
135 int err = WSAGetLastError();
136 if (err == 10054) return;
137 else if (err == 10044)
139 fprintf(stderr,
140 "Warning: you might not have TCP/IP \"networking\" turned on\n");
142 #endif
143 #ifdef UNIX
144 int err = errno;
145 #endif
146 fprintf(stderr, "%s: %s (%d)\n", s, strerror(err), err);
149 void x_closesocket(int fd)
151 #ifdef UNIX
152 close(fd);
153 #endif
154 #ifdef MSW
155 closesocket(fd);
156 #endif
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. */
162 /* the "pdsend" command. This is a standalone program that forwards messages
163 from its standard input to Pd via the netsend/netreceive ("FUDI") protocol. */
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
180 void sockerror(char *s);
181 void x_closesocket(int fd);
182 #define BUFSIZE 4096
184 int main(int argc, char **argv)
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)
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;
208 else protocol = SOCK_STREAM;
209 #ifdef MSW
210 if (WSAStartup(version, &nobby)) sockerror("WSAstartup");
211 #endif
213 sockfd = socket(AF_INET, protocol, 0);
214 if (sockfd < 0)
216 sockerror("socket()");
217 exit(1);
219 /* connect socket using hostname provided in command line */
220 server.sin_family = AF_INET;
221 hp = gethostbyname(hostname);
222 if (hp == 0)
224 fprintf(stderr, "%s: unknown host\n", hostname);
225 x_closesocket(sockfd);
226 exit(1);
228 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
230 /* assign client port number */
231 server.sin_port = htons((unsigned short)portno);
233 #if 0 /* try this again for 4.0; this crashed my RH 6.2 machine!) */
235 /* try to connect. */
236 for (nretry = 0; nretry < (protocol == SOCK_STREAM ? 10 : 1); nretry++)
239 if (nretry > 0)
241 sleep (nretry < 5 ? 1 : 5);
242 fprintf(stderr, "retrying...");
244 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) >= 0)
245 goto connected;
246 sockerror("connect");
248 x_closesocket(sockfd);
249 exit(1);
250 connected: ;
251 #else
252 /* try to connect. */
253 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
255 sockerror("connect");
256 x_closesocket(sockfd);
257 exit(1);
259 #endif
260 /* now loop reading stdin and sending it to socket */
261 while (1)
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;)
269 int res = send(sockfd, buf, nsend-nsent, 0);
270 if (res < 0)
272 sockerror("send");
273 goto done;
275 nsent += res;
276 bp += res;
279 done:
280 if (ferror(stdin))
281 perror("stdin");
282 exit (0);
283 usage:
284 fprintf(stderr, "usage: pdsend <portnumber> [host] [udp|tcp]\n");
285 fprintf(stderr, "(default is localhost and tcp)\n");
286 exit(1);
289 void sockerror(char *s)
291 #ifdef MSW
292 int err = WSAGetLastError();
293 if (err == 10054) return;
294 else if (err == 10044)
296 fprintf(stderr,
297 "Warning: you might not have TCP/IP \"networking\" turned on\n");
299 #endif
300 #ifdef UNIX
301 int err = errno;
302 #endif
303 fprintf(stderr, "%s: %s (%d)\n", s, strerror(err), err);
306 void x_closesocket(int fd)
308 #ifdef UNIX
309 close(fd);
310 #endif
311 #ifdef MSW
312 closesocket(fd);
313 #endif