# Io Reference

Networking / Socket / Server

|  | The Server object provides a simple interface for running a server. You just need to set the port and define a handleSocket method. Here's an example of an echo server:
    Echo := Object clone
    Echo handleSocketFromServer := method(aSocket, aServer,
      write("[Got echo connection from ", aSocket host, "]\n")
      while(aSocket isOpen,
       if(aSocket read, aSocket write(aSocket readBuffer asString))
       aSocket readBuffer empty
      )
      write("[Closed ", aSocket host, "]\n")
    )

    write("[Starting echo server on port 8456]\n")
    server := Server clone setPort(8456)
    server handleSocket := method(aSocket,
      Echo clone @handleSocketFromServer(aSocket, self)
    )
    server start
Notes Io's use of lightweight threading and select for dealing with sockets makes for servers that are much more efficient (both memory and cpu wise) than those written with kernel threads and socket polling.
|  | handleSocket(aSocket) This method is called when the server accepts a new socket. The new socket is passed as the argument. Override this method in your own server subclass. The default implementation raises an exception. port Returns the port on which the server will listen for connections. setHost(hostName) Sets the hostName. Returns self. setPort(aNumber) Sets the port on which the server will listen for connections. Returns self. start Starts the server. This method will not return until server is stopped, so you may want to send the start message as an asynchronous message. Returns self or an Error, if one occurs. stop Stops the server if it is running. Returns self.