Java - Ayuda con codigo de sockets en java que desconozco

 
Vista:

Ayuda con codigo de sockets en java que desconozco

Publicado por Miguel (1 intervención) el 20/06/2017 23:37:57
Buenas. Necesito una ayuda para entender este codigo el cual no lo hice yo. El problema es que necesito crear la parte del cliente y que funcione para este codigo el cual pertenece al servidor.

Alguna propuesta?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
public class HiloPrecioSubir extends Thread {
 
    private Socket cliente    = null;
    private BufferedReader entrada;
    private OutputStream salida;
    private File fDir         = null;
    private String direccionCliente;
    private String nombre     = "";
    private boolean instancia = false;
    private boolean debug     = false;
    private String directorio = null;
    private int largoNombre   =63;
    private String tokenEnc="ENC&%&%&%";
    private String tokenDet="DET&%&%&%";
    private Date fecha;
 
    /** Creates a new instance of HiloPrecioSubir */
 
    public HiloPrecioSubir(Socket socket, String directorio, String extensionArchivos, boolean debug) throws IOException {
 
        this.cliente=socket;
        this.debug=debug;
        this.directorio=directorio;
        this.entrada=new BufferedReader(new InputStreamReader(cliente.getInputStream()));
        this.salida =cliente.getOutputStream();
 
        if (this.directorio.endsWith("\\") || this.directorio.endsWith("/")) {   //Se agrega terminacion unix '/'. Leo
            this.directorio = this.directorio.substring(0, this.directorio.length() - 1);
        }
 
        fDir = new File(this.directorio);
 
        if (!fDir.exists() || !fDir.isDirectory()) {
            System.out.println("El directorio no existe o no es un directorio v�lido");
            //System.exit(-1);
            return;
        }
 
 
        this.start();
    }
 
 
    public void run(){
 
        String cadena="";
        String fechaHora="";
 
        try {
 
            int i=0;
            int c;
            direccionCliente = cliente.getLocalAddress().getHostName();
            direccionCliente = Utils.reemplazar(Utils.reemplazar(direccionCliente, '/', '@'), '.', '_');
            ControladorComandos ctrlCmd=new ControladorComandos();
 
            if (debug)
                System.out.println("\tConectado con cliente("+cliente.getInetAddress()+") desde nuevo " + direccionCliente);
 
             Utils.enviarFecha(salida);
            // Error.lanzarError(salida);
 
            while((c = entrada.read()) != -1) {
 
                if(i >= largoNombre) {
 
                    if(cadena.endsWith(tokenEnc)){
 
                        cadena=cadena.substring(0,cadena.length()-tokenEnc.length());
                        nombre = nombre.trim();
                        ctrlCmd.llamarComando(directorio, cadena.trim(), nombre, tokenEnc);
                        if (debug)
                            System.out.println("\t" + i + " bytes escritos en " + nombre+".txt" + ".");
                        nombre="";
                        cadena="";
                        largoNombre+=i;
 
 
                    }else if(cadena.endsWith(tokenDet)){
 
                        cadena=cadena.substring(0,cadena.length()-tokenEnc.length());
                        nombre = nombre.trim();
                        ctrlCmd.llamarComando(directorio, cadena.trim(), nombre, tokenDet);
                        if (debug)
                            System.out.println("\t" + i + " bytes escritos en " + nombre+".txt" +".");
                        nombre="";
                        cadena="";
                        largoNombre+=i;
 
                    }
 
                    cadena+=(char)c;
                } else {
                    nombre+=(char)c;
                }
                i++;
            }
 
            //Error.lanzarError(salida);
 
        } catch (Exception e) {
 
            System.out.println("Error: com.la14.chq.socket.ListaPrecioSubir: " + e.getMessage());
 
        }finally{
            try{
 
               salida.close();
               entrada.close();
               cliente.close();
 
            }catch(IOException e){
 
                System.out.println("Socket sin cerrar HiloPrecioSubir :"+e.getMessage());
            }
        }
 
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Ayuda con codigo de sockets en java que desconozco

Publicado por Costero (148 intervenciones) el 22/06/2017 15:21:12
Primero tienes que correr el servidor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class AppServer {
    public static void main(String [] args) throws IOException {
        ServerSocket listener = new ServerSocket(9090);
        try {
            while (true) {
                Socket socket = listener.accept();
                new HiloPrecioSubir(socket, "/tmp", "html", true);
            }
        }
        finally {
            listener.close();
        }
 
    }
}

Despues corres el cliente. Algo asi:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ClientePrecioSubir {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("10.3.12.86", 9090);
 
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
 
        // send greeting to server ...
        out.println("Hello Server");
 
        System.out.println("Response from server: " + in.readLine());
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar