Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3618 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
5859 hidnplayr 3
;; Copyright (C) KolibriOS team 2010-2015. All rights reserved.    ;;
3618 hidnplayr 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
 
3545 hidnplayr 15
format binary as ""
16
 
3618 hidnplayr 17
BUFFERSIZE      = 1500
18
 
3545 hidnplayr 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
 
3618 hidnplayr 31
include '../../macros.inc'
3545 hidnplayr 32
purge mov,add,sub
3618 hidnplayr 33
include '../../proc32.inc'
34
include '../../dll.inc'
3545 hidnplayr 35
 
3618 hidnplayr 36
include '../../network.inc'
3545 hidnplayr 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
5859 hidnplayr 46
        invoke  con_start, 1
47
        invoke  con_init, 80, 25, 80, 25, title
3545 hidnplayr 48
 
5859 hidnplayr 49
        mcall   40, EVM_STACK
3545 hidnplayr 50
 
5859 hidnplayr 51
        invoke  con_write_asciiz, str1
3545 hidnplayr 52
 
53
        mcall   socket, AF_INET4, SOCK_STREAM, 0
54
        cmp     eax, -1
55
        je      sock_err
56
        mov     [socketnum], eax
57
 
5859 hidnplayr 58
; This socket option is not implemented in kernel yet.
59
;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
60
;        cmp     eax, -1
61
;        je      opt_err
3545 hidnplayr 62
 
63
        mcall   bind, [socketnum], sockaddr1, sockaddr1.length
64
        cmp     eax, -1
65
        je      bind_err
66
 
67
        mcall   listen, [socketnum], 10 ; Backlog = 10
68
        cmp     eax, -1
69
        je      listen_err
70
 
5859 hidnplayr 71
        invoke  con_write_asciiz, str2
3545 hidnplayr 72
 
73
        mcall   accept, [socketnum], sockaddr1, sockaddr1.length
74
        cmp     eax, -1
75
        je      acpt_err
76
        mov     [socketnum2], eax
77
 
78
        mcall   send, [socketnum2], hello, hello.length
79
 
80
  .loop:
3562 hidnplayr 81
        mcall   recv, [socketnum2], buffer, buffer.length, 0
3545 hidnplayr 82
        cmp     eax, -1
83
        je      .loop
84
 
5859 hidnplayr 85
        mov     byte[buffer+eax], 0
86
        invoke  con_write_asciiz, buffer
3545 hidnplayr 87
        jmp     .loop
88
 
89
acpt_err:
5859 hidnplayr 90
        invoke  con_write_asciiz, str8
3545 hidnplayr 91
        jmp     done
92
 
93
listen_err:
5859 hidnplayr 94
        invoke  con_write_asciiz, str3
3545 hidnplayr 95
        jmp     done
96
 
97
bind_err:
5859 hidnplayr 98
        invoke  con_write_asciiz, str4
3545 hidnplayr 99
        jmp     done
100
 
101
sock_err:
5859 hidnplayr 102
        invoke  con_write_asciiz, str6
3545 hidnplayr 103
        jmp     done
104
 
105
done:
5859 hidnplayr 106
        invoke  con_getch2      ; Wait for user input
107
        invoke  con_exit, 1
3545 hidnplayr 108
exit:
5859 hidnplayr 109
        cmp     [socketnum], 0
110
        je      @f
111
        mcall   close, [socketnum]
112
  @@:
113
        cmp     [socketnum2], 0
114
        je      @f
115
        mcall   close, [socketnum2]
116
  @@:
3545 hidnplayr 117
        mcall   -1
118
 
119
 
120
 
121
; data
5859 hidnplayr 122
title   db      'TCP stream server demo',0
3545 hidnplayr 123
str1    db      'Opening socket',10, 0
124
str2    db      'Listening for incoming connections...',10,0
125
str3    db      'Listen error',10,10,0
126
str4    db      'Bind error',10,10,0
5859 hidnplayr 127
str5    db      'Setsockopt error',10,10,0
3545 hidnplayr 128
str6    db      'Could not open socket',10,10,0
5859 hidnplayr 129
str7    db      'Got data',10,10,0
3545 hidnplayr 130
str8    db      'Error accepting connection',10,10,0
131
 
132
hello   db      'Hello world!',0
133
.length = $ - hello
134
 
135
sockaddr1:
136
        dw AF_INET4
5859 hidnplayr 137
.port   dw 23 shl 8             ; port 23 - network byte order
3545 hidnplayr 138
.ip     dd 0
139
        rb 10
140
.length = $ - sockaddr1
141
 
142
; import
143
align 4
144
@IMPORT:
145
 
146
library console, 'console.obj'
147
 
148
import  console,        \
149
        con_start,      'START',        \
150
        con_init,       'con_init',     \
151
        con_write_asciiz,       'con_write_asciiz',     \
152
        con_exit,       'con_exit',     \
153
        con_gets,       'con_gets',\
154
        con_cls,        'con_cls',\
155
        con_printf,     'con_printf',\
156
        con_getch2,     'con_getch2',\
157
        con_set_cursor_pos, 'con_set_cursor_pos'
158
i_end:
159
 
5859 hidnplayr 160
socketnum       dd 0
161
socketnum2      dd 0
162
buffer          rb BUFFERSIZE
3545 hidnplayr 163
.length = BUFFERSIZE
164
 
165
align   4
166
rb      4096    ; stack
167
mem: