Echo server: Difference between revisions

From Rosetta Code
Content added Content deleted
(add Ruby)
(Added Python example)
Line 149: Line 149:
(echo-server 12321)
(echo-server 12321)
</lang>
</lang>

=={{header|Python}}==
{{works with|Python|2.3 or above}}
<lang python>import SocketServer

HOST = "localhost"
PORT = 12321

# this server uses ThreadingMixIn - one thread per connection
# replace with ForkMixIn to spawn a new process per connection

class EchoServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
# no need to override anything - default behavior is just fine
pass

class EchoRequestHandler(SocketServer.StreamRequestHandler):
"""
Handles one connection to the client.
"""
def handle(self):
print "connection from %s" % self.client_address[0]
while True:
line = self.rfile.readline()
if not line: break
print "%s wrote: %s" % (self.client_address[0], line.rstrip())
self.wfile.write(line)
print "%s disconnected" % self.client_address[0]


# Create the server
server = EchoServer((HOST, PORT), EchoRequestHandler)

# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
print "server listening on %s:%s" % server.server_address
server.serve_forever()</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==

Revision as of 07:49, 21 June 2009

Task
Echo server
You are encouraged to solve this task according to the task description, using any language you may know.

Create a network service that sits on TCP port 12321, which accepts connections on that port, and which echoes complete lines (using a carriage-return/line-feed sequence as line separator) back to clients. No error handling is required. For the purposes of testing, it is only necessary to support connections from localhost (127.0.0.1). Logging of connection information to standard output is recommended.

The implementation must be able to handle simultaneous connections from multiple clients. A multi-threaded solution may be used.

The implementation must not stop responding to other clients if one client sends a partial line or stops reading responses.

C

Works with: POSIX

This is a rather standard code (details apart); the reference guide for such a code is the Beej's Guide to Network programming. The dependency from POSIX is mainly in the use of the read and write functions, (using the socket as a file descriptor sometimes make things simpler).

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <string.h>
  3. include <errno.h>
  4. include <sys/types.h>
  5. include <sys/socket.h>
  6. include <netdb.h>
  7. include <unistd.h>
  8. include <sys/wait.h>
  9. include <signal.h>
  1. define MAX_ENQUEUED 20
  2. define BUF_LEN 256
  3. define PORT_STR "12321"

void wait_for_zombie(int s) {

  while(waitpid(-1, NULL, WNOHANG) > 0) ;

}

int main() {

   struct addrinfo hints, *res;
   struct sockaddr addr;
   struct sigaction sa;
   socklen_t addr_size;
   int sock;
   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_family = AF_UNSPEC;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags = AI_PASSIVE;
   if ( getaddrinfo(NULL, PORT_STR, &hints, &res) != 0 ) {
       perror("getaddrinfo");
       exit(EXIT_FAILURE);
   }
   if ( (sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1 ) {
       perror("socket");
       exit(EXIT_FAILURE);
   }
   sa.sa_handler = wait_for_zombie;
   sigemptyset(&sa.sa_mask);
   sa.sa_flags = SA_RESTART;
   if ( sigaction(SIGCHLD, &sa, NULL) == - 1 ) {
       perror("sigaction");
       exit(EXIT_FAILURE);
   }
   if ( bind(sock, res->ai_addr, res->ai_addrlen) == 0 ) {
       freeaddrinfo(res);
       if ( listen(sock, MAX_ENQUEUED) == 0 ) {
           /* Serve the listening socket infinitely often */
           for(;;) {
               addr_size = sizeof(addr);
               int csock = accept(sock, &addr, &addr_size);
               if ( csock == -1 ) {
                   perror("accept");
               } else {
                   if ( fork() == 0 ) {
                       close(sock);
                       /* Echo loop */
                       char buf[BUF_LEN];
                       int r;
                       while( (r = read(csock, buf, BUF_LEN)) > 0 ) {
                           (void)write(csock, buf, r);
                       }
                       exit(EXIT_SUCCESS);
                   }
                   close(csock);
               }
           }
       } else {
           perror("listen");
           exit(EXIT_FAILURE);
       }
   } else {
       perror("bind");
       exit(EXIT_FAILURE);
   }
   return EXIT_SUCCESS;

}</lang>

Common Lisp

Sockets is not a standard part of Common Lisp but many implementations have support for this. The following example

Works with: CLISP

<lang lisp>(defvar *clients* '()

   "This is a list of (socket :input status) which is used with

`socket:socket-status' to test for data ready on a socket.")

(defun echo-server (port)

   "Listen on `port' for new client connections and for data arriving on

any existing client connections"

   (let ((server (socket:socket-server port)))
       (format t "Echo service listening on port ~a:~d~%"
           (socket:socket-server-host server)
           (socket:socket-server-port server))
       (unwind-protect
           (loop 
               (when (socket:socket-status server 0 1)
                   (echo-accept-client (socket:socket-accept server 
                                           :external-format :dos
                                           :buffered t)))
               (when *clients*
                   (socket:socket-status *clients* 0 1)
                   (mapcar #'(lambda (client)
                                 (when (eq :input (cddr client))
                                     (echo-service-client (car client)))
                                 (when (eq :eof (cddr client))
                                     (echo-close-client (car client)))) *clients*)))
           (socket-server-close server))))

