Blame main.cpp

91ef56
91ef56
#include <cstring></cstring>
91ef56
91ef56
#include "tcp.h"
91ef56
#include "main.h"
91ef56
91ef56
91ef56
int main(int argc, char** argv) {
30ecb0
	if (argc != 3)
30ecb0
		{ printf("two args expected\n"); return 1; }
91ef56
	
30ecb0
	Tcp::Address address;
30ecb0
	address.resolve(argv[2]);
91ef56
	
6173c6
	if (0 == strcmp(argv[1], "tcpclient")) {
91ef56
		printf("tcpconnect\n");
91ef56
		
91ef56
		Tcp::Connection connection;
91ef56
		if (connection.open(address))
30ecb0
			printf( "connected to server: %hhu.%hhu.%hhu.%hhu:%hu\n",
91ef56
					address.ip[0], address.ip[1], address.ip[2], address.ip[3], address.port );
91ef56
		char buf[65] = {};
91ef56
		while(connection.check()) {
91ef56
			sprintf(buf, "client say %d\n", rand());
91ef56
			if (connection.write(buf, sizeof(buf) - 1))
91ef56
				printf("sent: %s\n", buf);
91ef56
			if (connection.read(buf, sizeof(buf) - 1))
91ef56
				printf("received: %s\n", buf);
91ef56
		}
91ef56
		
91ef56
		printf("last error: %u\n", connection.getLastError());
91ef56
		return connection.getLastError();
91ef56
	}
91ef56
	
91ef56
	if (0 == strcmp(argv[1], "tcpserver")) {
91ef56
		printf("tcpserver\n");
91ef56
		
91ef56
		class Handler: public Tcp::Handler {
91ef56
		public:
91ef56
			void handleTcpConnection(Tcp::Connection &connection) override {
91ef56
				const Tcp::Address &address = connection.getRemoteAddr();
30ecb0
				printf( "received connection from: %hhu.%hhu.%hhu.%hhu:%hu\n",
91ef56
						address.ip[0], address.ip[1], address.ip[2], address.ip[3], address.port );
91ef56
				while(connection.check()) {
91ef56
					char buf[65] = {};
91ef56
					if (connection.read(buf, sizeof(buf) - 1))
91ef56
						printf("received: %s\n", buf);
91ef56
					sprintf(buf, "server say %d\n", rand());
91ef56
					if (connection.write(buf, sizeof(buf) - 1))
91ef56
						printf("sent: %s\n", buf);
91ef56
				}
91ef56
			}
91ef56
		} handler;
91ef56
		
91ef56
		Tcp::Server server;
91ef56
		if (server.start(address, &handler))
30ecb0
			printf( "listening at: %hhu.%hhu.%hhu.%hhu:%hu\n",
91ef56
					address.ip[0], address.ip[1], address.ip[2], address.ip[3], address.port );
91ef56
		server.join();
91ef56
		
91ef56
		printf("last error: %u\n", server.getLastError());
91ef56
		return server.getLastError();
91ef56
	}
91ef56
91ef56
	printf("wrong args\n");
91ef56
	return 1;
91ef56
}
91ef56
91ef56