Advertisement
Guest User

Socket

a guest
Dec 5th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.32 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.*;
  8.  
  9. public class clientDefault {
  10.     public static void main(String args[]) {
  11.         String tmp;
  12.         Scanner input = new Scanner(System.in);
  13.         try {
  14.             // preparo l'indirizzo
  15.             InetAddress address = InetAddress.getByName("127.0.0.1");
  16.  
  17.             // creo il socket
  18.             Socket client = new Socket(address,Integer.valueOf(args[0]));
  19.  
  20.             // preparo i buffer in ingresso e uscita
  21.             BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  22.             PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
  23.  
  24.             // invio il messaggio
  25.             String text="",
  26.                    text2;
  27.                    
  28.             while(client.isClosed() != true) {
  29.                 System.out.print("Messaggio da inviare al server: ");
  30.                 out.println(input.nextLine());
  31.                 text="";
  32.                 while(((text2=in.readLine())!=null)) {
  33.                 //      System.out.println(text2);
  34.                         text+=text2;
  35.                         if(in.ready()==false) break;
  36.                 }
  37.                 //System.out.println("Esce1");
  38.             System.out.println(text);
  39.             }
  40.             //System.out.println("Esce");
  41.         }
  42.         catch(Exception e) {
  43.             e.printStackTrace();
  44.         }
  45.     }
  46. }
  47.  
  48.  
  49.  
  50.  
  51. import java.io.*;
  52. import java.net.*;
  53.  
  54. public class serverDefault {
  55.     public static void main(String args[])  {
  56.         try {
  57.             //1. inizializzazione serverSocket e Socket (client)
  58.             ServerSocket server = new ServerSocket(7777); //creazione server in ascolto su una porta
  59.             Socket client;
  60.  
  61.             //2. ciclo d'attesa
  62.             while(true) {
  63.                 //3. accept()
  64.                 System.out.println("In attesa di connessione...");
  65.                 client = server.accept(); //server in attesa fin quando il client non instaura una connessione
  66.                 System.out.println("Client connesso: " + client);
  67.  
  68.                 //info
  69.                 /*InetAddress infoclient = client.getInetAddress();
  70.                 String cliente = infoclient.getHostAddress();
  71.                 int portclient = client.getLocalPort();
  72.                 System.out.println("Info client: " + infoclient + "\nclient: " + cliente + "\nporta: " + portclient);
  73.                 //stampa nome host
  74.                 try {
  75.                     InetAddress host = InetAddress.getLocalHost();
  76.  
  77.                     System.out.println("Host: " + host.getHostName());
  78.                 }
  79.                 catch(UnknownHostException e) {
  80.                     System.out.println("Problemi con l'host");
  81.                     e.printStackTrace();
  82.                 }*/
  83.  
  84.                 //4. apertura canale di comunicazione
  85.                 // con client.getInputStream() si permette al server di usare gli stream che il cliente ha stabilito
  86.                 BufferedReader is = new BufferedReader(new InputStreamReader(client.getInputStream())); //leggere le richieste del client
  87.                 DataOutputStream os = new DataOutputStream(client.getOutputStream()); //inviare risposte al client 
  88.  
  89.                 //5. leggere dati scritti
  90.                 while (true) {
  91.                     String clientInput = is.readLine(); //ricezione dati dalla socket
  92.                     if (clientInput.length() == 0 || clientInput.equals("QUIT"))
  93.                         break;
  94.                    
  95.                     os.writeBytes(clientInput + "\n"); //risponde al client
  96.                     System.out.println("Il client ha scritto: " + clientInput); //risponde al server
  97.  
  98.                 }
  99.  
  100.                 //6. close()
  101.                 os.close();
  102.                 is.close();
  103.                 server.close();
  104.                 System.out.println("Connessione chiusa");
  105.             }
  106.         }
  107.         catch (Exception e) {System.out.println(e);}
  108.     }
  109. }
  110.  
  111.  
  112. |
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. //***************InetAddress (indirizzi internet)
  135. //posso sollevae l'eccezioni Unknown-HostException se l'indirizzo specificato non può essere risolto
  136. InetAddress.getByName(<string nome host>) //restituisce una istanza rappresentate l'host speficicato
  137.                                 //passando un parametro null viene restituito l'host locale
  138.  
  139. static InetAddress getByAddress(byte[] addr) //restituice una istanza InetAddress rappresentante l'host specificato come indirizzo IP
  140.  
  141. static InetAddress[] getAllByName(String hostname) //restituisce un array di istanze InetAddress
  142.                                                    //utile nei casi di più indirizzi IP registrato con lo stesso nome logico
  143.  
  144.  
  145. InetAddress.getLocalHost() //restituisce una istanza di INetAddress per la macchina locale                                                 
  146.  
  147. <InetAddress>.getHostName() //restituisce il nome dell'host con l'indirizzo <InetAddress>
  148.  
  149. //****************METODI CLASSI SOCKET
  150.     getInetAddress(); //restituisce l'indirizzo remoto a cui la socket è connessa
  151.     getlLocalAddress(); //restituisce l'indirizzo locale
  152.     getPort(); //restituisce la porta remota
  153.     getLocalPort(); //restituisce la porta locale
  154.  
  155.  
  156. //****************STRINGHE
  157.     //da char a stringa
  158.         String s = Character.toString('A');
  159.     //da stringa a intero
  160.         int n = Interger.parseInt("50");
  161.     //da intero a stringa
  162.         String s = String.valueOf(25);
  163.         String s = "" + 25;
  164.  
  165.  
  166.     //comparazione stringhe
  167.         s.equals(s1); //true se sono uguali, false altrimenti
  168.         s.equals(""); //stringa vuota
  169.  
  170.     //lunghezza stringa
  171.         s.length(); //restituisce la lunghezza, 0 se la stringa è nulla
  172.  
  173.     //posizioni stringa
  174.         s.charAt(0); //restituisce il carattere posizione 0
  175.  
  176.     //concatenazione
  177.         String s = "ciao"
  178.         s += " a"; //s diventa "ciao a"
  179.  
  180.         s = s.concat(" tutti"); //s diventa "ciao a tutti"
  181.  
  182.     //sottostringhe
  183.         s.substring(2); //restituisce la sottostringa dalla posizione 2 all'ultima
  184.         s.substring(2,5); //restituisce la sottostringa dalla posizione 2 alla 4 (5 escluso)
  185.  
  186.     //cercare un carattere o una sottostringa in una stringa
  187.         s.indexOf('v'); //restituisce un intero che rappresenta la prima posizione in cui viene trovato il carattere
  188.                         //-1 se non esiste
  189.         s.indexOf("tele"); //restituisce un intero che rappresenta la prima posizione in cui viene trovata la stringa
  190.                            //-1 se non esiste
  191.  
  192.         s.lastIndexOf('v'); //restituisce un intero che rappresenta l'ultima posizione in cui viene trovato il carattere
  193.                             //-1 se non esiste
  194.         s.lastIndexOf("tele"); //restituisce un intero che rappresenta l'ultima posizione in cui viene trovata la stringa
  195.                                //-1 se non esiste
  196.  
  197.     //ordinare più stringhe lessicograficamente
  198.         s1.compareTo(s2); //restituisce un valore < 0 se s1 viene prima di s2
  199.                           //            un valore > 0 se s1 viene dopo di s2
  200.                           //            0 se sono uguali
  201.  
  202.  
  203. //******************getClass
  204.     String a = "";
  205.     a.getClass();
  206.  
  207.  
  208. //******************input
  209.     int input = System.in.readLine();
  210.  
  211.     BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
  212.     String input = is.readLine();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement