Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cliente
- import java.net.*;
- import java.util.Scanner;
- import java.io.*;
- public class ClienteTCP {
- public static void main (String args[]) {
- Socket socketCliente = null;
- try{
- //Cria o socket
- socketCliente = new Socket("localhost", 7896);
- //Vincula os streams de enrada e saida
- DataInputStream entrada = new DataInputStream(socketCliente.getInputStream());
- DataOutputStream saida = new DataOutputStream(socketCliente.getOutputStream());
- //Prepara leitura do teclado
- Scanner s = new Scanner(System.in);
- System.out.println("Digite 1 para Solicitar\nDigite 2 para Devolver\n------------->");
- int resposta = s.nextInt();
- if (resposta == 1) {
- //Escreve no stream de saida
- saida.writeInt(1);
- saida.writeInt(1);
- saida.writeInt(1);
- } else {
- //Escreve no stream de saida
- saida.writeInt(2);
- saida.writeInt(1);
- saida.writeInt(1);
- }
- //Recebe no stream de entrada
- String data = entrada.readUTF();
- System.out.println("Recebeu do servidor: "+ data);
- }catch (UnknownHostException e){
- System.out.println("Socket:"+e.getMessage());
- }catch (EOFException e){System.out.println("EOF:"+e.getMessage());
- }catch (IOException e){System.out.println("IO:"+e.getMessage());
- }finally {
- if(socketCliente!=null) try {
- socketCliente.close();
- }catch (IOException e){System.out.println("close:"+e.getMessage());
- }
- }
- }
- }
- //cliente
- import java.net.*;
- import java.io.*;
- public class ServidorTCP {
- public static void main (String args[]) {
- try{
- //Cria o socket do servidor
- ServerSocket socketServidor = new ServerSocket(7896);
- while(true) {
- System.out.println("Aguardando cliente...");
- //Recebe solicita��o de socket do cliente
- Socket socketCliente = socketServidor.accept();
- //Estabelece conex�o instanciando a thread
- Connection c = new Connection(socketCliente);
- }
- } catch(IOException e) {System.out.println("Listen :"+e.getMessage());}
- }
- }
- class Connection extends Thread {
- DataInputStream entrada;
- DataOutputStream saida;
- Socket socketCliente;
- public Connection (Socket aSocketCliente) {
- try {
- socketCliente = aSocketCliente;
- //Vincula streams de entrada e sa�da ao socket
- entrada = new DataInputStream(socketCliente.getInputStream());
- saida = new DataOutputStream(socketCliente.getOutputStream());
- //Inicia thread
- this.start();
- } catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
- }
- public void run(){
- try {
- int dadosEntrada1 = entrada.readInt();
- int dadosEntrada2 = entrada.readInt();
- int dadosEntrada3 = entrada.readInt();
- if (dadosEntrada1 == 1) {
- String hora = "11:04:00";
- saida.writeUTF("Servidor: Solicitacao, " + hora);
- } else {
- saida.writeUTF("Servidor: Devolucao, 11:04:00");
- }
- } catch(EOFException e) {System.out.println("EOF:"+e.getMessage());
- } catch(IOException e) {System.out.println("IO:"+e.getMessage());
- } finally{
- try {
- socketCliente.close();
- }catch (IOException e){/*erro ao fechar*/}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement