Clienti Java (doua versiuni) pentru problema precedenta:
Client.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.net.*; import java.io.*; public class Client { public static void main(String args[]) throws Exception { Socket c = new Socket("127.0.0.1", 1234); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int a, b, s; System.out.print("a = "); a = Integer.parseInt(reader.readLine()); System.out.print("b = "); b = Integer.parseInt(reader.readLine()); DataInputStream socketIn = new DataInputStream(c.getInputStream()); DataOutputStream socketOut = new DataOutputStream(c.getOutputStream()); socketOut.writeShort(a); socketOut.writeShort(b); socketOut.flush(); s = socketIn.readUnsignedShort(); System.out.println("s = " + s); reader.close(); c.close(); } } |
Client2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import java.net.*; import java.io.*; public class Client2 { private static final String SERVER_ADDRESS = "127.0.0.1"; private static final int SERVER_PORT = 1234; private static final int UNSIGNED_SHORT_MAX_VALUE = 65535; private static final int UNSIGNED_SHORT_MIN_VALUE = 0; public static void main(String args[]) { Socket socket = null; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(System.in)); socket = new Socket(SERVER_ADDRESS, SERVER_PORT); int a = readUnsignedShort("a = ", reader); int b = readUnsignedShort("b = ", reader); writeIntegersToSocket(a, b, socket); readIntegersSumFromSocket(socket); } catch (IOException e) { System.err.println("Cautgh exception " + e.getMessage()); } finally { closeStreams(socket,reader); } } private static void readIntegersSumFromSocket(Socket c) throws IOException { DataInputStream socketIn = new DataInputStream(c.getInputStream()); int s = socketIn.readUnsignedShort(); System.out.println("s = " + s); } private static void writeIntegersToSocket(int a, int b, Socket c) throws IOException { DataOutputStream socketOut = new DataOutputStream(c.getOutputStream()); socketOut.writeShort(a); socketOut.writeShort(b); socketOut.flush(); } private static int readUnsignedShort(String message, BufferedReader reader) throws IOException { int unsignedShortNumber = 0; System.out.print(message); try { unsignedShortNumber = Integer.parseInt(reader.readLine()); if (unsignedShortNumber < UNSIGNED_SHORT_MIN_VALUE || unsignedShortNumber > UNSIGNED_SHORT_MAX_VALUE) { throw new IllegalArgumentException("The given number must be unsigned short [0..65535]!"); } } catch (NumberFormatException e) { System.err.println("The given input is not an integer!"); } return unsignedShortNumber; } private static void closeStreams(Socket socket, BufferedReader reader) { if (socket != null) { try { socket.close(); } catch (IOException e) { System.err.println("Could not close socket!"); } } if (reader != null) { try { reader.close(); } catch (IOException e) { System.err.println("Could not close reader!"); } } } } |