Added some comments to TCP server demo app.
git-svn-id: svn://kolibrios.org@7108 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
b6bb3d2c62
commit
9da83c9e1b
@ -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 ;;
|
;; 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 ;;
|
;; Written by hidnplayr@kolibrios.org ;;
|
||||||
;; ;;
|
;; ;;
|
||||||
@ -46,46 +46,59 @@ start:
|
|||||||
invoke con_start, 1
|
invoke con_start, 1
|
||||||
invoke con_init, 80, 25, 80, 25, title
|
invoke con_init, 80, 25, 80, 25, title
|
||||||
|
|
||||||
|
; Set event mask to socket events only
|
||||||
mcall 40, EVM_STACK
|
mcall 40, EVM_STACK
|
||||||
|
|
||||||
|
; Write message str1 to the console
|
||||||
invoke con_write_asciiz, str1
|
invoke con_write_asciiz, str1
|
||||||
|
|
||||||
|
; Allocate a TCP socket
|
||||||
mcall socket, AF_INET4, SOCK_STREAM, 0
|
mcall socket, AF_INET4, SOCK_STREAM, 0
|
||||||
cmp eax, -1
|
cmp eax, -1
|
||||||
je sock_err
|
je sock_err
|
||||||
|
; Socket allocation succeeded, store it's number in socketnum
|
||||||
mov [socketnum], eax
|
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,
|
; mcall setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
|
||||||
; cmp eax, -1
|
; cmp eax, -1
|
||||||
; je opt_err
|
; je opt_err
|
||||||
|
|
||||||
|
; Bind the socket to port 23 (as defined in sockaddr1)
|
||||||
mcall bind, [socketnum], sockaddr1, sockaddr1.length
|
mcall bind, [socketnum], sockaddr1, sockaddr1.length
|
||||||
cmp eax, -1
|
cmp eax, -1
|
||||||
je bind_err
|
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
|
cmp eax, -1
|
||||||
je listen_err
|
je listen_err
|
||||||
|
|
||||||
|
; Write message str2 to the console
|
||||||
invoke con_write_asciiz, str2
|
invoke con_write_asciiz, str2
|
||||||
|
|
||||||
|
; (Wait for and) accept incoming connection
|
||||||
mcall accept, [socketnum], sockaddr1, sockaddr1.length
|
mcall accept, [socketnum], sockaddr1, sockaddr1.length
|
||||||
cmp eax, -1
|
cmp eax, -1
|
||||||
je acpt_err
|
je acpt_err
|
||||||
|
; We have a new incoming connection, store the new socket number in socketnum2
|
||||||
mov [socketnum2], eax
|
mov [socketnum2], eax
|
||||||
|
|
||||||
|
; Send a message on the incoming connection
|
||||||
mcall send, [socketnum2], hello, hello.length
|
mcall send, [socketnum2], hello, hello.length
|
||||||
|
|
||||||
|
; Print the received data to the console, untill socket is closed by remote end
|
||||||
.loop:
|
.loop:
|
||||||
mcall recv, [socketnum2], buffer, buffer.length, 0
|
mcall recv, [socketnum2], buffer, BUFFERSIZE, 0
|
||||||
cmp eax, -1
|
cmp eax, -1
|
||||||
je .loop
|
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
|
invoke con_write_asciiz, buffer
|
||||||
jmp .loop
|
jmp .loop
|
||||||
|
|
||||||
|
; Print error message
|
||||||
acpt_err:
|
acpt_err:
|
||||||
invoke con_write_asciiz, str8
|
invoke con_write_asciiz, str8
|
||||||
jmp done
|
jmp done
|
||||||
@ -102,18 +115,25 @@ sock_err:
|
|||||||
invoke con_write_asciiz, str6
|
invoke con_write_asciiz, str6
|
||||||
jmp done
|
jmp done
|
||||||
|
|
||||||
|
|
||||||
done:
|
done:
|
||||||
invoke con_getch2 ; Wait for user input
|
; Wait for user input
|
||||||
|
invoke con_getch2
|
||||||
|
; Close console
|
||||||
invoke con_exit, 1
|
invoke con_exit, 1
|
||||||
exit:
|
exit:
|
||||||
|
; Close listening socket, if it is open
|
||||||
cmp [socketnum], 0
|
cmp [socketnum], 0
|
||||||
je @f
|
je @f
|
||||||
mcall close, [socketnum]
|
mcall close, [socketnum]
|
||||||
@@:
|
@@:
|
||||||
|
|
||||||
|
; Close second socket, if it is open
|
||||||
cmp [socketnum2], 0
|
cmp [socketnum2], 0
|
||||||
je @f
|
je @f
|
||||||
mcall close, [socketnum2]
|
mcall close, [socketnum2]
|
||||||
@@:
|
@@:
|
||||||
|
; Close application
|
||||||
mcall -1
|
mcall -1
|
||||||
|
|
||||||
|
|
||||||
@ -133,7 +153,7 @@ hello db 'Hello world!',0
|
|||||||
.length = $ - hello
|
.length = $ - hello
|
||||||
|
|
||||||
sockaddr1:
|
sockaddr1:
|
||||||
dw AF_INET4
|
dw AF_INET4 ; IPv4
|
||||||
.port dw 23 shl 8 ; port 23 - network byte order
|
.port dw 23 shl 8 ; port 23 - network byte order
|
||||||
.ip dd 0
|
.ip dd 0
|
||||||
rb 10
|
rb 10
|
||||||
@ -160,7 +180,6 @@ i_end:
|
|||||||
socketnum dd 0
|
socketnum dd 0
|
||||||
socketnum2 dd 0
|
socketnum2 dd 0
|
||||||
buffer rb BUFFERSIZE
|
buffer rb BUFFERSIZE
|
||||||
.length = BUFFERSIZE
|
|
||||||
|
|
||||||
align 4
|
align 4
|
||||||
rb 4096 ; stack
|
rb 4096 ; stack
|
||||||
|
Loading…
Reference in New Issue
Block a user