Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************SKELETON********************/
- import java.io.*;
- import java.net.*;
- public class skeleton {
- public static void main(String[] args) {
- try {
- InetAddress address = InetAddress.getByName("www.dmi.unict.it");
- Socket client = new Socket(address, 80);
- BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- PrintWriter out = new PrintWriter(client.getOutputStream(), true);
- }
- catch (Exception e) { e.printStackTrace(); }
- }
- }
- /********************SERVER BANCA********************/
- /*
- Implementare in C o Java, su localhost, un programma server banca, connection-oriented, che:
- mantenga un array di 10 interi conto che rappresentano il saldo dei conti da 0 a 9;
- risponda, su localhost, port 7777 ai seguenti messaggi:
- [Un] dove n, da 0 a 9, è il numero del conto;
- l'effetto sarà di rendere il conto n quello attuale, cioè su cui operano implicitamente gli altri comandi;
- [Vxyzw] dove xyzw sono 4 cifre intere;
- l'effetto sarà di versare la somma xyzw sul conto attuale (cioè l'ultimo selezionato con il comando [Un])
- [Pxyzw] dove xyzw sono 4 cifre intere;
- l'effetto sarà di prelevare la somma xyzw dal conto attuale (cioè l'ultimo selezionato con il comando [Un])
- [S] l'effetto sarà di inviare al cliente il saldo depositato sul conto attuale (cioè l'ultimo selezionato con il comando [Un])
- risponda ERROR a ogni altro dato ricevuto dal cliente
- */
- package bank;
- /**
- *
- * @author Ludovico
- */
- import java.io.*;
- import java.net.*;
- public class server {
- public static int conto[];
- private static int user;
- private static ServerSocket server;
- private static Socket client;
- private static BufferedReader in;
- private static PrintWriter out;
- public static boolean isInt(String str)
- {
- try
- {
- Integer.parseInt(str);
- return true;
- }
- catch(Exception e)
- {
- out.println("ERROR");
- return false;
- }
- }
- public static void service(String input_string)
- {
- try
- {
- int leng = input_string.length();
- int x;
- switch(input_string.substring(0,1))
- {
- case "U":
- if(isInt(input_string.substring(1))){
- user = Integer.parseInt(input_string.substring(1));
- out.println("Conto corrente selezionato: "+ user);
- }
- break;
- case "V":
- if(isInt(input_string.substring(1))){
- x = Integer.parseInt(input_string.substring(1));
- conto[user] += x;
- out.println("Varsamento di "+x+" sul conto corrente "+user);
- }
- break;
- case "P":
- if(isInt(input_string.substring(1))){
- x = Integer.parseInt(input_string.substring(1));
- conto[user] -= x;
- out.println("Prelievo di "+x+" sul conto corrente "+user);
- }
- break;
- case "S":
- out.println("Saldo attuale di "+conto[user]+" del conto corrente "+user);
- break;
- default:
- out.println("Server_ERROR");
- break;
- }
- }
- catch(Exception e)
- {
- out.println("ERROR");
- }
- }
- public static void main(String[] args)
- {
- try
- {
- conto = new int[10];
- user = 0;
- server = new ServerSocket(7777);
- System.out.println("Server Pronto");
- while(true)
- {
- client = server.accept();
- in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
- service(in.readLine());
- }
- }
- catch(Exception e)
- {
- out.println("ERROR");
- }
- }
- }
- /********************CLIENT BANCA********************/
- package bank;
- import java.net.*;
- import java.io.*;
- /**
- *
- * @author Ludovico
- */
- public class Client_bank {
- public static void main(String[] args)
- {
- try
- {
- InetAddress address = InetAddress.getByName("localhost");
- Socket client = new Socket(address, 7777);
- BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- BufferedReader in_console = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
- System.out.println("Inserisci una funzione tra U - V - P - S");
- String str = "";
- while(true)
- {
- if(str.equals("close"))
- break;
- System.out.println("Wait...");
- str = in_console.readLine();
- out.println(str);
- System.out.println(in.readLine());
- client.close();
- in.close();
- out.close();
- client = new Socket (address, 7777);
- in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
- }
- client.close();
- }
- catch(Exception e)
- {
- System.out.println("Client_ERROR");
- }
- }
- }
- /********************CLIENT BANCA STEFANO********************/
- /*
- >>> Seconda parte (facoltativa)
- Scrivere un programma client che permetta di inviare
- comandi al server descritto.
- */
- import java.io.*;
- import java.net.*;
- public class client {
- public static void main(String[] args) {
- if (args.length != 1) {
- System.out.println("Inserisci uno ed un solo parametro!");
- return;
- }
- try {
- InetAddress address = InetAddress.getByName("localhost");
- Socket client = new Socket(address, 12345);
- BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- PrintWriter out = new PrintWriter(client.getOutputStream(), true);
- out.println(args[0]);
- System.out.println(in.readLine());
- client.close();
- }
- catch (Exception e) { e.printStackTrace(); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement