aboutsummaryrefslogtreecommitdiff
path: root/pd/src/u_pdsend.c
diff options
context:
space:
mode:
authorMiller Puckette <millerpuckette@users.sourceforge.net>2004-09-06 20:20:36 +0000
committerMiller Puckette <millerpuckette@users.sourceforge.net>2004-09-06 20:20:36 +0000
commited932acb5860bf8b9296169676499562a55d139e (patch)
treedc6a40dba908deb07c175cd40ee19c197318f72d /pd/src/u_pdsend.c
parentdad636821f6e7d3ead02c157f308c0ceeba9af3d (diff)
checking in version 0.38test5.
Oops, I realize I forgot some more nice files, will add them and re-commit. svn path=/trunk/; revision=2010
Diffstat (limited to 'pd/src/u_pdsend.c')
-rw-r--r--pd/src/u_pdsend.c110
1 files changed, 55 insertions, 55 deletions
diff --git a/pd/src/u_pdsend.c b/pd/src/u_pdsend.c
index de9488b0..7b464cad 100644
--- a/pd/src/u_pdsend.c
+++ b/pd/src/u_pdsend.c
@@ -10,14 +10,16 @@ from its standard input to Pd via the netsend/netreceive ("FUDI") protocol. */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
-#ifdef UNIX
-#include <unistd.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
-#else
-#include <winsock.h>
#endif
void sockerror(char *s);
@@ -36,17 +38,17 @@ int main(int argc, char **argv)
WSADATA nobby;
#endif
if (argc < 2 || sscanf(argv[1], "%d", &portno) < 1 || portno <= 0)
- goto usage;
+ goto usage;
if (argc >= 3)
- hostname = argv[2];
+ 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;
+ 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
@@ -56,37 +58,37 @@ int main(int argc, char **argv)
sockfd = socket(AF_INET, protocol, 0);
if (sockfd < 0)
{
- sockerror("socket()");
- exit(1);
+ sockerror("socket()");
+ exit(1);
}
- /* connect socket using hostname provided in command line */
+ /* 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);
+ 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 */
+ /* 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!) */
+#if 0 /* try this again for 4.0; this crashed my RH 6.2 machine!) */
- /* try to connect. */
+ /* 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");
+ 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);
@@ -95,33 +97,33 @@ connected: ;
/* try to connect. */
if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
{
- sockerror("connect");
- x_closesocket(sockfd);
- exit(1);
+ sockerror("connect");
+ x_closesocket(sockfd);
+ exit(1);
}
#endif
- /* now loop reading stdin and sending it to socket */
+ /* now loop reading stdin and sending it to socket */
while (1)
{
- char buf[BUFSIZE], *bp, 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;
- }
+ char buf[BUFSIZE], *bp, 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");
+ perror("stdin");
exit (0);
usage:
fprintf(stderr, "usage: pdsend <portnumber> [host] [udp|tcp]\n");
@@ -136,11 +138,10 @@ void sockerror(char *s)
if (err == 10054) return;
else if (err == 10044)
{
- fprintf(stderr,
- "Warning: you might not have TCP/IP \"networking\" turned on\n");
+ fprintf(stderr,
+ "Warning: you might not have TCP/IP \"networking\" turned on\n");
}
-#endif
-#ifdef UNIX
+#else
int err = errno;
#endif
fprintf(stderr, "%s: %s (%d)\n", s, strerror(err), err);
@@ -148,10 +149,9 @@ void sockerror(char *s)
void x_closesocket(int fd)
{
-#ifdef UNIX
- close(fd);
-#endif
#ifdef MSW
closesocket(fd);
+#else
+ close(fd);
#endif
}