I am new to java client and server and I have been given a task to complete which does the following below:

Client:
c1. Connect to server via a connection-oriented socket.
c2. Read (from socket) and display the prompt message sent (see step s2 in server) by the server. c3. Receives and displays the first math question plus three possible answers from the server.
c4. User then selects and inputs the answer and the client program sends the answer to the server. This process is repeated three times.
c5. After the test is over the client accepts and displays the test result of all 3 questions from the server.
c6. Close the connection.

Server:
s0. [Initialization] Create a socket on one of the port numbers in the range above 1024.
s1.Wait for a client connection on this socket
s2.When a client connection is accepted send an acknowledgement (a welcome message) as a sting of text.
s3. Sends the first question plus three possible answers to the client.
s4. Receives the answer from the client. Stores and evaluates the answer. This process is repeated three times.
s5. After that the server sends the result to the client.
s6. Close the connection to the client and then loop back and wait for another connection.
Questions database:
Q1: (A + B)(A+B)
1. A
A + BB
2. A
A +AB + BB
3. AA +2AB + BB //(correct)

Q2: (A + B)(A - B)
1. A
A + 2BB
2. AA - BB //(correct)

Q3: sin(x)sin(x) + cos(x)cos(x)
1. 1 //(correct)
2. 2
3. 3

I have my client server program but i need help inplimenting these questions in my code. I am also try to build this in GUI and the same thing but for UDP. Can anyone help me?

My code:

Server:

import java.net.*;
import java.io.*;



    public class Server 
    {




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



    ServerSocket serverSocket = null;
    try 
    {
        serverSocket = new ServerSocket(1234);
    } 
    catch (IOException e) 
    {
        System.err.println("Could not listen on port: 1234.");
        System.exit(1);
    }

    Socket clientSocket = null;
    try 
    {
        clientSocket = serverSocket.accept();
    } 
    catch (IOException e) {
        System.err.println("Accept failed.");
    }
    System.out.println("Connected");


    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    String textFromClient;
    textFromClient = in.readLine(); // read the text from client
    System.out.println(textFromClient); 


    String textToClient = "Q1: (A+B)*(A+B)" + "answer the following\n"; //here is the question 
    textToClient = "1. A*A + B*B"; 
    textToClient = "2. A*A + A*B + B*B"; 
    textToClient = "3. A*A + 2*A*B + B*B"; 
    out.print(textToClient + "\r\n");  // send the response to client

    out.close();
    in.close();
    clientSocket.close();
    serverSocket.close();

}

Client: 
import java.net.*;
import java.io.*;

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

    Socket socket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

    try 
    {
        socket = new Socket("localhost", 1234);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } 
    catch (UnknownHostException e) {
        System.err.println("Don't know about host");
    } 
    catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection");
    }
    System.out.println("Connected");

    String textToServer;

    textToServer= read.readLine();
    out.print(textToServer + "\r\n" );  // send to server
    out.flush();

    System.out.println(in.readLine()); // read from server

    out.close();
    in.close();
    read.close();
    socket.close();
  }
}

Your first question is, is this a stateless server? In a stateless server, the storage of all intermediate state is achieved by sending it back to the client, which reiterates it on the next service request, like with browser cookies or embedded variables in dynamically generated response forms.

The stateful server needs to assign a state container with each connection, where the progress of that connection (session) is stored. These container could be created and destroyed or precreated empty and disconnected, used and then cleaned for reuse.

You need to design how the server will filter the client input against the session state, and how it will deal with invalid responses.

There should be logging of progress and errors of every session, so the app can be debugged and later production supported. The log should be easy to sort and filter for each session, one at a time in order. Since many sessions may simultaneously share the same log, all logging must be in full lines!

UDP is connectionless, so you need to create virtual TCP connectivity in your session state, to deal with lost packets and broken connections: keep-alive packets, client packet timeout, last packet storage for retransmisson, client session timeout. Even at close, you must preserve the session in case a retransmission is needed of the last packet. You have to write the protocol under UDP to make it into TCP! Steal! On the plus side, one thread and one socket can service infinite clients, as life is just a packet in and a packet out, like DNS, plus periodic scans for timed out sessions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.