FileDocCategorySizeDatePackage
ChatterServer.javaAPI DocExample3750Mon Apr 06 18:10:22 BST 1998None

ChatterServer

public class ChatterServer extends Object

Fields Summary
static final int
INPORT
private byte[]
buf
private DatagramPacket
dp
private DatagramSocket
socket
Constructors Summary
public ChatterServer()


    
    try {
      socket = new DatagramSocket(INPORT);
      System.out.println("Server started");
      while(true) {
        // Block until a datagram appears:
        socket.receive(dp);
        String rcvd = Dgram.toString(dp) +
          ", from address: " + dp.getAddress() +
          ", port: " + dp.getPort();
        System.out.println(rcvd);
        String echoString = 
          "Echoed: " + rcvd;
        // Extract the address and port from the
        // received datagram to find out where to
        // send it back:
        DatagramPacket echo = 
          Dgram.toDatagram(echoString,
            dp.getAddress(), dp.getPort());
        socket.send(echo);
      }
    } catch(SocketException e) {
      System.err.println("Can't open socket");
      System.exit(1);
    } catch(IOException e) {
      System.err.println("Communication error");
      e.printStackTrace();
    }
  
Methods Summary
public static voidmain(java.lang.String[] args)

    new ChatterServer();