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

The Archive Base Latest Questions

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

How to return action to while loop in a switch statement ? The client

  • 0

How to return action to while loop in a switch statement ? The client is unable to return to while statement. I meant to say, whenever response comes from server, the client must start from while loop again, so that client can select his choices. Is it a problem of System.out ? (Note: It’s a copied code from others) Here are the client and server :

Client:

    .............
while (true) {
       String userchoice = console.readLine("Enter your choice :"); 
       int choice= Integer.parseInt(userchoice);
      switch (choice){  
    ..........
    case 2: // for viewing files in the client's directory
      try{
      Socket mysoc = new Socket("localhost", 9001);
      String user_name = username;
      DataOutputStream ToServer = new DataOutputStream(mysoc.getOutputStream());
      ToServer.writeBytes(user_name + '\n');
      BufferedReader FromServer = new BufferedReader(new InputStreamReader(mysoc.getInputStream()));
      String line = null;
      while ((line = FromServer.readLine()) != null) {
       System.out.println(line);
      } 
     }
    catch(Exception e)
            {
                e.printStackTrace();
                System.exit(0);
            }
      break;
      ............
}

Server:

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

class DirList
{
   public static void main(String argv[]) throws Exception
      {
         String clientString;
         //String replyString;
         ServerSocket welcomeSoc = new ServerSocket(9001);

         while(true)
         {
            Socket connectionSocket = welcomeSoc.accept();
            BufferedReader FromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            PrintWriter ToClient = new PrintWriter(connectionSocket.getOutputStream(),true);
            clientString = FromClient.readLine();
            System.out.println("Received view songs request from : " + clientString);
            String path = "/home/sri/Songs/";
            String text="";
            File f = new File(path);
                File[] listOfFiles = f.listFiles();
                for (int j = 0; j < listOfFiles.length; j++) {
                        if (listOfFiles[j].isFile()) {
                                text = listOfFiles[j].getName();
                                ToClient.println(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-12T21:31:55+00:00Added an answer on June 12, 2026 at 9:31 pm

    try to close your PrintWriter and Socket in the Server code after they finished their task.

    @Sri, about the question you’ve asked in the comment:

    by Socket I meant connectionSocket. ServerSocket will remain there during your server life time. But you will have a new connectionSocket for each user. so each connectionSocket will serve only a single client.

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

Sidebar

Related Questions

i have an action that return a file content. i added: Response.AddHeader(Content-Disposition, attactment; filename:\
Is it possible to skip the whole action method execution and return a specific
When the Action method below attempts to return the Json result no data object
I've got the following action on an ApiController: public string Something() { return value;
I have an action method that depending on some conditions needs to return a
Im creating Triggers and Action Application for my Final Year Project, I return a
In my controller i had given [ValidateInput(false)] for that particular Action in my return
What's wrong with this function: function() { $.get('/controller/action', function(data) { $('#temporaryPhotos').text(data); } ); return
I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
$('form').submit(function(){ this.action=http://www.sitename.com/post; return false; }); having altered the action attribute of the form ,i

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.