#include <conio.h>
#include "pcdefs.h"
#include "mbuf.h"
#include "netdef.h"
#include "protocol.h"
#include "in.h"
#include <malloc.h>
#include "net.h"

struct tcpport tcpout;

main()
{
	int	i, j;
	int	s;
	int	t;
	u_char	buf[1024];
	struct acache *cp;
	struct sockaddr saddr;

	i = Snetinit();
	if (i < 0) {
		printf("Snetinit returns %d\n", i);
		exit (-1);
	}
	s = Socket(SOCK_STREAM, &saddr, 0, (struct port *)&tcpout);
	if (s < 0) {
		printf("send Socket returns %d\n", s);
		Snetshut();
		exit(-1);
	}
	saddr.port = htons(4000);
	if (SoBind(s, &saddr) < 0) {
		printf("send SoBind returns errord\n");
		Snetshut();
		exit(-1);
	}
	saddr.addr = 0x010101c0L;
	saddr.port = htons(2000);
	i = SoConnect(s, &saddr);
	if (i < 0) {
		goto err;
		Snetshut();
		exit(-1);
	}
	SoWrite(s, "This is a test.\r\n", 17);
	while ((i = SoRead(s, buf, sizeof(buf))) > 0) {
		write(1, buf, i);
		SoWrite(s, "Another line.\r\n", 15);
	}
#ifdef notdef
	printf("last SoRead returned %d\nhit any character to continue\n", i);
	while(!kbhit())
		;
	i = getch();
	printf("closing, state = %d\n", tcpout.state);
#endif
	i = SoClose(s);
	printf("SoClose returns %d\n", i);
	cp = Sgetcache();
	for (i = 0; i < 4; i++) {
		printf("%d: ip = 0x%lx, at 0x%lx tm = 0x%lx\n", i, cp->ip,
			*(u_long *)cp->hrd, cp->tm);
		cp++;
	}
err:
	Snetshut();
}
