SquareServer.java Source code

Java tutorial

Introduction

Here is the source code for SquareServer.java

Source

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

class SquareServer {
    private final static int BUFSIZE = 20;

    public static void main(String args[]) throws Exception {
        int port = Integer.parseInt(args[0]);
        ServerSocket ss = new ServerSocket(port);
        while (true) {
            Socket s = ss.accept();
            InputStream is = s.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            double value = dis.readDouble();
            value *= value;

            OutputStream os = s.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            dos.writeDouble(value);

            s.close();
        }
    }
}