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

时间:2013-02-08 18:28:23

标签: java sockets client serversocket

我几天前在大学开始学习Java(主题是“服务器编程”),我们开始编写两个代码作为客户端和服务器,因此客户端发送了一个包含3个数值的字符串给服务器,它必须应用数学公式(我不记得什么公式,但这无关紧要)然后将结果返回给客户端,客户端必须在屏幕上打印它或其他什么。

这是我的代码。我使用了老师教授的一些课程,如Socket和ServerSocket,但我知道代码中有一些错误,我找不到原因。它在客户端与服务器联系并且服务器为其打开套接字后停止工作。

客户代码:

    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());
    }

服务器的代码:

    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());
    }

(两个代码都在主函数内) 另外,我知道服务器代码中的System.out.println(...)仅显示在服务器中,而客户端则相同。我这样做,所以我可以看到应用程序的进展:)

我希望你能帮助我。我知道有一个错误(好吧......超过1哈哈)但我不确定在哪里,有2个单独的项目/应用意味着2个代码共享线路,我必须混合一些功能或其他什么,以及一个星期左右,老师将无法帮助我。我可以等一下,但我真的希望这只是为了让你觉得有成就感,就像你可能理解的那样。

我不是要求一个有效的代码,而是一些方向,所以我可以自己修复代码,就像我在课堂上一样。

再见!

1 个答案:

答案 0 :(得分:2)

为什么要为server->客户端通信创建新的套接字?

使用accept()调用返回的套接字!