Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3618 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
7108 hidnplayr 3
;; Copyright (C) KolibriOS team 2010-2017. All rights reserved.    ;;
3618 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
7108 hidnplayr 6
;;  tcpserv.asm - TCP server demo program for KolibriOS            ;;
3618 hidnplayr 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
 
7108 hidnplayr 49
; Set event mask to socket events only
5859 hidnplayr 50
        mcall   40, EVM_STACK
3545 hidnplayr 51
 
7108 hidnplayr 52
; Write message str1 to the console
5859 hidnplayr 53
        invoke  con_write_asciiz, str1
3545 hidnplayr 54
 
7108 hidnplayr 55
; Allocate a TCP socket
3545 hidnplayr 56
        mcall   socket, AF_INET4, SOCK_STREAM, 0
57
        cmp     eax, -1
58
        je      sock_err
7108 hidnplayr 59
; Socket allocation succeeded, store it's number in socketnum
3545 hidnplayr 60
        mov     [socketnum], eax
61
 
7108 hidnplayr 62
; This might be needed in the future,
63
; SO_REUSEADDR option is not implemented in kernel yet.
5859 hidnplayr 64
;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
65
;        cmp     eax, -1
66
;        je      opt_err
3545 hidnplayr 67
 
7108 hidnplayr 68
; Bind the socket to port 23 (as defined in sockaddr1)
3545 hidnplayr 69
        mcall   bind, [socketnum], sockaddr1, sockaddr1.length
70
        cmp     eax, -1
71
        je      bind_err
72
 
7108 hidnplayr 73
; Start listening for incoming connections, with a backlog of max 1
74
        mcall   listen, [socketnum], 1
3545 hidnplayr 75
        cmp     eax, -1
76
        je      listen_err
77
 
7108 hidnplayr 78
; Write message str2 to the console
5859 hidnplayr 79
        invoke  con_write_asciiz, str2
3545 hidnplayr 80
 
7108 hidnplayr 81
; (Wait for and) accept incoming connection
3545 hidnplayr 82
        mcall   accept, [socketnum], sockaddr1, sockaddr1.length
83
        cmp     eax, -1
84
        je      acpt_err
7108 hidnplayr 85
; We have a new incoming connection, store the new socket number in socketnum2
3545 hidnplayr 86
        mov     [socketnum2], eax
87
 
7108 hidnplayr 88
; Send a message on the incoming connection
3545 hidnplayr 89
        mcall   send, [socketnum2], hello, hello.length
90
 
7108 hidnplayr 91
; Print the received data to the console, untill socket is closed by remote end
3545 hidnplayr 92
  .loop:
7108 hidnplayr 93
        mcall   recv, [socketnum2], buffer, BUFFERSIZE, 0
3545 hidnplayr 94
        cmp     eax, -1
95
        je      .loop
96
 
7108 hidnplayr 97
        mov     byte[buffer+eax], 0             ; Zero-terminate the data, so we can print it
5859 hidnplayr 98
        invoke  con_write_asciiz, buffer
3545 hidnplayr 99
        jmp     .loop
100
 
7108 hidnplayr 101
; Print error message
3545 hidnplayr 102
acpt_err:
5859 hidnplayr 103
        invoke  con_write_asciiz, str8
3545 hidnplayr 104
        jmp     done
105
 
106
listen_err:
5859 hidnplayr 107
        invoke  con_write_asciiz, str3
3545 hidnplayr 108
        jmp     done
109
 
110
bind_err:
5859 hidnplayr 111
        invoke  con_write_asciiz, str4
3545 hidnplayr 112
        jmp     done
113
 
114
sock_err:
5859 hidnplayr 115
        invoke  con_write_asciiz, str6
3545 hidnplayr 116
        jmp     done
117
 
7108 hidnplayr 118
 
3545 hidnplayr 119
done:
7108 hidnplayr 120
; Wait for user input
121
        invoke  con_getch2
122
; Close console
5859 hidnplayr 123
        invoke  con_exit, 1
3545 hidnplayr 124
exit:
7108 hidnplayr 125
; Close listening socket, if it is open
5859 hidnplayr 126
        cmp     [socketnum], 0
127
        je      @f
128
        mcall   close, [socketnum]
129
  @@:
7108 hidnplayr 130
 
131
; Close second socket, if it is open
5859 hidnplayr 132
        cmp     [socketnum2], 0
133
        je      @f
134
        mcall   close, [socketnum2]
135
  @@:
7108 hidnplayr 136
; Close application
3545 hidnplayr 137
        mcall   -1
138
 
139
 
140
 
141
; data
5859 hidnplayr 142
title   db      'TCP stream server demo',0
3545 hidnplayr 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
5859 hidnplayr 147
str5    db      'Setsockopt error',10,10,0
3545 hidnplayr 148
str6    db      'Could not open socket',10,10,0
5859 hidnplayr 149
str7    db      'Got data',10,10,0
3545 hidnplayr 150
str8    db      'Error accepting connection',10,10,0
151
 
152
hello   db      'Hello world!',0
153
.length = $ - hello
154
 
155
sockaddr1:
7108 hidnplayr 156
        dw AF_INET4             ; IPv4
5859 hidnplayr 157
.port   dw 23 shl 8             ; port 23 - network byte order
3545 hidnplayr 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
 
5859 hidnplayr 180
socketnum       dd 0
181
socketnum2      dd 0
182
buffer          rb BUFFERSIZE
3545 hidnplayr 183
 
184
align   4
185
rb      4096    ; stack
186
mem: