#include "hspsock.as"

#define ADDR "127.0.0.1"
#define PORT 27182
#define BUFSIZE 256

	sdim send, BUFSIZE
	sdim recv, BUFSIZE
	
	onexit *exit
	
	button "SERVER", *sb
	button "CLIENT", *cb
	stop

*sb
	clrobj
	pos 0, 0
	mes "SERVER MODE"

*s
	mes
	sockmake 0, PORT
	if stat != 0 : ec = 1 : goto *se
	
	repeat
		sockwait 0
		if stat != 1 : break
		wait 10
	loop
	if stat != 0 : ec = 2 : goto *se
	
	send = strf("I'M A SERVER! @ %02d:%02d:%02d.%03d", gettime(4), gettime(5), gettime(6), gettime(7))
	sockput send, 0
	if stat != 0 : ec = 3 : goto *se
	mes "SEND: " + send
	
	repeat
		sockcheck 0
		if stat != 1 : break
		wait 10
	loop
	if stat != 0 : ec = 4 : goto *se
	
	sockget recv, BUFSIZE, 0
	if stat != 0 : ec = 5 : goto *se
	mes "RECV: " + recv
	
	sockclose 0
	goto *s

*cb
	mes
	objenable 1, 0
	clrobj 0, 0
	
	sockopen 0, ADDR, PORT
	if stat != 0 : ec = 1 : goto *ce
	
	repeat
		sockcheck 0
		if stat != 1 : break
		wait 10
	loop
	if stat != 0 : ec = 2 : goto *ce
	
	sockget recv, BUFSIZE, 0
	if stat != 0 : ec = 3 : goto *ce
	mes "RECV: " + recv
	
	send = strf("I'M A CLIENT! @ %02d:%02d:%02d.%03d", gettime(4), gettime(5), gettime(6), gettime(7))
	sockput send, 0
	if stat != 0 : ec = 4 : goto *ce
	mes "SEND: " + send
	
	sockclose 0
	objenable 1, 1
	stop

*se
	dialog "SERVER ERROR #"+ec, 1
	goto *exit

*ce
	dialog "CLIENT ERROR #"+ec, 1
	goto *exit

*exit
	sockclose 0
	end