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 8657267
In Process

The Archive Base Latest Questions

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

I would like to take text from a file and then send it to

  • 0

I would like to take text from a file and then send it to the server for it to capitalise before sending back to the client where it is printed out. How do I achieve this?

I can read one line and send that back to the client and I’ve managed to write multiple lines to the output stream (for the server to read) but I don’t know what to do now..

I have a client that reads text from a file:

import java.io.*;
import java.net.*;
import java.util.Date;

public class Client 
{
    public static void main(String[] args)
    {
        try
        {
            // First create the input from keyboard
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Client Program");

            // Next we need to find out the IP address and port number of the server
            System.out.print("Enter IP Address of server: ");
            String ip = input.readLine();
            System.out.print("Enter port number of server: ");
            String port_string = input.readLine();

            // The port number needs to be an int, so convert the string to an int
            int port = Integer.parseInt(port_string);

            // Connect to the server
            Socket sock = new Socket(ip, port);


            // Create the incoming stream to read messages from
            DataInputStream network = new DataInputStream(sock.getInputStream());

            //Create the output stream to the client
            DataOutputStream message = new DataOutputStream(sock.getOutputStream());

            //Send message
            //message.writeUTF("some text");
            FileReader file = new FileReader("text.dat");
            BufferedReader input_file = new BufferedReader(file);

            // Loop until EOF
            while (input_file.ready()){
            // Read next line from the file
                String line = input_file.readLine();
                // Write line to server
                message.writeUTF(line + "\n");
                //System.out.println(line);
            }

            // Display our address
            System.out.println("Address: " + sock.getInetAddress());
            String line;

            // Loop until the connection closes, reading from the network
            while ((line = network.readUTF()) != null)
            {
                // Display the received message
                System.out.println(line);
            }
        }
        catch (IOException ioe)
        {
            // This is expected when the server closes the network connection
            System.err.println("Error in I/O");
            System.err.println(ioe.getMessage());
            System.exit(-1);
        }
    }
}

And then a server that is supposed to take those strings and capitalise them:

import java.io.*;
import java.net.*;
import java.util.*;

public class Server 
{
    public static void main(String[] args)
    {
        try
        {
            // First create the input from the keyboard
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Server Program");

            // Get the port to listen on
            System.out.print("Enter port number to listen on: ");
            String port_string = input.readLine();

            // The port number needs to be an int, so convert the String to an int
            int port = Integer.parseInt(port_string);

            // Create a ServerSocket to listen on this address
            ServerSocket server = new ServerSocket(port);

            // Accept an incoming client connection on the server socket
            Socket sock = server.accept();

            // Create the output stream to the client
            DataOutputStream network = new DataOutputStream(sock.getOutputStream());

            //Create the incoming stream to read messages from
            DataInputStream message =  new DataInputStream(sock.getInputStream());



            String newLine = inFromClient.readLine();

            //Line to read
            String line;
            line = message.readUTF();
            line = line.toUpperCase();




            // Send message
            network.writeUTF(newLine);

            // Close sockets.  This will cause the client to exit
            sock.close();
            server.close();
        }
        catch (IOException ioe)
        {
            System.err.println("Error in I/O");
            System.err.println(ioe.getMessage());
            System.exit(-1);
        }
    }
}
  • 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-12T15:28:55+00:00Added an answer on June 12, 2026 at 3:28 pm

    The problem is your server is reading the stream only once and closing the socket connection.
    How will your server know you have finished sending the client data to the server socket ?
    You should modify the server to listen to the port till you have finished sending the whole text. For that do something like –

    String newLine;
    while ( true )
    newLine = inFromClient.readLine();              
              if (newLine.equalsIgnoreCase("END"))
    {
        break;
    }
    
              newLine = newLine.toUpperCase();                 
    
    // Send message             
              network.writeUTF(newLine);
    }
    // Close sockets.  This will cause the client to exit
    
    sock.close();               
    server.close();
    

    And from the client send “END” after all lines have been sent.

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

Sidebar

Related Questions

I would like to take data (some text) from a file and insert it
I would like to take input from a text file in Perl. Though lot
I would like to take a list that I have in a text file,
I have a backup log file from robocopy and would like to take last
I would like to take the memory generated from my struct and push it
I would like to write to a text file, but I have small problem.
I would like to take the Roman Numeral value stored in the object and
I would like to take a string representation of a set and parse it.
I would like to take a field and replace all characters that are not
I would like to take a specific part of the last line of a

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.