Created
October 14, 2010 01:45
-
-
Save yuya-takeyama/625380 to your computer and use it in GitHub Desktop.
Various implementations of echo server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$server = socket_create_listen(12345); | |
echo "Listening..." . PHP_EOL; | |
while (true) { | |
$client = socket_accept($server); | |
$receivedInput = chop(socket_read($client, 1024)); | |
echo "Received Input: " . $receivedInput . PHP_EOL; | |
if (strlen($receivedInput) === 0) { | |
break; | |
} | |
socket_write($client, $receivedInput . PHP_EOL); | |
socket_close($client); | |
} | |
socket_close($server); | |
echo "Good bye." . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Socket; | |
socket(SERVER, PF_INET, SOCK_STREAM, 0); | |
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1); | |
bind(SERVER, pack_sockaddr_in(12345, INADDR_ANY)); | |
listen(SERVER, SOMAXCONN); | |
print "Listening...$/"; | |
while (1) { | |
my $client = accept(CLIENT, SERVER); | |
my ($client_port, $client_iaddr) = unpack_sockaddr_in($client); | |
my ($client_ip) = inet_ntoa($client_iaddr); | |
my $received_input = <CLIENT>; | |
$received_input =~ s/(\r\n|\r|\n)$//g; | |
print "Received Input: $received_input$/"; | |
last if ($received_input eq ''); | |
print CLIENT $received_input . $/; | |
close(CLIENT); | |
} | |
close(SERVER); | |
print "Good bye.$/"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
server.bind(('', 12345)) | |
server.listen(5) | |
print "Listening..." | |
while 1: | |
client, addr = server.accept() | |
received_input = client.recv(1024).rstrip() | |
print received_input | |
if received_input == '': | |
break | |
client.send(received_input + "\n") | |
client.close() | |
print "Good bye." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
TCPServer.open(12345) do |server| | |
puts "Listening..." | |
loop do | |
client = server.accept | |
received_input = client.gets.chomp | |
puts "Received Input: " + received_input | |
break if received_input.size == 0 | |
client.puts received_input | |
client.close | |
end | |
end | |
puts "Good bye." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment