Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author luis-rei
- */
- public class Cliente {
- public Cliente()
- {
- try
- {
- // Declara um objeto da class InetAddress;
- InetAddress host = null;
- // Devolve o endereço IP do utilizador, usando o método getHostAddress();
- byte[] ip = InetAddress.getLocalHost().getAddress();
- String IPAddress = "";
- /*
- Vai percorrer o array de bytes do Endereço IP,
- e vai imprimir o seu valor, em inteiros,
- pois o java guarda o seu valor em bytes;
- */
- for(int i = 0; i < ip.length; i++)
- {
- if(i > 0)
- IPAddress += ".";
- IPAddress += (ip[i] & 0xff);
- }
- System.out.println("IP: " + IPAddress);
- Socket sc = new Socket("127.0.0.1", 2222);
- System.out.println("[LOG CLIENT] Coneção ao servidor feita com sucesso!");
- ObjectOutputStream oOS = new ObjectOutputStream(sc.getOutputStream());
- System.out.println("[LOG CLIENT] ObjectOutputStream criado com sucesso!");
- ObjectInputStream oIS = new ObjectInputStream(sc.getInputStream());
- System.out.println("[LOG CLIENT] ObjectInputStream criado com sucesso!");
- oOS.writeUTF(IPAddress);
- oOS.flush();
- System.out.println("[LOG CLIENT] IP enviado com sucesso!");
- System.out.print("Deseja parar a thread [Y/N]? ");
- String test = Ler.umaString();
- if(test.equals("Y"))
- {
- oOS.writeUTF(test);
- oOS.flush();
- }
- }
- /*catch(ClassNotFoundException e)
- {
- System.out.println("Class not found!");
- }*/
- catch(IOException e)
- {
- System.out.println("IOException: " + e.getMessage());
- }
- }
- public static void main(String[] args)
- {
- Cliente client = new Cliente();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement