#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <io.h>
#include "pcdefs.h"
#include "netdef.h"
#include "protocol.h"
#include "in.h"
#include <malloc.h>
#include "net.h"
#include "config.h"

extern struct conf Scon;
struct udpport udpout;

main()
{
	int	i, j;
	int	s;
	char	buf[1024];
	struct sockaddr saddr;

	strcpy(buf, "This is a test of what we can send  out.\n");
	if (configure((char *)0, &Scon) < 0) {
		printf("ERROR: reading config.tel\n");
		exit (-1);
	}
	i = Snetinit();
	if (i < 0) {
		printf("Snetinit returns %d\n", i);
		exit (-1);
	}
	s = Socket(SOCK_DGRAM, &saddr, 0, (struct port *)&udpout);
	if (s < 0) {
		printf("send Socket returns %d\n", s);
		Snetshut();
		exit(-1);
	}
	printf("send socket is %d\n", s);
	saddr.port = htons(4000);
	if (SoBind(s, &saddr) < 0) {
		printf("send SoBind returns errord\n");
		Snetshut();
		exit(-1);
	}
	printf("before SoSend\n");
	saddr.port = htons(2000);
	saddr.addr = 0x010101c0L;
	for (i = 0; i < 10; i++) {
		netsleep();
		if ((j = SoSend(s, &saddr, (u_char *)buf, strlen(buf))) < 0)
			printf("Sosend: error returns %d\n", j);
		else
			printf("Sosend returns %d\n", j);
	}
	printf("done send\n");
	_fill_char(buf, sizeof(buf), 0);
	for (;;) {
	   netsleep();
	   if ((j = SoReceive(s, &saddr, (u_char *)buf, sizeof(buf))) > 0) {
		_write(1, buf, j);
		_fill_char(buf, sizeof(buf), 0);
	   }
	    if (j == 1)
		    break;
	}
	printf("done\n");
	while(!kbhit())
		;
	Snetshut();
}
