From 9da83c9e1b69c4704886ab0c1011bf82a5009a59 Mon Sep 17 00:00:00 2001 From: hidnplayr Date: Thu, 2 Nov 2017 18:22:06 +0000 Subject: [PATCH] Added some comments to TCP server demo app. git-svn-id: svn://kolibrios.org@7108 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/network/tcpserv/tcpserv.asm | 37 +++++++++++++++++++++------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/programs/network/tcpserv/tcpserv.asm b/programs/network/tcpserv/tcpserv.asm index 9aebfd28b..bb7df61e8 100644 --- a/programs/network/tcpserv/tcpserv.asm +++ b/programs/network/tcpserv/tcpserv.asm @@ -1,9 +1,9 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; -;; Copyright (C) KolibriOS team 2010-2015. All rights reserved. ;; +;; Copyright (C) KolibriOS team 2010-2017. All rights reserved. ;; ;; Distributed under terms of the GNU General Public License ;; ;; ;; -;; tcpserv.asm - TCP demo program for KolibriOS ;; +;; tcpserv.asm - TCP server demo program for KolibriOS ;; ;; ;; ;; Written by hidnplayr@kolibrios.org ;; ;; ;; @@ -46,46 +46,59 @@ start: invoke con_start, 1 invoke con_init, 80, 25, 80, 25, title +; Set event mask to socket events only mcall 40, EVM_STACK +; Write message str1 to the console invoke con_write_asciiz, str1 +; Allocate a TCP socket mcall socket, AF_INET4, SOCK_STREAM, 0 cmp eax, -1 je sock_err +; Socket allocation succeeded, store it's number in socketnum mov [socketnum], eax -; This socket option is not implemented in kernel yet. +; This might be needed in the future, +; SO_REUSEADDR option is not implemented in kernel yet. ; mcall setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes, ; cmp eax, -1 ; je opt_err +; Bind the socket to port 23 (as defined in sockaddr1) mcall bind, [socketnum], sockaddr1, sockaddr1.length cmp eax, -1 je bind_err - mcall listen, [socketnum], 10 ; Backlog = 10 +; Start listening for incoming connections, with a backlog of max 1 + mcall listen, [socketnum], 1 cmp eax, -1 je listen_err +; Write message str2 to the console invoke con_write_asciiz, str2 +; (Wait for and) accept incoming connection mcall accept, [socketnum], sockaddr1, sockaddr1.length cmp eax, -1 je acpt_err +; We have a new incoming connection, store the new socket number in socketnum2 mov [socketnum2], eax +; Send a message on the incoming connection mcall send, [socketnum2], hello, hello.length +; Print the received data to the console, untill socket is closed by remote end .loop: - mcall recv, [socketnum2], buffer, buffer.length, 0 + mcall recv, [socketnum2], buffer, BUFFERSIZE, 0 cmp eax, -1 je .loop - mov byte[buffer+eax], 0 + mov byte[buffer+eax], 0 ; Zero-terminate the data, so we can print it invoke con_write_asciiz, buffer jmp .loop +; Print error message acpt_err: invoke con_write_asciiz, str8 jmp done @@ -102,18 +115,25 @@ sock_err: invoke con_write_asciiz, str6 jmp done + done: - invoke con_getch2 ; Wait for user input +; Wait for user input + invoke con_getch2 +; Close console invoke con_exit, 1 exit: +; Close listening socket, if it is open cmp [socketnum], 0 je @f mcall close, [socketnum] @@: + +; Close second socket, if it is open cmp [socketnum2], 0 je @f mcall close, [socketnum2] @@: +; Close application mcall -1 @@ -133,7 +153,7 @@ hello db 'Hello world!',0 .length = $ - hello sockaddr1: - dw AF_INET4 + dw AF_INET4 ; IPv4 .port dw 23 shl 8 ; port 23 - network byte order .ip dd 0 rb 10 @@ -160,7 +180,6 @@ i_end: socketnum dd 0 socketnum2 dd 0 buffer rb BUFFERSIZE -.length = BUFFERSIZE align 4 rb 4096 ; stack