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,s1,s2;
- struct sockaddr_in me, her, name;
- int namelen;
- int herlen = sizeof(her);
- int bytes;
- char line[1000];
- int count,readfds, writefds, exceptfds;
-
- #if 1
- dprintf("address %08x\n",xIPAddr());
- dprintf("netmask %08x\n",xNetMask());
- dprintf("max mtu %d\n",xMaxMTU());
- #endif
-
- /* make our socket to listen on */
- s = s_socket(AF_INET, SOCK_STREAM, 0);
- if (s < 0)
- {
- perror("socket");
- exit(1);
- }
-
- /* bind it to port 25 */
- bzero((char *)&me, sizeof(me));
- me.sin_family = AF_INET;
- me.sin_port = htons(25);
- if (s_bind(s, (caddr_t)&me, sizeof(me), 0) < 0)
- {
- perror("bind");
- exit(1);
- }
-
- #if 1
- /* go non-blocking */
- if (s_ioctl(s,FIONBIO,0) < 0)
- {
- perror("ioctl(FIONBIO)");
- exit(1);
- }
- #endif
-
- /* start listening */
- if (s_listen(s,1) < 0)
- {
- perror("listen");
- exit(1);
- }
-
- /* loop checking for a connection to come in */
- for (;;)
- {
- tcpCheckNotify();
- readfds = 0xffffffff;
- writefds = 0xffffffff;
- count = s_select(32, &readfds, &writefds, (int *)0, &selectPoll);
- if (count < 0)
- {
- perror("select");
- exit(1);
- }
- if (count > 0)
- break;
- }
- dprintf("count %d readfds %08x writefds %08x\n",count,readfds,writefds);
-
- /* accept the connection */
- s1 = s_accept(s,&her,&herlen);
- if (s1 < 0)
- {
- perror("accept");
- exit(1);
- }
- dprintf("talking to %08x/%d on %d\n",her.sin_addr,her.sin_port,s1);
-
- /* close the listen socket */
- status = s_close(s);
- if (status < 0)
- {
- perror("close(s)");
- exit(1);
- }
-
- /* send out a greeting */
- tcpCheckNotify();
- strcpy(line,"hello from the test server\015\012");
- bytes = s_write(s1,line,strlen(line));
- if (bytes < 0 && errno != EINPROGRESS)
- {
- perror("write");
- exit(1);
- }
-
- /* wait for the write to finish */
- for (;;)
- {
- tcpCheckNotify();
- writefds = 0xffffffff;
- count = s_select(32, NULL, &writefds, NULL, &selectPoll);
- if (count < 0)
- {
- perror("select");
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- /* wait for an answer */
- for (;;)
- {
- tcpCheckNotify();
- readfds = 0xffffffff;
- count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
- if (count < 0)
- {
- perror("select");
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- /* read the answer */
- tcpCheckNotify();
- bytes = s_read(s1,line,sizeof(line)-2);
- if (bytes < 0)
- {
- perror("read");
- exit(1);
- }
- dprintf("read got %d bytes\n",bytes);
- line[bytes] = '\0';
- dprintf("'%s'\n",line);
-
- /* send a goodbye message */
- tcpCheckNotify();
- strcpy(line,"over and out\015\012");
- bytes = s_write(s1,line,strlen(line));
- if (bytes < 0 && errno != EINPROGRESS)
- {
- perror("write");
- exit(1);
- }
-
- /* wait for it to get there */
- for (;;)
- {
- tcpCheckNotify();
- writefds = 0xffffffff;
- count = s_select(32, NULL, &writefds, NULL, &selectPoll);
- if (count < 0)
- {
- perror("select");
- exit(1);
- }
- if (count > 0)
- break;
- }
-
- /* clean up */
- status = s_close(s1);
- if (status < 0)
- {
- perror("close(s1)");
- exit(1);
- }
-
- /* spin incase something odd happens */
- for (;;)
- {
- tcpCheckNotify();
- }
- }
-
-