1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/* Copyright (c) 2000 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in the Pd distribution. */
/* the "pdsend" command. This is a standalone program that forwards messages
from its standard input to Pd via the netsend/netreceive ("FUDI") protocol. */
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifdef MSW
#include <winsock.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#define SOCKET_ERROR -1
#endif
void sockerror(char *s);
void x_closesocket(int fd);
#define BUFSIZE 4096
int main(int argc, char **argv)
{
int sockfd, portno, protocol;
struct sockaddr_in server;
struct hostent *hp;
char *hostname;
int nretry = 10;
#ifdef MSW
short version = MAKEWORD(2, 0);
WSADATA nobby;
#endif
if (argc < 2 || sscanf(argv[1], "%d", &portno) < 1 || portno <= 0)
goto usage;
if (argc >= 3)
hostname = argv[2];
else hostname = "127.0.0.1";
if (argc >= 4)
{
if (!strcmp(argv[3], "tcp"))
protocol = SOCK_STREAM;
else if (!strcmp(argv[3], "udp"))
protocol = SOCK_DGRAM;
else goto usage;
}
else protocol = SOCK_STREAM;
#ifdef MSW
if (WSAStartup(version, &nobby)) sockerror("WSAstartup");
#endif
sockfd = socket(AF_INET, protocol, 0);
if (sockfd < 0)
{
sockerror("socket()");
exit(1);
}
/* connect socket using hostname provided in command line */
server.sin_family = AF_INET;
hp = gethostbyname(hostname);
if (hp == 0)
{
fprintf(stderr, "%s: unknown host\n", hostname);
x_closesocket(sockfd);
exit(1);
}
memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
/* assign client port number */
server.sin_port = htons((unsigned short)portno);
#if 0 /* try this again for 4.0; this crashed my RH 6.2 machine!) */
/* try to connect. */
for (nretry = 0; nretry < (protocol == SOCK_STREAM ? 10 : 1); nretry++)
{
if (nretry > 0)
{
sleep (nretry < 5 ? 1 : 5);
fprintf(stderr, "retrying...");
}
if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) >= 0)
goto connected;
sockerror("connect");
}
x_closesocket(sockfd);
exit(1);
connected: ;
#else
/* try to connect. */
if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
{
sockerror("connect");
x_closesocket(sockfd);
exit(1);
}
#endif
/* now loop reading stdin and sending it to socket */
while (1)
{
char buf[BUFSIZE], *bp;
int nsent, nsend;
if (!fgets(buf, BUFSIZE, stdin))
break;
nsend = strlen(buf);
for (bp = buf, nsent = 0; nsent < nsend;)
{
int res = send(sockfd, buf, nsend-nsent, 0);
if (res < 0)
{
sockerror("send");
goto done;
}
nsent += res;
bp += res;
}
}
done:
if (ferror(stdin))
perror("stdin");
exit (0);
usage:
fprintf(stderr, "usage: pdsend <portnumber> [host] [udp|tcp]\n");
fprintf(stderr, "(default is localhost and tcp)\n");
exit(1);
}
void sockerror(char *s)
{
#ifdef MSW
int err = WSAGetLastError();
if (err == 10054) return;
else if (err == 10044)
{
fprintf(stderr,
"Warning: you might not have TCP/IP \"networking\" turned on\n");
}
#else
int err = errno;
#endif
fprintf(stderr, "%s: %s (%d)\n", s, strerror(err), err);
}
void x_closesocket(int fd)
{
#ifdef MSW
closesocket(fd);
#else
close(fd);
#endif
}
|