Subversion Repositories Kolibri OS

Rev

Rev 3562 | Rev 3704 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2010-2013. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  tcpserv.asm - TCP 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.         push    1
  47.         call    [con_start]
  48.         push    title
  49.         push    25
  50.         push    80
  51.         push    25
  52.         push    80
  53.         call    [con_init]
  54.  
  55.         mcall   40, 1 shl 7     ; we only want network events
  56.  
  57.         push    str1
  58.         call    [con_write_asciiz]
  59.  
  60.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  61.         cmp     eax, -1
  62.         je      sock_err
  63.  
  64.         mov     [socketnum], eax
  65.  
  66. ;;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
  67. ;;        cmp     eax, -1
  68. ;;        je      opt_err
  69.  
  70.         mcall   bind, [socketnum], sockaddr1, sockaddr1.length
  71.         cmp     eax, -1
  72.         je      bind_err
  73.  
  74.         mcall   listen, [socketnum], 10 ; Backlog = 10
  75.         cmp     eax, -1
  76.         je      listen_err
  77.  
  78.         push    str2
  79.         call    [con_write_asciiz]
  80.  
  81.         mcall   10
  82.  
  83.         mcall   accept, [socketnum], sockaddr1, sockaddr1.length
  84.         cmp     eax, -1
  85.         je      acpt_err
  86.  
  87.         mov     [socketnum2], eax
  88.  
  89. ;;        mcall   close, [socketnum]
  90.  
  91.         mcall   send, [socketnum2], hello, hello.length
  92.  
  93.   .loop:
  94.         mcall   10
  95.  
  96.         mcall   recv, [socketnum2], buffer, buffer.length, 0
  97.         cmp     eax, -1
  98.         je      .loop
  99.  
  100.         mov     byte [buffer + eax], 0
  101.  
  102.         push    buffer
  103.         call    [con_write_asciiz]
  104.  
  105.         jmp     .loop
  106.  
  107. acpt_err:
  108.         push    str8
  109.         call    [con_write_asciiz]
  110.         jmp     done
  111.  
  112. listen_err:
  113.         push    str3
  114.         call    [con_write_asciiz]
  115.         jmp     done
  116.  
  117. bind_err:
  118.         push    str4
  119.         call    [con_write_asciiz]
  120.         jmp     done
  121.  
  122. sock_err:
  123.         push    str6
  124.         call    [con_write_asciiz]
  125.         jmp     done
  126.  
  127. done:
  128.         call    [con_getch2]
  129.         push    1
  130.         call    [con_exit]
  131. exit:
  132.         mcall   -1
  133.  
  134.  
  135.  
  136. ; data
  137. title   db      'TCP stream server - test',0
  138. str1    db      'Opening socket',10, 0
  139. str2    db      'Listening for incoming connections...',10,0
  140. str3    db      'Listen error',10,10,0
  141. str4    db      'Bind error',10,10,0
  142. str5    db      'Setsockopt error.',10,10,0
  143. str6    db      'Could not open socket',10,10,0
  144. str7    db      'Got data!',10,10,0
  145. str8    db      'Error accepting connection',10,10,0
  146.  
  147. hello   db      'Hello world!',0
  148. .length = $ - hello
  149.  
  150. sockaddr1:
  151.         dw AF_INET4
  152. .port   dw 0x1700       ; 23
  153. .ip     dd 0
  154.         rb 10
  155. .length = $ - sockaddr1
  156.  
  157. ; import
  158. align 4
  159. @IMPORT:
  160.  
  161. library console, 'console.obj'
  162.  
  163. import  console,        \
  164.         con_start,      'START',        \
  165.         con_init,       'con_init',     \
  166.         con_write_asciiz,       'con_write_asciiz',     \
  167.         con_exit,       'con_exit',     \
  168.         con_gets,       'con_gets',\
  169.         con_cls,        'con_cls',\
  170.         con_printf,     'con_printf',\
  171.         con_getch2,     'con_getch2',\
  172.         con_set_cursor_pos, 'con_set_cursor_pos'
  173. i_end:
  174.  
  175. socketnum       dd ?
  176. socketnum2      dd ?
  177. buffer         rb BUFFERSIZE
  178. .length = BUFFERSIZE
  179.  
  180. align   4
  181. rb      4096    ; stack
  182. mem:
  183.