Echo server: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 458:
A [[/AutoHotkey Client|client]] is also available for testing this code.
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
<syntaxhighlight lang="bacon">OPEN "localhost:12321" FOR SERVER AS echo
WHILE TRUE
Line 485 ⟶ 486:
Incoming connection from: 127.0.0.1:36784</pre>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SOCKLIB"
Line 552 ⟶ 553:
Connection on socket 1012 closed
</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">NewMap RecData.s()
OpenWindow(0, 100, 200, 200, 100, "Echo Server", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget )
InitNetwork()
CreateNetworkServer(1, 12321)
 
Repeat
Event = NetworkServerEvent()
ClientID = EventClient()
If Event = #PB_NetworkEvent_Connect ; When a new client has been connected...
AddMapElement(RecData(), Str(ClientID))
ElseIf Event = #PB_NetworkEvent_Data
*Buffer = AllocateMemory(20000)
count = ReceiveNetworkData(ClientID, *Buffer, 20000)
For i = 1 To count
RecData(Str(ClientID)) + Mid( PeekS(*Buffer, count), i , 1)
If Right( RecData(Str(ClientID)), 2) = #CRLF$
SendNetworkString (ClientID, RecData(Str(ClientID)))
Debug IPString(GetClientIP(ClientID)) + ":" + Str(GetClientPort(ClientID)) + " " + RecData(Str(ClientID))
RecData(Str(ClientID)) = ""
EndIf
Next
FreeMemory(*Buffer)
ElseIf Event = #PB_NetworkEvent_Disconnect ; When a client has closed the connection...
DeleteMapElement(RecData(), Str(ClientID))
EndIf
 
Event = WaitWindowEvent(10)
Until Event = #PB_Event_CloseWindow</syntaxhighlight>
 
==={{header|REALbasic}}===
This example uses the built-in ServerSocket class to handle multiple users.
<syntaxhighlight lang="vb">
Class EchoSocket
Inherits TCPSocket
Sub DataAvailable()
If Instr(Me.LookAhead, EndofLine.Windows) > 0 Then
Dim data As String = Me.ReadAll
Dim lines() As String = Split(data, EndofLine.Windows)
For i As Integer = 0 To Ubound(lines)
Me.Write(lines(i) + EndOfLine.Windows)
Print(lines(i))
Next
End If
End Sub
End Class
 
Class EchoServer
Inherits ServerSocket
Function AddSocket() As TCPSocket
Return New EchoSocket
End Function
End Class
 
Class App
Inherits ConsoleApplication
Function Run(args() As String) As Integer
Listener = New EchoServer
Listener.Port = 12321
Listener.Listen()
While True
DoEvents() 'pump the event loop
Wend
End Function
Private Listener As EchoServer
End Class
</syntaxhighlight>
 
=={{header|C}}==
Line 1,533 ⟶ 1,605:
table.insert(connections, newClient)
end</syntaxhighlight>
----
 
{{works with|Luvit}}
 
<syntaxhighlight lang="lua">local http = require("http")
 
http.createServer(function(req, res)
print(("Connection from %s"):format(req.socket:address().ip))
 
local chunks = {}
local function dumpChunks()
for i=1,#chunks do
res:write(table.remove(chunks, 1))
end
end
 
req:on("data", function(data)
for line, nl in data:gmatch("([^\n]+)(\n?)") do
if nl == "\n" then
dumpChunks()
res:write(line)
res:write("\n")
else
table.insert(chunks, line)
end
end
end)
 
req:on("end", function()
dumpChunks()
res:finish()
end)
end):listen(12321, "127.0.0.1")
 
print("Server running at http://127.0.0.1:12321/")</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 1,857 ⟶ 1,964:
(prinl (stamp) " -- (Pid " *Pid ") Client disconnected")
(bye) # Terminate child</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">NewMap RecData.s()
OpenWindow(0, 100, 200, 200, 100, "Echo Server", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget )
InitNetwork()
CreateNetworkServer(1, 12321)
 
Repeat
Event = NetworkServerEvent()
ClientID = EventClient()
If Event = #PB_NetworkEvent_Connect ; When a new client has been connected...
AddMapElement(RecData(), Str(ClientID))
ElseIf Event = #PB_NetworkEvent_Data
*Buffer = AllocateMemory(20000)
count = ReceiveNetworkData(ClientID, *Buffer, 20000)
For i = 1 To count
RecData(Str(ClientID)) + Mid( PeekS(*Buffer, count), i , 1)
If Right( RecData(Str(ClientID)), 2) = #CRLF$
SendNetworkString (ClientID, RecData(Str(ClientID)))
Debug IPString(GetClientIP(ClientID)) + ":" + Str(GetClientPort(ClientID)) + " " + RecData(Str(ClientID))
RecData(Str(ClientID)) = ""
EndIf
Next
FreeMemory(*Buffer)
ElseIf Event = #PB_NetworkEvent_Disconnect ; When a client has closed the connection...
DeleteMapElement(RecData(), Str(ClientID))
EndIf
 
Event = WaitWindowEvent(10)
Until Event = #PB_Event_CloseWindow</syntaxhighlight>
 
=={{header|Python}}==
Line 2,073 ⟶ 2,147:
}
}
</syntaxhighlight>
 
=={{header|REALbasic}}==
 
This example uses the built-in ServerSocket class to handle multiple users.
<syntaxhighlight lang="vb">
Class EchoSocket
Inherits TCPSocket
Sub DataAvailable()
If Instr(Me.LookAhead, EndofLine.Windows) > 0 Then
Dim data As String = Me.ReadAll
Dim lines() As String = Split(data, EndofLine.Windows)
For i As Integer = 0 To Ubound(lines)
Me.Write(lines(i) + EndOfLine.Windows)
Print(lines(i))
Next
End If
End Sub
End Class
 
Class EchoServer
Inherits ServerSocket
Function AddSocket() As TCPSocket
Return New EchoSocket
End Function
End Class
 
Class App
Inherits ConsoleApplication
Function Run(args() As String) As Integer
Listener = New EchoServer
Listener.Port = 12321
Listener.Listen()
While True
DoEvents() 'pump the event loop
Wend
End Function
Private Listener As EchoServer
End Class
</syntaxhighlight>
 
Line 2,541 ⟶ 2,576:
{{trans|C}}
An embedded program so we can ask the C host to call the relevant library functions for us and also handle simultaneous connections from multiple clients using a multi-process approach.
<syntaxhighlight lang="ecmascriptwren">/* echo_serverEcho_server.wren */
 
var MAX_ENQUEUED = 20
Line 2,682 ⟶ 2,717:
<br>
which we now embed in the following C program, build and run:
<syntaxhighlight lang="c">/* gcc echo_serverEcho_server.c -o echo_serverEcho_server -lwren -lm */
 
#include <stdio.h>
Line 2,965 ⟶ 3,000:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "echo_serverEcho_server.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 2,987 ⟶ 3,022:
<pre>
/* start server on terminal 1 */
$ ./echo_serverEcho_server
 
/* start telnet on terminal 2 and type 'hello' */
Line 3,057 ⟶ 3,092:
{{omit from|M4}}
{{omit from|Maxima}}
{{omit from|Minimal BASIC}}
{{omit from|ML/I}}
{{omit from|Palo Alto Tiny BASIC}}
{{omit from|PARI/GP}}
{{omit from|PL/0}}
{{omit from|Retro|No concurrency support}}
{{omit from|SNUSP|No networking.}}
{{omit from|Tiny BASIC}}
{{omit from|Unlambda|Does not have network access.}}
{{omit from|Commodore BASIC}}
9,482

edits