Advertisement
lucast0rres

socketstcp 040917

Sep 4th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. // cliente
  2.  
  3. import java.net.*;
  4. import java.util.Scanner;
  5. import java.io.*;
  6.  
  7. public class ClienteTCP {
  8.     public static void main (String args[]) {
  9.         Socket socketCliente = null;
  10.         try{
  11.             //Cria o socket
  12.             socketCliente = new Socket("localhost", 7896);    
  13.             //Vincula os streams de enrada e saida
  14.             DataInputStream entrada = new DataInputStream(socketCliente.getInputStream());
  15.             DataOutputStream saida = new DataOutputStream(socketCliente.getOutputStream());
  16.             //Prepara leitura do teclado
  17.             Scanner s = new Scanner(System.in);
  18.             System.out.println("Digite 1 para Solicitar\nDigite 2 para Devolver\n------------->");
  19.             int resposta = s.nextInt();
  20.            
  21.             if (resposta == 1) {
  22.                 //Escreve no stream de saida
  23.                 saida.writeInt(1);
  24.                 saida.writeInt(1);
  25.                 saida.writeInt(1);
  26.             } else {
  27.                 //Escreve no stream de saida
  28.                 saida.writeInt(2);
  29.                 saida.writeInt(1);
  30.                 saida.writeInt(1);
  31.             }
  32.            
  33.             //Recebe no stream de entrada
  34.             String data = entrada.readUTF();         
  35.             System.out.println("Recebeu do servidor: "+ data);      
  36.         }catch (UnknownHostException e){
  37.                 System.out.println("Socket:"+e.getMessage());
  38.         }catch (EOFException e){System.out.println("EOF:"+e.getMessage());
  39.         }catch (IOException e){System.out.println("IO:"+e.getMessage());
  40.         }finally {
  41.             if(socketCliente!=null) try {
  42.                 socketCliente.close();
  43.                 }catch (IOException e){System.out.println("close:"+e.getMessage());
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. //cliente
  50. import java.net.*;
  51. import java.io.*;
  52.  
  53. public class ServidorTCP {
  54.     public static void main (String args[]) {
  55.  
  56.     try{
  57.         //Cria o socket do servidor
  58.         ServerSocket socketServidor = new ServerSocket(7896);
  59.         while(true) {
  60.             System.out.println("Aguardando cliente...");
  61.             //Recebe solicita��o de socket do cliente
  62.             Socket socketCliente = socketServidor.accept();
  63.             //Estabelece conex�o instanciando a thread
  64.             Connection c = new Connection(socketCliente);
  65.         }
  66.     } catch(IOException e) {System.out.println("Listen :"+e.getMessage());}
  67.     }
  68. }
  69.  
  70. class Connection extends Thread {
  71.     DataInputStream entrada;
  72.     DataOutputStream saida;
  73.     Socket socketCliente;
  74.     public Connection (Socket aSocketCliente) {
  75.         try {
  76.             socketCliente = aSocketCliente;
  77.             //Vincula streams de entrada e sa�da ao socket
  78.             entrada = new DataInputStream(socketCliente.getInputStream());
  79.             saida = new DataOutputStream(socketCliente.getOutputStream());
  80.             //Inicia thread
  81.             this.start();
  82.          } catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
  83.     }
  84.     public void run(){
  85.         try {
  86.             int dadosEntrada1 = entrada.readInt();
  87.             int dadosEntrada2 = entrada.readInt();
  88.             int dadosEntrada3 = entrada.readInt();
  89.            
  90.             if (dadosEntrada1 == 1) {
  91.                 String hora = "11:04:00";
  92.                 saida.writeUTF("Servidor: Solicitacao, " + hora);
  93.             } else {
  94.                 saida.writeUTF("Servidor: Devolucao, 11:04:00");
  95.             }
  96.            
  97.         } catch(EOFException e) {System.out.println("EOF:"+e.getMessage());
  98.         } catch(IOException e) {System.out.println("IO:"+e.getMessage());
  99.         } finally{
  100.             try {
  101.                 socketCliente.close();
  102.             }catch (IOException e){/*erro ao fechar*/}
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement