The Way to Programming
The Way to Programming
Would really love some help on these from somebody that knows java / android. This should be simple apparently… An i have been given some examples but i cant get them to work. Please find attached my Eclipse workspace so you can look at / edit what i have done. Would really like some help asap, Much <3
You are required to develop and design a solution, in Java.. The system should be developed to show:
1. A Server responding to a telnet session
2. The Android Client and Server working together
Server
package diceserver; import java.io.*; import java.net.*; public class DiceGameServer { private static int port=4444, maxConnections=0; // Listen for incoming connections and handle them public static void main(String[] args) { boolean quitServer = false; try{ ServerSocket listener = new ServerSocket(port); Socket server; do { server = listener.accept(); GameRequestHandler handler = new GameRequestHandler(server); handler.handle(); quitServer = handler.isQuitServer(); } while (!quitServer); } catch (IOException e) { System.out.println(“IOException on socket listen: ” + e); e.printStackTrace(); } } /** * @param args the command line arguments */ /* public static void main(String[] args) { Dice diceone = new Dice(); Dice dicetwo = new Dice(2); for (int i=0; i<10; i++) { Roll roll = diceone.roll(); System.out.println("Single Roll : " + roll.getNumber() + " = " + roll.getPips()); roll = dicetwo.roll(); System.out.println("Double \" : " + roll.getNumber() + " = " + roll.getPips()); } } */ }
ServerGame Request Handler
package diceserver; import java.io.*; import java.net.*; import diceserver.data.Dice; import diceserver.data.Roll; public class GameRequestHandler { private Socket server; private boolean quitServer = false; private Integer parsedNumber = null; GameRequestHandler(Socket server) { this.server=server; } public void handle() // set carryOn to false when it's time to exit { try { String input = getInput(); parseLine(input); sendOutput(); server.close(); } catch (IOException e) { System.out.println("IOException on socket listen: " + e); e.printStackTrace(); setQuitServer(true); } } private String getInput() throws IOException { // Get input from the client BufferedReader in = new BufferedReader( new InputStreamReader(server.getInputStream())); return in.readLine(); } private void parseLine(String input) { if (input != null) { if (input.equals("XYZZY")) // quit the server when the magic code is entered { setQuitServer(true); setParsedNumber(null); } else { try { setParsedNumber(Integer.parseInt(input)); } catch (NumberFormatException ex) { setParsedNumber(null); System.out.println("Warning: '"+input+"' not parsed as Integer"); } } } } /** * @return the quitServer */ public boolean isQuitServer() { return quitServer; } /** * @param quitServer the quitServer to set */ public void setQuitServer(boolean quitServer) { this.quitServer = quitServer; } /** * @return the parsedNumber */ public Integer getParsedNumber() { return parsedNumber; } /** * @param parsedNumber the parsedNumber to set */ public void setParsedNumber(Integer parsedNumber) { this.parsedNumber = parsedNumber; } private void sendOutput() throws IOException { PrintStream out = new PrintStream(server.getOutputStream()); if (getParsedNumber() != null) { Dice dice = new Dice(getParsedNumber().intValue()); Roll roll = dice.roll(); out.println(roll.getPips()); System.out.println("Overall message is:" + roll.getPips()); } out.close(); } }
Dice
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package diceserver.data; import java.util.Random; public class Dice { private int count=1; private Random random = new Random(); private int rollNumber = 1; public Dice() { } public Dice(int icount) // pass in the number of dice to roll { count = icount; } public Roll roll() { int pipsSum = 0; // sum/total of pips rolled Roll result = new Roll(); result.setNumber(rollNumber++); for (int i=0; iRoll
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package diceserver.data; public class Roll { private int number = 0; private int pips = 0; /** * @return the number */ public int getNumber() { return number; } /** * @param number the number to set */ public void setNumber(int number) { this.number = number; } /** * @return the pips */ public int getPips() { return pips; } /** * @param pips the pips to set */ public void setPips(int pips) { this.pips = pips; } }ANDROID APP
package uk.ac.shu.dpt.diceclient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText diceCountText; private TextView rolledView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); diceCountText = (EditText) findViewById(R.id.diceCountText); rolledView = (TextView) findViewById(R.id.rolledView); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onRollClick(View view) { String diceStr = diceCountText.getText().toString(); Integer Roll = Integer.parseInt(diceStr); if (Roll == null) { rolledView.setText("Please enter a valid number..."); } else { try { int rolls = Roll.intValue(); Socket diceSocket = new Socket("10.0.2.2", 4444); PrintWriter pw = new PrintWriter(diceSocket.getOutputStream()); pw.println(rolls); pw.flush(); BufferedReader br = new BufferedReader( new InputStreamReader(diceSocket.getInputStream())); String response = br.readLine(); Integer iresponse = Integer.parseInt(response); if (iresponse == null) { System.out.println(" Server returned unrecognised response, exiting..."); } else { System.out.println("Rolled a total of: " + iresponse); rolledView.setText(response); } pw.close(); br.close(); diceSocket.close(); } catch (IOException ex) { System.out.println(" Unexpected exception..."); ex.printStackTrace(); } } } }
Sign in to your account