(defun echo-accept-client (socket)

   "Accept a new client connection and add it to the watch list."
   (multiple-value-bind 
       (host port) (socket:socket-stream-peer socket)
       (format t "Connect from ~a:~d~%" host port))
   (push (list socket :input nil) *clients*))
   

(defun echo-service-client (socket)

   (let ((line (read-line socket nil nil)))
       (princ line socket)
       (finish-output socket)))

(defun echo-close-client (socket)

   "Close a client connection and remove it from the watch list."
   (multiple-value-bind 
       (host port) (socket:socket-stream-peer socket)
       (format t "Closing connection from ~a:~d~%" host port))
   (close socket)
   (setq *clients* (remove socket *clients* :key #'car)))

(echo-server 12321) </lang>

Python

Works with: Python version 2.3 or above

<lang python>import SocketServer

HOST = "localhost" PORT = 12321

  1. this server uses ThreadingMixIn - one thread per connection
  2. replace with ForkMixIn to spawn a new process per connection

class EchoServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):

   # no need to override anything - default behavior is just fine
   pass

class EchoRequestHandler(SocketServer.StreamRequestHandler):

   """
   Handles one connection to the client.
   """
   def handle(self):
       print "connection from %s" % self.client_address[0]
       while True:
           line = self.rfile.readline()
           if not line: break
           print "%s wrote: %s" % (self.client_address[0], line.rstrip())
           self.wfile.write(line)
       print "%s disconnected" % self.client_address[0]


  1. Create the server

server = EchoServer((HOST, PORT), EchoRequestHandler)

  1. Activate the server; this will keep running until you
  2. interrupt the program with Ctrl-C

print "server listening on %s:%s" % server.server_address server.serve_forever()</lang>

Ruby

<lang ruby>require 'socket' server = TCPServer.new('localhost', 12321) while (session = server.accept)

 client_port, client_host = session.peeraddr[1..2]
 puts "activity from #{client_host}:#{client_port}"
 line = session.gets
 if line.nil?
   session.close
 else
   session.puts(line)
 end

end</lang>

Tcl

<lang tcl># How to handle an incoming new connection proc acceptEcho {chan host port} {

   puts "opened connection from $host:$port"
   fconfigure $chan -blocking 0 -buffering line -translation crlf
   fileevent $chan readable [list echo $chan $host $port]

}

  1. How to handle an incoming message on a connection

proc echo {chan host port} {

   if {[gets $chan line] >= 0} {
       puts $chan $line
   } elseif {[eof $chan]} {
       close $chan
       puts "closed connection from $host:$port"
   }
   # Other conditions causing a short read need no action

}

  1. Make the server socket and wait for connections

socket -server acceptEcho -myaddr localhost 12321 vwait forever</lang>

Alternative version

A more succinct version (though one harder to adapt to other kinds of services, but closer to the standard unix echo daemon since it has no line-buffering) is to use an asynchronous binary copy. <lang tcl># How to handle an incoming new connection proc acceptEcho {chan host port} {

   puts "opened connection from $host:$port"
   fconfigure $chan -translation binary -buffering none
   fcopy $chan $chan -command [list done $chan $host $port]

}

  1. Called to finalize the connection

proc done {chan host port args} {

   puts "closed connection from $host:$port"
   close $chan

}

  1. Make the server socket and wait for connections

socket -server acceptEcho -myaddr localhost 12321 vwait forever</lang>