-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_for_multiple_clients.kt
208 lines (181 loc) · 5.6 KB
/
server_for_multiple_clients.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import java.net.ServerSocket
import kotlin.concurrent.thread
import java.net.Socket
import java.io.File
var clients : MutableList<Socket> = mutableListOf()
var names : MutableList<String> = mutableListOf()
var mentes1 : MutableList<String> = mutableListOf()
var mentes2 : MutableList<String> = mutableListOf()
var mentes3 = ""
val database = File("database.txt")
fun main(){
val server = ServerSocket(12345)
var i = 0
var szo = ""
var command = ""
var alany = ""
thread{
while (true){
command = readLine()!!
if (":" in command){
command = command.split(":")[0]
alany = command.split(":")[1]
}
if (command == "!save"){
save()
}//else if(command == "!ban"){
// val index = names.index(alany)
// clients.remove(clients[index])
//
//}
}
}
while(true){
println("-- --$i")
val client = server.accept()
clients.add(client)
println("--")
thread(name = "client"){
var o = false
var id = i-1
var name = ""
var l = false
var m = false
var v = false
var n = false
var f = false
var ff = false
var fff = false
var namesetting = false
//val commands = listOf("l", "")
var letter = ""
println("new thread with id = $id")
while(true){
var input = client.getInputStream().read()
//println(input)
if (namesetting == true){
if (input != 10){
name += input.toChar()
}else{
names += name
println("name = $name")
namesetting = false
}
}
if (n == true){
if (input == 110){
namesetting = true
}
n = false
}
if (l == true){
if (input == 108){
//var a = clients[id]
client.close()
clients.remove(client)
names.remove(name)
break
}else{
l = false
println("l= false")
}
}
if(m == true){
if (input == 115){
save()
}else{
m = false
println("m = false")
}
}
if (o == true){
if (input == 111){
println("online")
for (name in names){
client.getOutputStream().write(name.toByteArray() + 10)
}
}else{
o = false
}
}
if (v == true){
if (input == 118){
println("load")
val data : List<String> = database.readLines()
client.getOutputStream().write(byteArrayOf(45, 45, 108, 111, 97, 100, 101, 100, 45, 45, 10))
for (i in data){
client.getOutputStream().write((i + "\n").toByteArray())
}
}else{
v = false
println("v = false")
}
}
if(input == 33){
l = true
m = true
v = true
o = true
n = true
f = true
ff = true
fff = true
println("! received")
}
if(fff!=true){
print(input.toChar())
for (client in clients){
client.getOutputStream().write(input)
}
if (input != 10){
szo += input.toChar()
}else{
szo += "\n"
mentes1 += szo
szo = ""
save()
}
}
if (ff != true){
fff = false
}
if (f == true){
f=false
ff = false
}
letter = ""
//if (input != 10){
// szo += input.toChar()
//}else{
// szo += "\n"
// mentes1 += szo
// szo = ""
// save()
//}
}
}
i++
println("--")
}
}
fun save(){
val data : List<String> = database.readLines()
println("data = ")
for (i in data){
println(i)
}
for (i in data){
mentes2 += (i+"\n")
}
for (i in mentes1){
mentes2 += i
}
for (i in mentes2){
mentes3 += i
}
database.writeText(mentes3)
println(mentes3)
mentes1 = mutableListOf()
mentes2 = mutableListOf()
mentes3 = ""
}