Fix some greedy sed changes in imported code. Also provide a sys/types.h for compatib...
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / u_pdsend.c
blob4fe714d70f07a8eee35104580dd28e1cd719a10e
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