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. */
15 #include <sys/socket.h>
16 #include <netinet/in.h>
18 #define SOCKET_ERROR -1
23 void sockerror(char *s
);
24 void x_closesocket(int fd
);
27 int main(int argc
, char **argv
)
29 int sockfd
, portno
, protocol
;
30 struct sockaddr_in server
;
35 short version
= MAKEWORD(2, 0);
38 if (argc
< 2 || sscanf(argv
[1], "%d", &portno
) < 1 || portno
<= 0)
42 else hostname
= "127.0.0.1";
45 if (!strcmp(argv
[3], "tcp"))
46 protocol
= SOCK_STREAM
;
47 else if (!strcmp(argv
[3], "udp"))
48 protocol
= SOCK_DGRAM
;
51 else protocol
= SOCK_STREAM
;
53 if (WSAStartup(version
, &nobby
)) sockerror("WSAstartup");
56 sockfd
= socket(AF_INET
, protocol
, 0);
59 sockerror("socket()");
62 /* connect socket using hostname provided in command line */
63 server
.sin_family
= AF_INET
;
64 hp
= gethostbyname(hostname
);
67 fprintf(stderr
, "%s: unknown host\n", hostname
);
68 x_closesocket(sockfd
);
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!) */
79 for (nretry
= 0; nretry
< (protocol
== SOCK_STREAM
? 10 : 1); nretry
++)
84 sleep (nretry
< 5 ? 1 : 5);
85 fprintf(stderr
, "retrying...");
87 if (connect(sockfd
, (struct sockaddr
*) &server
, sizeof (server
)) >= 0)
91 x_closesocket(sockfd
);
96 if (connect(sockfd
, (struct sockaddr
*) &server
, sizeof (server
)) < 0)
99 x_closesocket(sockfd
);
103 /* now loop reading stdin and sending it to socket */
106 char buf
[BUFSIZE
], *bp
, nsent
, nsend
;
107 if (!fgets(buf
, BUFSIZE
, stdin
))
110 for (bp
= buf
, nsent
= 0; nsent
< nsend
;)
112 int res
= send(sockfd
, buf
, nsend
-nsent
, 0);
127 fprintf(stderr
, "usage: pdsend <portnumber> [host] [udp|tcp]\n");
128 fprintf(stderr
, "(default is localhost and tcp)\n");
132 void sockerror(char *s
)
135 int err
= WSAGetLastError();
136 if (err
== 10054) return;
137 else if (err
== 10044)
140 "Warning: you might not have TCP/IP \"networking\" turned on\n");
146 fprintf(stderr
, "%s: %s (%d)\n", s
, strerror(err
), err
);
149 void x_closesocket(int fd
)
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>
172 #include <sys/socket.h>
173 #include <netinet/in.h>
175 #define SOCKET_ERROR -1
180 void sockerror(char *s
);
181 void x_closesocket(int fd
);
184 int main(int argc
, char **argv
)
186 int sockfd
, portno
, protocol
;
187 struct sockaddr_in server
;
192 short version
= MAKEWORD(2, 0);
195 if (argc
< 2 || sscanf(argv
[1], "%d", &portno
) < 1 || portno
<= 0)
199 else hostname
= "127.0.0.1";
202 if (!strcmp(argv
[3], "tcp"))
203 protocol
= SOCK_STREAM
;
204 else if (!strcmp(argv
[3], "udp"))
205 protocol
= SOCK_DGRAM
;
208 else protocol
= SOCK_STREAM
;
210 if (WSAStartup(version
, &nobby
)) sockerror("WSAstartup");
213 sockfd
= socket(AF_INET
, protocol
, 0);
216 sockerror("socket()");
219 /* connect socket using hostname provided in command line */
220 server
.sin_family
= AF_INET
;
221 hp
= gethostbyname(hostname
);
224 fprintf(stderr
, "%s: unknown host\n", hostname
);
225 x_closesocket(sockfd
);
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
++)
241 sleep (nretry
< 5 ? 1 : 5);
242 fprintf(stderr
, "retrying...");
244 if (connect(sockfd
, (struct sockaddr
*) &server
, sizeof (server
)) >= 0)
246 sockerror("connect");
248 x_closesocket(sockfd
);
252 /* try to connect. */
253 if (connect(sockfd
, (struct sockaddr
*) &server
, sizeof (server
)) < 0)
255 sockerror("connect");
256 x_closesocket(sockfd
);
260 /* now loop reading stdin and sending it to socket */
263 char buf
[BUFSIZE
], *bp
, nsent
, nsend
;
264 if (!fgets(buf
, BUFSIZE
, stdin
))
267 for (bp
= buf
, nsent
= 0; nsent
< nsend
;)
269 int res
= send(sockfd
, buf
, nsend
-nsent
, 0);
284 fprintf(stderr
, "usage: pdsend <portnumber> [host] [udp|tcp]\n");
285 fprintf(stderr
, "(default is localhost and tcp)\n");
289 void sockerror(char *s
)
292 int err
= WSAGetLastError();
293 if (err
== 10054) return;
294 else if (err
== 10044)
297 "Warning: you might not have TCP/IP \"networking\" turned on\n");
303 fprintf(stderr
, "%s: %s (%d)\n", s
, strerror(err
), err
);
306 void x_closesocket(int fd
)