Exemplo n.º 1
0
/* ==============================================
	main task routine
   ============================================== */
int main(void) {
	pool_memadd((uint32_t)pool, sizeof(pool));

#ifdef DEBUG
	dbg.start();
#endif
	char	ch;
	CSocket *client;
	CSocket server;

	server.create(SOCK_STREAM);
	server.bind(23);	// bind to port 23
	server.listen();	// enter listen mode

	// Enter an endless loop
	while(1){
		client = server.accept();						// accept and create a connected socket
		if ( client!=NULL ) {
			dbg.println("Client connected\n");
			while( client->isConnected() ) {			// check connection
				if ( client->read(&ch, sizeof(ch)) ) {	// read from client
					client->write(&ch, sizeof(ch));		// echo to client
				}
			}
			dbg.println("Client disconnected\n");
			delete client;								// free client after disconnected
		}
	}
	return 0 ;
}
Exemplo n.º 2
0
	virtual void run() {
		CSocket *client;
		CSocket sock(SOCK_STREAM);
		sock.bind(REMOTE_PORT);
		sock.listen();
		while(1) {
			client = sock.accept();								// wait for client connecting
			if ( client ) {
				dbg.println("server: connected");
				byte buf[sizeof(msg)];
				int  len;
				while( client->isConnected() ) {
					if ( client->readable() ) {
						len = client->read(buf, sizeof(buf));	// read message from client
						client->write(buf, len);				// write back message to client
						leds[0] = !leds[0];
						dbg.println("Client > %s", buf);
					}
				}
				delete client;									// free the client socket after disconnected
				dbg.println("server: disconnected");
			}
		}
	}