Advertisement
gastaojunior

Especializacao JAVA IFMT - Socket cliente

Jul 4th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1.  import java.io.IOException;  
  2.  import java.io.PrintStream;  
  3.  import java.net.Socket;  
  4.    
  5.  public class Cliente  
  6.  {  
  7.    
  8.      public static void main(String[] args)  
  9.      {  
  10.            
  11.          //Declaro o socket cliente  
  12.          Socket s = null;  
  13.            
  14.          //Declaro a Stream de saida de dados  
  15.          PrintStream ps = null;  
  16.            
  17.          try{  
  18.                
  19.              int x = 20;  
  20.              //Cria o socket com o recurso desejado na porta especificada  
  21.              s = new Socket("127.0.0.1",7000);  
  22.                
  23.              //Cria a Stream de saida de dados  
  24.              ps = new PrintStream(s.getOutputStream());  
  25.                
  26.              //Imprime uma linha para a stream de saída de dados  
  27.              ps.println(x);  
  28.                
  29.          //Trata possíveis exceções  
  30.          }  
  31.          catch(IOException e)  
  32.          {  
  33.                
  34.              System.out.println("Algum problema ocorreu ao criar ou enviar dados pelo socket.");  
  35.            
  36.          }  
  37.          finally  
  38.          {  
  39.                
  40.              try{  
  41.                    
  42.                  //Encerra o socket cliente  
  43.                  s.close();  
  44.                    
  45.              }  
  46.              catch(IOException e){}  
  47.            
  48.          }  
  49.    
  50.      }  
  51.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement