简体   繁体   中英

Java Client-server communication issues (3rd day with Java)

I started learning Java a few days ago at university (Subject is "Server programming"), and we started writing two codes that would act as a client and a server, so the client sent a string with 3 numeric values to the server, which would have to apply a math formula (I don't remember what formula, but that doesn't matter) and then return the result to the client, which had to print it in the screen or whatever.

Here are my codes. I used some classes taught by the teacher such as Socket and ServerSocket, but I know there's some mistake in the code and I can't find why. It stops working after the client contacts the server and the server opens a socket for it.

Client code:

    int capital = 300000;
    int interes = 4;
    int plazos = 360;
    String IP_servidor = "127.0.0.1";
    int puerto_servidor = 8801;

    try{

        // Connect with server
        Socket servidor = new Socket (IP_servidor,puerto_servidor); // Se crea un socket para el servidor, con su IP para mandarle luego datos       
        //Server connected

        //Preparing data
        String datos = capital + ";" + interes + ";" + plazos;
        //Data prepared


        //Sending data
        PrintWriter conexion = new PrintWriter(servidor.getOutputStream(),true);   //Object "conexion" will be the one to which we'll write data the server will receive
        conexion.println(datos);
        //Data sent

        // Server processes data and returns it

        //Receiving data
        Socket recepcion = new Socket (IP_servidor,puerto_servidor); 
        BufferedReader lector = new BufferedReader (new InputStreamReader (recepcion.getInputStream()));
        String operacion = new String("");
        operacion = lector.readLine();
        //Data received back

        // End

    }

     catch (IOException Err){
        System.out.println("Error");
        System.out.println(Err.getMessage());
    }

And the server's code:

    int puerto_servidor = 8801;

    try{

        // Server starts and connects with client
        ServerSocket miServidor = new ServerSocket(puerto_servidor);
        System.out.println("Socket created");
        Socket cliente = miServidor.accept();
        System.out.println("Petition from " + cliente.getInetAddress() + " received...");
        // Server started and client connected


        //Receiving data
        Socket cliente2 = new Socket (cliente.getInetAddress(),puerto_servidor); // Creating socket for client, so we can send data there after this
        BufferedReader lector = new BufferedReader (new InputStreamReader (cliente2.getInputStream()));
        String operacion = new String("");
        operacion = lector.readLine();        //Copy from the client to a string to decode afterwards
        System.out.println("Received string \"" + operacion + ".");
        //Data received


        //Preparing data
        String[] datos;
        datos = operacion.split(";"); //En 'datos' (0,1,2) se guardan separados los valores que se recibieron
        capital = Integer.parseInt(datos[0]);
        interes = Integer.parseInt(datos[1]);
        plazos = Integer.parseInt(datos[2]);
        System.out.println("String was cut to 3 pieces: \"" + capital + "\", \"" + interes + "\" y \"" + plazos + "\".");
        int resultado = capital*interes*plazos;
        //Data prepared


        // Se devuelve el resultado al cliente
        PrintWriter way_back = new PrintWriter(cliente.getOutputStream(),true);   
        way_back.println("Result is: " + resultado);
        System.out.println("Result was returned");
        // Se ha devuelto el resultado

        // End

    }

     catch (IOException Err){
        System.out.println("Error");
        System.out.println(Err.getMessage());
    }

(Both codes are inside main function) Also, I know that System.out.println(...) in the server code only shows in the server, and the same for the client. I just do that so I can see the progress of the app :)

I hope you can help me. I know there's an error (well... more than 1 haha) but I'm not sure of where, having 2 separate projects/applications means 2 codes which share lines and well, I must have mixed up some functions or whatever, and the teacher will not be able to help me for a week or so. I can wait, but I really want this to work just to feel accomplished, as you may understand.

I'm not asking for a working code, but a bit of directions so I can fix the code all by myself, just as I would do in class.

Bye!

Why are you creating a new socket for server->client comms?

Use the socket returned by the accept() call!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM