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
)