home *** CD-ROM | disk | FTP | other *** search
- #include <Events.h>
- #include <memory.h>
- #include <types.h>
- #include <OSUtils.h> /* for SysBeep */
-
- #include <stdio.h>
-
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/errno.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <netinet/in.h>
- #include <sys/uio.h>
-
- #include "tcpglue.h"
- #include "socket.internal.h"
-
- static struct timeval selectPoll = {0,0};
- main()
- {
- int status;
- int s;
- struct sockaddr_in me, her, name;
- int namelen;
- int herlen = sizeof(her);
- int bytes;
- char line[1000];
- int count,readfds, writefds, exceptfds;
- int i;
-
- #if 1
- dprintf("address %08x\n",xIPAddr());
- dprintf("netmask %08x\n",xNetMask());
- dprintf("max mtu %d\n",xMaxMTU());
- #endif
-
- /* make our socket */
- s = s_socket(AF_INET, SOCK_STREAM, 0);
- if (s < 0)
- {
- dprintf("socket - error %d",errno);
- exit(1);
- }
-
- /* bind it to port zero - ie. let MacTCP pick a port */
- bzero((char *)&me, sizeof(me));
- me.sin_family = AF_INET;
- me.sin_port = htons(0);
- if (s_bind(s, (caddr_t)&me, sizeof(me), 0) < 0)
- {
- dprintf("bind - error %d",errno);
- exit(1);
- }
-
- /* go non-blocking */
- if (s_ioctl(s,FIONBIO,0) < 0)
- {
- dprintf("ioctl(FIONBIO) - error %d",errno);
- exit(1);
- }
-
- /* connect to the server */
- her.sin_family = AF_INET;
- #if 1
- her.sin_addr.s_addr = 0x8064660a; /* madhaus */
- #else
- her.sin_addr.s_addr = 0x0d000ce8; /* xerox.com */
- #endif
- her.sin_port = htons(25); /* SMTP */
- if (s_connect(s,(caddr_t)&her,sizeof(her)) < 0)
- {
- if (errno != EINPROGRESS)
- {
- dprintf("connect - error %d",errno);
- exit(1);
- }
- }
- s = s;
-
- /* wait for the server's greeting */
- for (;;)
- {
- tcpCheckNotify();
- readfds = 0xffffffff;
- count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
- if (count < 0)
- {
- dprintf("select - error %d",errno);
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- /* read what he said */
- tcpCheckNotify();
- bytes = s_read(s,line,sizeof(line)-2);
- if (bytes < 0)
- {
- dprintf("read - error %d",errno);
- exit(1);
- }
- dprintf("read got %d bytes\n",bytes);
- line[bytes] = '\0';
- dprintf("'%s'\n",line);
-
- /* send our own introduction (using writev just for fun) */
- tcpCheckNotify();
- {
- struct iovec iov[10];
- char a[100],b[100],c[100],d[100];
-
- strcpy(a,"HELO milligan.");
- iov[0].iov_len = strlen(a);
- iov[0].iov_base = a;
-
- strcpy(b,"utcs.");
- iov[1].iov_len = strlen(b);
- iov[1].iov_base = b;
-
- strcpy(c,"utoronto.");
- iov[2].iov_len = strlen(c);
- iov[2].iov_base = c;
-
- strcpy(d,"ca\015\012");
- iov[3].iov_len = strlen(d);
- iov[3].iov_base = d;
-
- bytes = s_writev(s,&iov[0],4);
- if (bytes <= 0 && errno != EINPROGRESS)
- {
- dprintf("writev - error %d",errno);
- exit(1);
- }
- }
-
- /* wait for it to get there */
- for (;;)
- {
- tcpCheckNotify();
- writefds = 0xffffffff;
- count = s_select(32, NULL, &writefds, NULL, &selectPoll);
- if (count < 0)
- {
- dprintf("select - error %d",errno);
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- /* wait for him to decide he likes us */
- for (;;)
- {
- tcpCheckNotify();
- readfds = 0xffffffff;
- count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
- if (count < 0)
- {
- dprintf("select - error %d",errno);
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- /* read his approval message (or disapproval...) */
- tcpCheckNotify();
- bytes = s_read(s,line,sizeof(line)-2);
- if (bytes < 0)
- {
- dprintf("read - error %d",errno);
- exit(1);
- }
- dprintf("read got %d bytes\n",bytes);
- line[bytes] = '\0';
- dprintf("'%s'\n",line);
-
- /* a long message to test the buffer allocator */
- tcpCheckNotify();
- for (i=0; i<697; i++)
- line[i] = 'a';
- line[698] = '\015';
- line[699] = '\012';
- bytes = s_write(s,line,700);
- if (bytes <= 0 && errno != EINPROGRESS)
- {
- dprintf("write - error %d",errno);
- exit(1);
- }
- /* read his error message */
- tcpCheckNotify();
- for (;;)
- {
- bytes = s_read(s,line,sizeof(line)-2);
- if (bytes < 0)
- {
- if (errno = EWOULDBLOCK)
- continue;
- dprintf("read - error %d",errno);
- exit(1);
- }
- break;
- }
- dprintf("read got %d bytes\n",bytes);
- line[bytes] = '\0';
- dprintf("'%s'\n",line);
-
- /* now lets be rude and quit the conversation */
- tcpCheckNotify();
- strcpy(line,"QUIT\015\012");
- bytes = s_write(s,line,strlen(line));
- if (bytes <= 0 && errno != EINPROGRESS)
- {
- dprintf("write - error %d",errno);
- }
-
- #ifndef LIKE_CRASHES
- for (;;)
- {
- tcpCheckNotify();
- writefds = 0xffffffff;
- count = s_select(32, NULL, &writefds, NULL, &selectPoll);
- if (count < 0)
- {
- dprintf("select - error %d",errno);
- exit(1);
- }
- if (count > 0)
- break;
- }
- for (;;)
- {
- tcpCheckNotify();
- readfds = 0xffffffff;
- count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
- if (count < 0)
- {
- dprintf("select - error %d",errno);
- exit(1);
- }
- if (count > 0)
- break;
- }
- tcpCheckNotify();
- bytes = s_read(s,line,sizeof(line)-2);
- if (bytes < 0)
- {
- dprintf("read - error %d",errno);
- exit(1);
- }
- dprintf("read got %d bytes\n",bytes);
- line[bytes] = '\0';
- dprintf("'%s'\n",line);
- #endif
- tcpCheckNotify();
- if (s_close(s) < 0)
- {
- dprintf("close(s) - error %d",errno);
- }
-
- /* spin in case something odd happens at the end */
- for (;;)
- {
- tcpCheckNotify();
- }
- }
-
-