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

  • Home
  • SEARCH
  • 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 8157337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:14:58+00:00 2026-06-06T17:14:58+00:00

I wrote two small java programs: a TCP client which sends many lines of

  • 0

I wrote two small java programs: a TCP client which sends many lines of data from a txt file, and a TCP Server which accepts connection and receives line by line.

It works, but Server receives all the lines together, when transmission is over and client closes the socket. I don’t understand why, because I’m using flush after each line sent into the Client, and I always thought that was to avoid this kind of situation.

If my test.txt file contents the numbers 1 2 3 … 10, each one in a new line, Server’s output is: “12345678910” and then in a new line it writes in console “null”.

Server code:

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

class ServidorTCP {

  private String HOST;
  static final int PUERTO = 20001;

      public ServidorTCP( ) {
        try{
          ServerSocket skServidor = new ServerSocket(PUERTO);
              Socket skCliente = skServidor.accept();
          DataInputStream inFromClient = new DataInputStream(new BufferedInputStream(skCliente.getInputStream()));

          while(true){
              String lineaLeida = inFromClient.readUTF();
              if(lineaLeida.equals("END")) break;
              System.out.println(lineaLeida);
          }
              inFromClient.close();
              skCliente.close();
          skServidor.close();
          System.out.println( "Transmission ended" );
        } catch( Exception e ) {
          System.out.println( e.getMessage() );
        }
      }


      public static void main( String[] arg ) {
        new ServidorTCP();
      }

    }

Cliente code:

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

class ClienteTCP {

  static final String HOST = "192.168.1.201";
  static final int PUERTO = 20001;

  public ClienteTCP( ) {
    try{
      Socket skCliente = new Socket(HOST, PUERTO);
      if(skCliente.isConnected()) System.out.println("Connected!");
      DataOutputStream outToServer = new DataOutputStream(skCliente.getOutputStream());

      File testFile = new File("test.txt");
      BufferedReader input = new BufferedReader(new FileReader(testFile));
      String line;
      while((line=input.readLine())!=null) {
        if(!line.endsWith("\n")) line = line + "\n";
        System.out.println("Sending: " + line);
        outToServer.writeBytes(line);
        outToServer.flush();
        Thread.currentThread().sleep(1000);
      }
      outToServer.writeBytes("END");
      input.close();
      outToServer.close();
      skCliente.close();
    } catch( Exception e ) {
      System.out.println( e.getMessage() );
    }
  }


  public static void main( String[] arg ) {
    new ClienteTCP();
  }

}

Where is the problem?

EDIT: I have edited the code as suggested. Now doesn’t receive anyhing

  • 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-06T17:15:00+00:00Added an answer on June 6, 2026 at 5:15 pm

    The fact of the matter is that your code doesn’t work at all, let alone before or after the socket is closed. You are writing with writeUTF() and reading with readLine(). This does not work. You need to:

    1. write with DataOutputStream.writeUTF() and read with DataInputStream.readUTF()

    2. write with println() (supplied by several I/O classes), or write() or print() (supplied by several I/O classes), followed by BufferedWriter.newline(), or

    3. write with ObjectOutputStream.writeObject() and read with ObjectInputStream.readObject().

    Etc. Writing with one API and reading with a non-symmetrical API doesn’t work.

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

Sidebar

Related Questions

I wrote two small applications (a client and a server) to test UDP communication
I wrote two simple programs server and a client using sockets in C++ (Linux).
I wrote two small C++ applications, one of which is an ActiveX container embedding
I wrote two queries to find duplicates in the array var groups = from
I have been working on a small Plagiarism detection engine which uses Idea from
I have a developed two small Java applications - a vanilla Java app and
I wrote a small geoip lookup linux daemon in C++, and I'm getting two
i have been working on a small demo and i wrote a function which
I have been working for two weeks on JamVM, a small but powerful Java
I wrote a small app that allows me to compare two database schemas to

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.