Advertisement
Gilgamesh858

socket.java

Dec 6th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.90 KB | None | 0 0
  1. /********************SKELETON********************/
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. public class skeleton {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         try {
  10.             InetAddress address = InetAddress.getByName("www.dmi.unict.it");
  11.  
  12.             Socket client = new Socket(address, 80);
  13.  
  14.             BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  15.             PrintWriter out = new PrintWriter(client.getOutputStream(), true);
  16.  
  17.            
  18.         }
  19.         catch (Exception e) { e.printStackTrace(); }
  20.  
  21.     }
  22. }
  23.  
  24. /********************SERVER BANCA********************/
  25. /*
  26. Implementare in C o Java, su localhost, un programma server banca, connection-oriented, che:
  27.  
  28.     mantenga un array di 10 interi conto che rappresentano il saldo dei conti da 0 a 9;
  29.     risponda, su localhost, port 7777 ai seguenti messaggi:
  30.         [Un] dove n, da 0 a 9, è il numero del conto;
  31.         l'effetto sarà di rendere il conto n quello attuale, cioè su cui operano implicitamente gli altri comandi;
  32.         [Vxyzw] dove xyzw sono 4 cifre intere;
  33.         l'effetto sarà di versare la somma xyzw sul conto attuale (cioè l'ultimo selezionato con il comando [Un])
  34.         [Pxyzw] dove xyzw sono 4 cifre intere;
  35.         l'effetto sarà di prelevare la somma xyzw dal conto attuale (cioè l'ultimo selezionato con il comando [Un])
  36.         [S] l'effetto sarà di inviare al cliente il saldo depositato sul conto attuale (cioè l'ultimo selezionato con il comando [Un])
  37.     risponda ERROR a ogni altro dato ricevuto dal cliente
  38. */
  39. package bank;
  40.  
  41. /**
  42.  *
  43.  * @author Ludovico
  44.  */
  45.  
  46. import java.io.*;
  47. import java.net.*;
  48.  
  49.  
  50. public class server {
  51.    
  52.     public static int conto[];
  53.     private static int user;
  54.     private static ServerSocket server;
  55.     private static Socket client;
  56.     private static BufferedReader in;
  57.     private static PrintWriter out;
  58.    
  59.     public static boolean isInt(String str)
  60.     {
  61.         try
  62.         {
  63.             Integer.parseInt(str);
  64.             return true;
  65.         }
  66.         catch(Exception e)
  67.         {
  68.             out.println("ERROR");
  69.             return false;
  70.         }
  71.     }
  72.    
  73.     public static void service(String input_string)
  74.     {
  75.         try
  76.         {
  77.             int leng = input_string.length();
  78.             int x;
  79.            
  80.             switch(input_string.substring(0,1))
  81.             {
  82.                 case "U":
  83.                         if(isInt(input_string.substring(1))){
  84.                             user = Integer.parseInt(input_string.substring(1));
  85.                             out.println("Conto corrente selezionato: "+ user);
  86.                         }
  87.                     break;
  88.                 case "V":
  89.                         if(isInt(input_string.substring(1))){
  90.                             x = Integer.parseInt(input_string.substring(1));
  91.                             conto[user] += x;
  92.                             out.println("Varsamento di "+x+" sul conto corrente "+user);
  93.                         }
  94.                     break;
  95.                 case "P":
  96.                         if(isInt(input_string.substring(1))){
  97.                             x = Integer.parseInt(input_string.substring(1));
  98.                             conto[user] -= x;
  99.                             out.println("Prelievo di "+x+" sul conto corrente "+user);
  100.                         }
  101.                     break;
  102.                 case "S":
  103.                         out.println("Saldo attuale di "+conto[user]+" del conto corrente "+user);
  104.                     break;
  105.                 default:
  106.                     out.println("Server_ERROR");
  107.                     break;
  108.                        
  109.             }
  110.         }
  111.         catch(Exception e)
  112.         {
  113.             out.println("ERROR");
  114.         }
  115.     }
  116.    
  117.     public static void main(String[] args)
  118.     {
  119.         try
  120.         {
  121.             conto = new int[10];
  122.             user = 0;
  123.             server = new ServerSocket(7777);
  124.             System.out.println("Server Pronto");
  125.             while(true)
  126.             {
  127.                 client = server.accept();
  128.                 in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  129.                 out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
  130.                 service(in.readLine());
  131.             }
  132.         }
  133.         catch(Exception e)
  134.         {
  135.             out.println("ERROR");
  136.         }
  137.     }
  138.    
  139. }
  140.  
  141. /********************CLIENT BANCA********************/
  142. package bank;
  143.  
  144.  
  145. import java.net.*;
  146. import java.io.*;
  147.  
  148. /**
  149.  *
  150.  * @author Ludovico
  151.  */
  152.  
  153. public class Client_bank {
  154.    
  155.     public static void main(String[] args)
  156.     {
  157.         try
  158.         {
  159.             InetAddress address = InetAddress.getByName("localhost");
  160.             Socket client = new Socket(address, 7777);
  161.             BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  162.             BufferedReader in_console = new BufferedReader(new InputStreamReader(System.in));
  163.             PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
  164.            
  165.             System.out.println("Inserisci una funzione tra U - V - P - S");
  166.             String str = "";
  167.            
  168.             while(true)
  169.             {
  170.                 if(str.equals("close"))
  171.                     break;
  172.                 System.out.println("Wait...");
  173.                 str = in_console.readLine();
  174.                 out.println(str);
  175.                 System.out.println(in.readLine());    
  176.                 client.close();
  177.                 in.close();
  178.                 out.close();
  179.                 client = new Socket (address, 7777);
  180.                 in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  181.                 out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
  182.                
  183.             }
  184.            
  185.            
  186.             client.close();
  187.         }
  188.         catch(Exception e)
  189.         {
  190.             System.out.println("Client_ERROR");
  191.         }
  192.     }
  193.    
  194. }
  195.  
  196.  
  197. /********************CLIENT BANCA STEFANO********************/
  198. /*
  199. >>> Seconda parte (facoltativa)
  200. Scrivere un programma client che permetta di inviare
  201. comandi al server descritto.
  202. */
  203.  
  204. import java.io.*;
  205. import java.net.*;
  206.  
  207. public class client {
  208.  
  209.     public static void main(String[] args) {
  210.  
  211.         if (args.length != 1) {
  212.             System.out.println("Inserisci uno ed un solo parametro!");
  213.             return;
  214.         }
  215.  
  216.         try {
  217.             InetAddress address = InetAddress.getByName("localhost");
  218.  
  219.             Socket client = new Socket(address, 12345);
  220.  
  221.             BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  222.             PrintWriter out = new PrintWriter(client.getOutputStream(), true);
  223.  
  224.             out.println(args[0]);
  225.             System.out.println(in.readLine());
  226.  
  227.             client.close();
  228.         }
  229.         catch (Exception e) { e.printStackTrace(); }
  230.        
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement