简体   繁体   English

Java客户端 - 服务器通信问题(Java第3天)

[英]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. 我几天前在大学开始学习Java(主题是“服务器编程”),我们开始编写两个代码作为客户端和服务器,因此客户端向服务器发送了一个带有3个数值的字符串,将不得不应用数学公式(我不记得什么公式,但这没关系)然后将结果返回给客户端,客户端必须在屏幕上打印它或其他什么。

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. 我使用了老师教授的一些课程,如Socket和ServerSocket,但我知道代码中有一些错误,我找不到原因。 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. (两个代码都在main函数内)另外,我知道服务器代码中的System.out.println(...)只显示在服务器中,而客户端则相同。 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. 我知道有一个错误(好吧......超过1哈哈)但我不确定在哪里,有2个单独的项目/应用意味着2个代码共享线路,我必须混合一些功能或其他什么,以及一个星期左右,老师将无法帮助我。 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? 为什么要为server-> client comms创建一个新套接字?

Use the socket returned by the accept() call! 使用accept()调用返回的套接字!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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