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. AA + BB
2. AA +AB + BB
3. AA +2AB + BB //(correct)
Q2: (A + B)(A - B)
1. AA + 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();
}
}