Advertisement
Guest User

Cliente

a guest
Apr 4th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.net.InetAddress;
  6. import java.net.Socket;
  7.  
  8. /*
  9.  * To change this license header, choose License Headers in Project Properties.
  10.  * To change this template file, choose Tools | Templates
  11.  * and open the template in the editor.
  12.  */
  13.  
  14. /**
  15.  *
  16.  * @author luis-rei
  17.  */
  18. public class Cliente {
  19.    
  20.     public Cliente()
  21.     {      
  22.         try
  23.         {
  24.             // Declara um objeto da class InetAddress;
  25.             InetAddress host = null;
  26.  
  27.             // Devolve o endereço IP do utilizador, usando o método getHostAddress();
  28.             byte[] ip = InetAddress.getLocalHost().getAddress();
  29.            
  30.             String IPAddress = "";
  31.            
  32.             /*
  33.                 Vai percorrer o array de bytes do Endereço IP,
  34.                     e vai imprimir o seu valor, em inteiros,
  35.                     pois o java guarda o seu valor em bytes;
  36.             */
  37.  
  38.             for(int i = 0; i < ip.length; i++)
  39.             {
  40.                 if(i > 0)
  41.                     IPAddress += ".";
  42.  
  43.                 IPAddress += (ip[i] & 0xff);
  44.             }
  45.  
  46.             System.out.println("IP: " + IPAddress);
  47.            
  48.            
  49.             Socket sc = new Socket("127.0.0.1", 2222);
  50.            
  51.             System.out.println("[LOG CLIENT] Coneção ao servidor feita com sucesso!");
  52.            
  53.             ObjectOutputStream oOS = new ObjectOutputStream(sc.getOutputStream());
  54.             System.out.println("[LOG CLIENT] ObjectOutputStream criado com sucesso!");
  55.            
  56.             ObjectInputStream oIS = new ObjectInputStream(sc.getInputStream());
  57.             System.out.println("[LOG CLIENT] ObjectInputStream criado com sucesso!");
  58.            
  59.             oOS.writeUTF(IPAddress);
  60.             oOS.flush();
  61.             System.out.println("[LOG CLIENT] IP enviado com sucesso!");
  62.            
  63.             System.out.print("Deseja parar a thread [Y/N]? ");
  64.             String test = Ler.umaString();
  65.            
  66.             if(test.equals("Y"))
  67.             {
  68.                 oOS.writeUTF(test);
  69.                 oOS.flush();
  70.             }
  71.         }
  72.         /*catch(ClassNotFoundException e)
  73.         {
  74.             System.out.println("Class not found!");
  75.         }*/
  76.         catch(IOException e)
  77.         {
  78.             System.out.println("IOException: " + e.getMessage());
  79.         }
  80.     }
  81.  
  82.     public static void main(String[] args)
  83.     {
  84.         Cliente client = new Cliente();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement