/*******************************************************************************
        echo.c
        Z-World, 2000

        A basic server, that when a client connect, echos back to them
        any data that they send.
*******************************************************************************/

#define MY_IP_ADDRESS "10.10.6.105"
#define MY_NETMASK "255.255.255.0"
#define MY_GATEWAY "10.10.6.19"

#memmap xmem
#use "dcrtcp.lib"

#define PORT 23

tcp_Socket echosock;

main()
{
	char buffer[2048];
	int status;
	
	sock_init();

	while(1) {
		tcp_listen(&echosock,PORT,0,0,NULL,0);
		sock_wait_established(&echosock,0,NULL,&status);
	
		printf("Receiving incomming connection\n");
		sock_mode(&echosock,TCP_MODE_ASCII);
		
		while(tcp_tick(&echosock)) {
			sock_wait_input(&echosock,0,NULL,&status);
			if(sock_gets(&echosock,buffer,2048))
				sock_puts(&echosock,buffer);
		}

sock_err:
		switch(status) {
			case 0: /* foreign host closed */
			case 1:
				printf("User closed session\n");
				break;
				
			case -1: /* timeout */
				printf("\nConnection timed out\n");
				break;
		}
	}
}