Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:53:41+00:00 2026-06-18T15:53:41+00:00

I started learning Java a few days ago at university (Subject is Server programming),

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:53:43+00:00Added an answer on June 18, 2026 at 3:53 pm

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

    Use the socket returned by the accept() call!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started learning Java (about a week ago) and the last few days
I am really new to java (started learning 2 days ago). Sorry if this
All, I started learning Java threads in the past few days and have only
So I started learning java several days ago and got a question. For the
I started learning Java. When would I use a HashMap over a TreeMap?
Just started learning Java, wrote a basic program that calculates the area of user
I recently started learning Java and found it very strange that every Java public
I've only started learning Java about 3 months ago and this is my first
Started learning java a week ago and also decided learning the right way to
I have started learning Java and am having problem with a simple program that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.