Rev 5859 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5859 | Rev 7108 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2010-2015. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2010-2017. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; tcpserv.asm - TCP demo program for KolibriOS ;; |
6 | ;; tcpserv.asm - TCP server demo program for KolibriOS ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Written by hidnplayr@kolibrios.org ;; |
8 | ;; Written by hidnplayr@kolibrios.org ;; |
9 | ;; ;; |
9 | ;; ;; |
10 | ;; GNU GENERAL PUBLIC LICENSE ;; |
10 | ;; GNU GENERAL PUBLIC LICENSE ;; |
11 | ;; Version 2, June 1991 ;; |
11 | ;; Version 2, June 1991 ;; |
Line 44... | Line 44... | ||
44 | 44 | ||
45 | ; initialize console |
45 | ; initialize console |
46 | invoke con_start, 1 |
46 | invoke con_start, 1 |
Line -... | Line 47... | ||
- | 47 | invoke con_init, 80, 25, 80, 25, title |
|
47 | invoke con_init, 80, 25, 80, 25, title |
48 | |
Line -... | Line 49... | ||
- | 49 | ; Set event mask to socket events only |
|
48 | 50 | mcall 40, EVM_STACK |
|
Line -... | Line 51... | ||
- | 51 | ||
49 | mcall 40, EVM_STACK |
52 | ; Write message str1 to the console |
50 | 53 | invoke con_write_asciiz, str1 |
|
51 | invoke con_write_asciiz, str1 |
54 | |
- | 55 | ; Allocate a TCP socket |
|
52 | 56 | mcall socket, AF_INET4, SOCK_STREAM, 0 |
|
Line -... | Line 57... | ||
- | 57 | cmp eax, -1 |
|
53 | mcall socket, AF_INET4, SOCK_STREAM, 0 |
58 | je sock_err |
54 | cmp eax, -1 |
59 | ; Socket allocation succeeded, store it's number in socketnum |
55 | je sock_err |
60 | mov [socketnum], eax |
56 | mov [socketnum], eax |
61 | |
Line -... | Line 62... | ||
- | 62 | ; This might be needed in the future, |
|
57 | 63 | ; SO_REUSEADDR option is not implemented in kernel yet. |
|
58 | ; This socket option is not implemented in kernel yet. |
64 | ; mcall setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes, |
59 | ; mcall setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes, |
65 | ; cmp eax, -1 |
Line -... | Line 66... | ||
- | 66 | ; je opt_err |
|
60 | ; cmp eax, -1 |
67 | |
61 | ; je opt_err |
68 | ; Bind the socket to port 23 (as defined in sockaddr1) |
62 | 69 | mcall bind, [socketnum], sockaddr1, sockaddr1.length |
|
Line -... | Line 70... | ||
- | 70 | cmp eax, -1 |
|
63 | mcall bind, [socketnum], sockaddr1, sockaddr1.length |
71 | je bind_err |
Line -... | Line 72... | ||
- | 72 | ||
64 | cmp eax, -1 |
73 | ; Start listening for incoming connections, with a backlog of max 1 |
65 | je bind_err |
74 | mcall listen, [socketnum], 1 |
66 | 75 | cmp eax, -1 |
|
- | 76 | je listen_err |
|
67 | mcall listen, [socketnum], 10 ; Backlog = 10 |
77 | |
Line -... | Line 78... | ||
- | 78 | ; Write message str2 to the console |
|
68 | cmp eax, -1 |
79 | invoke con_write_asciiz, str2 |
Line -... | Line 80... | ||
- | 80 | ||
69 | je listen_err |
81 | ; (Wait for and) accept incoming connection |
70 | 82 | mcall accept, [socketnum], sockaddr1, sockaddr1.length |
|
71 | invoke con_write_asciiz, str2 |
83 | cmp eax, -1 |
72 | 84 | je acpt_err |
|
Line 73... | Line 85... | ||
73 | mcall accept, [socketnum], sockaddr1, sockaddr1.length |
85 | ; We have a new incoming connection, store the new socket number in socketnum2 |
74 | cmp eax, -1 |
86 | mov [socketnum2], eax |
75 | je acpt_err |
87 | |
Line -... | Line 88... | ||
- | 88 | ; Send a message on the incoming connection |
|
76 | mov [socketnum2], eax |
89 | mcall send, [socketnum2], hello, hello.length |
77 | 90 | ||
78 | mcall send, [socketnum2], hello, hello.length |
91 | ; Print the received data to the console, untill socket is closed by remote end |
Line 79... | Line 92... | ||
79 | 92 | .loop: |
|
Line 100... | Line 113... | ||
100 | 113 | ||
101 | sock_err: |
114 | sock_err: |
102 | invoke con_write_asciiz, str6 |
115 | invoke con_write_asciiz, str6 |
Line -... | Line 116... | ||
- | 116 | jmp done |
|
103 | jmp done |
117 | |
- | 118 | ||
104 | 119 | done: |
|
- | 120 | ; Wait for user input |
|
105 | done: |
121 | invoke con_getch2 |
106 | invoke con_getch2 ; Wait for user input |
122 | ; Close console |
- | 123 | invoke con_exit, 1 |
|
107 | invoke con_exit, 1 |
124 | exit: |
108 | exit: |
125 | ; Close listening socket, if it is open |
109 | cmp [socketnum], 0 |
126 | cmp [socketnum], 0 |
110 | je @f |
127 | je @f |
- | 128 | mcall close, [socketnum] |
|
- | 129 | @@: |
|
111 | mcall close, [socketnum] |
130 | |
112 | @@: |
131 | ; Close second socket, if it is open |
113 | cmp [socketnum2], 0 |
132 | cmp [socketnum2], 0 |
114 | je @f |
133 | je @f |
- | 134 | mcall close, [socketnum2] |
|
115 | mcall close, [socketnum2] |
135 | @@: |
Line 116... | Line 136... | ||
116 | @@: |
136 | ; Close application |
Line 131... | Line 151... | ||
131 | 151 | ||
132 | hello db 'Hello world!',0 |
152 | hello db 'Hello world!',0 |
Line 133... | Line 153... | ||
133 | .length = $ - hello |
153 | .length = $ - hello |
134 | 154 | ||
135 | sockaddr1: |
155 | sockaddr1: |
136 | dw AF_INET4 |
156 | dw AF_INET4 ; IPv4 |
137 | .port dw 23 shl 8 ; port 23 - network byte order |
157 | .port dw 23 shl 8 ; port 23 - network byte order |
138 | .ip dd 0 |
158 | .ip dd 0 |
Line 158... | Line 178... | ||
158 | i_end: |
178 | i_end: |
Line 159... | Line 179... | ||
159 | 179 | ||
160 | socketnum dd 0 |
180 | socketnum dd 0 |
161 | socketnum2 dd 0 |
181 | socketnum2 dd 0 |
162 | buffer rb BUFFERSIZE |
- | |
Line 163... | Line 182... | ||
163 | .length = BUFFERSIZE |
182 | buffer rb BUFFERSIZE |
164 | 183 | ||
165 | align 4 |
184 | align 4 |