Hello guys.
I need to write a client-server app that provides a simple Lotto server and client to generate a user-defined sequence of random numbers in the range 1 - 42. The system operates as follows:
1) Client sends a string message to the server indicating the operation to be performed and the number of random numbers to generate (e.g. “GENERATE%4”).
2) Server parses the string message and generates the random numbers using the Math::random(), Math::round() and Integer::parseInt() methods.
3) Server sends a reply string (e.g. “GEN%4%21%13%30%11%6”) to the client with the numbers generated.
That's what Im basing on now:
public class LottoClient {
ObjectInputStream Sinput; // to read the socker
ObjectOutputStream Soutput; // towrite on the socket
Socket socket;
// Constructor connection receiving a socket number
LottoClient(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
try {
socket = new Socket("localhost", port);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// now that I have my connection
String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
// send the string to the server
System.out.println("Client sending \"" + test + "\" to serveur");
try {
Soutput.writeObject(test);
Soutput.flush();
}
catch(IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server: " + e);
}
try{
Sinput.close();
Soutput.close();
}
catch(Exception e) {}
}
public static void main(String[] arg) {
new LottoClient(1500);
}
}