Rev 3618 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3618 | hidnplayr | 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 | |||
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 |
||
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: |
||
3562 | hidnplayr | 94 | mcall recv, [socketnum2], buffer, buffer.length, 0 |
3545 | hidnplayr | 95 | cmp eax, -1 |
96 | je .loop |
||
97 | |||
98 | mov byte [buffer + eax], 0 |
||
99 | |||
100 | push buffer |
||
101 | call [con_write_asciiz] |
||
102 | |||
103 | jmp .loop |
||
104 | |||
105 | acpt_err: |
||
106 | push str8 |
||
107 | call [con_write_asciiz] |
||
108 | jmp done |
||
109 | |||
110 | listen_err: |
||
111 | push str3 |
||
112 | call [con_write_asciiz] |
||
113 | jmp done |
||
114 | |||
115 | bind_err: |
||
116 | push str4 |
||
117 | call [con_write_asciiz] |
||
118 | jmp done |
||
119 | |||
120 | sock_err: |
||
121 | push str6 |
||
122 | call [con_write_asciiz] |
||
123 | jmp done |
||
124 | |||
125 | done: |
||
126 | call [con_getch2] |
||
127 | push 1 |
||
128 | call [con_exit] |
||
129 | exit: |
||
130 | mcall -1 |
||
131 | |||
132 | |||
133 | |||
134 | ; data |
||
135 | title db 'TCP stream server - test',0 |
||
136 | str1 db 'Opening socket',10, 0 |
||
137 | str2 db 'Listening for incoming connections...',10,0 |
||
138 | str3 db 'Listen error',10,10,0 |
||
139 | str4 db 'Bind error',10,10,0 |
||
140 | str5 db 'Setsockopt error.',10,10,0 |
||
141 | str6 db 'Could not open socket',10,10,0 |
||
142 | str7 db 'Got data!',10,10,0 |
||
143 | str8 db 'Error accepting connection',10,10,0 |
||
144 | |||
145 | hello db 'Hello world!',0 |
||
146 | .length = $ - hello |
||
147 | |||
148 | sockaddr1: |
||
149 | dw AF_INET4 |
||
150 | .port dw 0x1700 ; 23 |
||
151 | .ip dd 0 |
||
152 | rb 10 |
||
153 | .length = $ - sockaddr1 |
||
154 | |||
155 | ; import |
||
156 | align 4 |
||
157 | @IMPORT: |
||
158 | |||
159 | library console, 'console.obj' |
||
160 | |||
161 | import console, \ |
||
162 | con_start, 'START', \ |
||
163 | con_init, 'con_init', \ |
||
164 | con_write_asciiz, 'con_write_asciiz', \ |
||
165 | con_exit, 'con_exit', \ |
||
166 | con_gets, 'con_gets',\ |
||
167 | con_cls, 'con_cls',\ |
||
168 | con_printf, 'con_printf',\ |
||
169 | con_getch2, 'con_getch2',\ |
||
170 | con_set_cursor_pos, 'con_set_cursor_pos' |
||
171 | i_end: |
||
172 | |||
173 | socketnum dd ? |
||
174 | socketnum2 dd ? |
||
175 | buffer rb BUFFERSIZE |
||
176 | .length = BUFFERSIZE |
||
177 | |||
178 | align 4 |
||
179 | rb 4096 ; stack |
||
180 | mem: |