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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:49:24+00:00 2026-06-11T11:49:24+00:00

How can I send the file listing to client from server using Socket programming.

  • 0

How can I send the file listing to client from server using Socket programming. I have used DataOutputStream and PrintWriter, both returns only one file name to Client. I know there is some problem in ‘\n’. But unable to solve it. Awaiting experts advice … Thank you.

Client

switch (choice) {

.......

  case 2: // for viewing files in the client's directory
  Socket mysocket = new Socket("localhost", 6103);
  String user_name = username;
  DataOutputStream outToServer2= new DataOutputStream(mysocket.getOutputStream());
  outToServer2.writeBytes(user_name + '\n');
  BufferedReader inFromServer2 = new BufferedReader(newInputStreamReader(mysocket.getInputStream()));
  String list = inFromServer2.readLine();
  System.out.println("FROM SERVER - LIST OF FILES:" + list);

  break;

}

.......

Server

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

class DirList
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6103);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            PrintWriter outToClient = new PrintWriter(connectionSocket.getOutputStream(),true);
            clientSentence = inFromClient.readLine();
            System.out.println("Received view files request from user: " + clientSentence);
            String path = "/home/user/Files/";
            String userdir = path + clientSentence;
            String text="";
            String capitalizedSentence1;
            File f = new File(userdir);
                File[] listOfFiles = f.listFiles();
                for (int j = 0; j < listOfFiles.length; j++) {
                        if (listOfFiles[j].isFile()) {
                                text = listOfFiles[j].getName();
                                outToClient.println(text);
                                System.out.print(text+' ');

                   }
                }


         }
      }
}
  • 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-11T11:49:25+00:00Added an answer on June 11, 2026 at 11:49 am

    You need to flush the output from your server:

    outToClient.flush();
    

    Also, in your client, you need to place the read in a loop to consume all the output:

    String line = null;
    while ((line = inFromServer2.readLine()) != null) {
       System.out.println(line);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can sendfile() linux syscall be used to send part of file from one mmaped
how can i send an xml file on my system to an http server
I'm using ssh2_scp_send to send one file from server1 to server2. It works almost
Im trying to send a file from my SD using Bluetooth . I'm using
I need to send a file to an Android application from my server. I
I have to send file byte-by-byte to serially connected AT89s52 from computer (VB.NET). Every
I want to create this parameter file so that I can send it to
I want to send a xml file to my wcf service , how can
I can send requests to friends using Multi Friend Request Selector inside my Page
In Javascript I can send XML string to JSP server (XmlAction.jsp): Javascript Code: var

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.