Subversion Repositories Kolibri OS

Rev

Rev 5859 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2010-2017. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  tcpserv.asm - TCP server demo program for KolibriOS            ;;
  7. ;;                                                                 ;;
  8. ;;  Written by hidnplayr@kolibrios.org                             ;;
  9. ;;                                                                 ;;
  10. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  11. ;;             Version 2, June 1991                                ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. format binary as ""
  16.  
  17. BUFFERSIZE      = 1500
  18.  
  19. use32
  20. ; standard header
  21.         db      'MENUET01'      ; signature
  22.         dd      1               ; header version
  23.         dd      start           ; entry point
  24.         dd      i_end           ; initialized size
  25.         dd      mem             ; required memory
  26.         dd      mem             ; stack pointer
  27.         dd      0               ; parameters
  28.         dd      0               ; path
  29.  
  30.  
  31. include '../../macros.inc'
  32. purge mov,add,sub
  33. include '../../proc32.inc'
  34. include '../../dll.inc'
  35.  
  36. include '../../network.inc'
  37.  
  38. ; entry point
  39. start:
  40. ; load libraries
  41.         stdcall dll.Load, @IMPORT
  42.         test    eax, eax
  43.         jnz     exit
  44.  
  45. ; initialize console
  46.         invoke  con_start, 1
  47.         invoke  con_init, 80, 25, 80, 25, title
  48.  
  49. ; Set event mask to socket events only
  50.         mcall   40, EVM_STACK
  51.  
  52. ; Write message str1 to the console
  53.         invoke  con_write_asciiz, str1
  54.  
  55. ; Allocate a TCP socket
  56.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  57.         cmp     eax, -1
  58.         je      sock_err
  59. ; Socket allocation succeeded, store it's number in socketnum
  60.         mov     [socketnum], eax
  61.  
  62. ; This might be needed in the future,
  63. ; SO_REUSEADDR option is not implemented in kernel yet.
  64. ;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
  65. ;        cmp     eax, -1
  66. ;        je      opt_err
  67.  
  68. ; Bind the socket to port 23 (as defined in sockaddr1)
  69.         mcall   bind, [socketnum], sockaddr1, sockaddr1.length
  70.         cmp     eax, -1
  71.         je      bind_err
  72.  
  73. ; Start listening for incoming connections, with a backlog of max 1
  74.         mcall   listen, [socketnum], 1
  75.         cmp     eax, -1
  76.         je      listen_err
  77.  
  78. ; Write message str2 to the console
  79.         invoke  con_write_asciiz, str2
  80.  
  81. ; (Wait for and) accept incoming connection
  82.         mcall   accept, [socketnum], sockaddr1, sockaddr1.length
  83.         cmp     eax, -1
  84.         je      acpt_err
  85. ; We have a new incoming connection, store the new socket number in socketnum2
  86.         mov     [socketnum2], eax
  87.  
  88. ; Send a message on the incoming connection
  89.         mcall   send, [socketnum2], hello, hello.length
  90.  
  91. ; Print the received data to the console, untill socket is closed by remote end
  92.   .loop:
  93.         mcall   recv, [socketnum2], buffer, BUFFERSIZE, 0
  94.         cmp     eax, -1
  95.         je      .loop
  96.  
  97.         mov     byte[buffer+eax], 0             ; Zero-terminate the data, so we can print it
  98.         invoke  con_write_asciiz, buffer
  99.         jmp     .loop
  100.  
  101. ; Print error message
  102. acpt_err:
  103.         invoke  con_write_asciiz, str8
  104.         jmp     done
  105.  
  106. listen_err:
  107.         invoke  con_write_asciiz, str3
  108.         jmp     done
  109.  
  110. bind_err:
  111.         invoke  con_write_asciiz, str4
  112.         jmp     done
  113.  
  114. sock_err:
  115.         invoke  con_write_asciiz, str6
  116.         jmp     done
  117.  
  118.  
  119. done:
  120. ; Wait for user input
  121.         invoke  con_getch2
  122. ; Close console
  123.         invoke  con_exit, 1
  124. exit:
  125. ; Close listening socket, if it is open
  126.         cmp     [socketnum], 0
  127.         je      @f
  128.         mcall   close, [socketnum]
  129.   @@:
  130.  
  131. ; Close second socket, if it is open
  132.         cmp     [socketnum2], 0
  133.         je      @f
  134.         mcall   close, [socketnum2]
  135.   @@:
  136. ; Close application
  137.         mcall   -1
  138.  
  139.  
  140.  
  141. ; data
  142. title   db      'TCP stream server demo',0
  143. str1    db      'Opening socket',10, 0
  144. str2    db      'Listening for incoming connections...',10,0
  145. str3    db      'Listen error',10,10,0
  146. str4    db      'Bind error',10,10,0
  147. str5    db      'Setsockopt error',10,10,0
  148. str6    db      'Could not open socket',10,10,0
  149. str7    db      'Got data',10,10,0
  150. str8    db      'Error accepting connection',10,10,0
  151.  
  152. hello   db      'Hello world!',0
  153. .length = $ - hello
  154.  
  155. sockaddr1:
  156.         dw AF_INET4             ; IPv4
  157. .port   dw 23 shl 8             ; port 23 - network byte order
  158. .ip     dd 0
  159.         rb 10
  160. .length = $ - sockaddr1
  161.  
  162. ; import
  163. align 4
  164. @IMPORT:
  165.  
  166. library console, 'console.obj'
  167.  
  168. import  console,        \
  169.         con_start,      'START',        \
  170.         con_init,       'con_init',     \
  171.         con_write_asciiz,       'con_write_asciiz',     \
  172.         con_exit,       'con_exit',     \
  173.         con_gets,       'con_gets',\
  174.         con_cls,        'con_cls',\
  175.         con_printf,     'con_printf',\
  176.         con_getch2,     'con_getch2',\
  177.         con_set_cursor_pos, 'con_set_cursor_pos'
  178. i_end:
  179.  
  180. socketnum       dd 0
  181. socketnum2      dd 0
  182. buffer          rb BUFFERSIZE
  183.  
  184. align   4
  185. rb      4096    ; stack
  186. mem:
  187.