Вы находитесь на странице: 1из 3

Create a server application that can create objects and run java methods for its clients.

1
Create a server application that can accept connection from the client. The client should be able to send textual messages to the server (the
messages are terminated by newline), and the server should respond by sending back the message to the user. The server should listen on the
port 2345.

The client should be a simple socket client program that send the message that the user typed and prints out the servers response:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

public class Client {

public static void main(String[] args) throws IOException {

try (Socket socket = new Socket("127.0.0.1", 2345)) {

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

BufferedReader clientIn = new BufferedReader(new InputStreamReader(System.in));

while (true) {

System.out.print("> ");

String line = clientIn.readLine();

out.println(line);

String recvLine = in.readLine();

System.out.println("- " + recvLine);

The server should store a map of objects with string keys.

When the user sends a message: create <classname> <name> <list of quoted strings> , the server should create a new object using reflection.
The server should invoke the correct constructor of the class classname . The server should store the resulting object for the key name and
respond with its toString() value.

The strings will not contain spaces or newlines. Invoking the constructor with different arguments than the declared parameters results in an
exception that can be caught.

Example:

> create java.lang.Integer i "42"

- 42

2
Modify the server to enable multiple clients connecting to the same server at the same time.
When the user sends a get <name> command, the server should respond with the toString() value of the object stored for name .

When using the create operation, if the user gives simple names instead of quoted strings, than the already existing objects will be used as
parameters.

> create java.lang.Integer i "42"

- 42

> create java.lang.Integer j "3"

- 3

> create Point p i j

- Point [x=42, y=3]

3
When the user sends exec <obj> <meth> <store> <list of arguments> the server should execute the method meth on the object obj , with

the given arguments (quotes strings or object names), store the result for the name store .

> create java.lang.Integer i "42"

- 42

> create java.lang.Integer j "3"

- 3

> create Point p i j

- Point [x=42, y=3]

> create java.lang.Integer zero "0"

- 0

> exec p translate p2 zero j

- Point [x=42, y=6]

4
The server should handle the following errors:

• The client disconnects.


• The given class or method does not exist.
• There is no object for the given name.

It should also be able to resolve cases when there are overloaded methods, and should be able to select the correct method for the arguments.

The quoted strings may contain spaces. Modify the server to handle spaces inside quotes.

One possible interaction with the server:

> create java.lang.Integer i "42"

- 42

> create java.lang.Integer j "3"

- 3

> create Point p i j

- Point [x=42, y=3]

> create java.lang.Integer zero "0"

- 0

> exec p translate p2 zero j

- Point [x=42, y=6]

> exec p neighbours n

- [Point [x=43, y=3], Point [x=41, y=3], Point [x=43, y=4], Point [x=43, y=2], Point [x=41, y=4], Point [x=41, y=2], Point [x=42, y=

4], Point [x=42, y=2]]

> get n

- [Point [x=43, y=3], Point [x=41, y=3], Point [x=43, y=4], Point [x=43, y=2], Point [x=41, y=4], Point [x=41, y=2], Point [x=42, y=

4], Point [x=42, y=2]]


When another client connects it can see the objects:

> get n

- [Point [x=43, y=3], Point [x=41, y=3], Point [x=43, y=4], Point [x=43, y=2], Point [x=41, y=4], Point [x=41, y=2], Point [x=42, y=

4], Point [x=42, y=2]]

Вам также может понравиться