September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
51
Task/Echo-server/Aime/echo-server.aime
Normal file
51
Task/Echo-server/Aime/echo-server.aime
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
void
|
||||
readc(dispatch w, file i, file o, data b)
|
||||
{
|
||||
integer e;
|
||||
data t;
|
||||
|
||||
while (1) {
|
||||
e = f_b_read(i, t, 1 << 10);
|
||||
if (e < 1) {
|
||||
if (e == -1) {
|
||||
w_resign(w, i);
|
||||
}
|
||||
|
||||
break;
|
||||
} else {
|
||||
e = b_frame(t, '\n');
|
||||
if (e != -1) {
|
||||
e += 1;
|
||||
b_rule(b, -1, t, 0, e);
|
||||
f_data(o, b);
|
||||
w_register(w, o);
|
||||
b_bill(b, t, e, ~t - e);
|
||||
} else {
|
||||
b_add(b, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
serve(dispatch w, file s)
|
||||
{
|
||||
file i, o;
|
||||
data b;
|
||||
|
||||
accept(i, o, s, NONBLOCKING_INPUT | NONBLOCKING_OUTPUT);
|
||||
w_watch(w, i, readc, w, i, o, b);
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
dispatch w;
|
||||
file s;
|
||||
|
||||
tcpip_listen(s, 12321, 0);
|
||||
w_watch(w, s, serve, w, s);
|
||||
w_press(w);
|
||||
|
||||
return 0;
|
||||
}
|
||||
49
Task/Echo-server/Kotlin/echo-server.kotlin
Normal file
49
Task/Echo-server/Kotlin/echo-server.kotlin
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// version 1.1.3
|
||||
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.io.PrintWriter
|
||||
import java.net.ServerSocket
|
||||
import java.net.Socket
|
||||
|
||||
class ClientHandler(private val clientSocket: Socket): Runnable {
|
||||
private val connectionId: Int
|
||||
|
||||
init {
|
||||
connectionId = ++numConnections
|
||||
println("Handling connection, #$connectionId")
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
val pw = PrintWriter(clientSocket.outputStream, true)
|
||||
val br = BufferedReader(InputStreamReader(clientSocket.inputStream))
|
||||
while (true) {
|
||||
val line = br.readLine() ?: break
|
||||
println("Received: $line")
|
||||
pw.write("$line\n")
|
||||
pw.flush()
|
||||
if (line == "exit") break
|
||||
}
|
||||
br.close()
|
||||
pw.close()
|
||||
clientSocket.close()
|
||||
println("Closing connection, #$connectionId")
|
||||
}
|
||||
|
||||
private companion object {
|
||||
var numConnections = 0
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val serverSocket = ServerSocket(12321)
|
||||
try {
|
||||
while (true) {
|
||||
Thread(ClientHandler(serverSocket.accept())).start()
|
||||
}
|
||||
}
|
||||
finally {
|
||||
serverSocket.close()
|
||||
println("Closing server socket")
|
||||
}
|
||||
}
|
||||
16
Task/Echo-server/Zkl/echo-server.zkl
Normal file
16
Task/Echo-server/Zkl/echo-server.zkl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
const PORT=12321;
|
||||
pipe:=Thread.Pipe(); // how server tells thread to connect to user
|
||||
|
||||
fcn echo(socket){ // a thread, one per connection
|
||||
text:=Data();
|
||||
while(t:=socket.read()){
|
||||
text.append(t);
|
||||
if(text.find("\n",text.cursor)){ text.readln().print(); }
|
||||
}
|
||||
// socket was closed
|
||||
}
|
||||
|
||||
// Set up the server socket.
|
||||
server:=Network.TCPServerSocket.open(PORT);
|
||||
println("Listening on %s:%s".fmt(server.hostname,server.port));
|
||||
server.listen(echo.launch); // Main event loop
|
||||
Loading…
Add table
Add a link
Reference in a new issue