Click here to Skip to main content
15,881,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A server program which accepts requests from clients to capitalize strings or finding the factorial of any number (You have to provide both functionalities), When a client connects, a new thread is started to handle it. The Server will process the “1” for capitalizing string and “2” for finding factorial. Receiving client data, capitalizing it or finding the factorial, and sending the response back is all done on the thread, allowing much greater throughput because more clients can be handled concurrently. Note: Use Thread Pool, Scanner and PrintWriter

This is the task to do as mentioned above. I am trying my best but can't achieve the goal. The problem is that the server will handle clients through thread pool and multiple clients can send a flag signal to the server and the server will act accordingly. Will send client the factorial of a number or Capitalized String according to the choice.

What I have tried:

try {  
                Socket clientSocket = new Socket("localhost",port);  
                System.out.println("Client Started");  
                InputStreamReader isr = new InputStreamReader(System.in);  
                BufferedReader br = new BufferedReader(isr);  
                PrintStream toServer = new PrintStream(clientSocket.getOutputStream());  
                BufferedReader fromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));  
                String menu = "1. Factorial\n2. Capitalize String\n3. Exit\nEnter your choice: ";  
                int choice = 0;
                do  
                {  
                     toServer.flush();  
                     System.out.println(menu);  
                     choice=Integer.parseInt(br.readLine());  
                     toServer.println(choice);  
                     toServer.flush();  
                     switch(choice)  
                     {  
                     case 1:  
                          System.out.println("Enter a number: ");  
                          int no=Integer.parseInt(br.readLine());  
                          toServer.println(no);  
                          int result=Integer.parseInt(fromServer.readLine());  
                          System.out.println("Factorial of "+ no + ": " +result);  
                          break;
                     case 2:  
                          System.out.println("Enter a string: ");  
                          String word = br.readLine();  
                          toServer.println(word);  
                          String result1 = fromServer.readLine();  
                          System.out.println(word +" in capital: " + result1);  
                          break;
                     case 3:  
                          break;  
                     default:  
                          System.out.println("Invalid Choice!");  
                     }  
                }while(choice!=3);  
           } catch (Exception e) {  
           }  
      }
Posted
Updated 23-Dec-20 20:18pm
v2
Comments
Sandeep Mewara 24-Dec-20 1:31am    
And your query is?
[no name] 24-Dec-20 1:35am    
I am trying from the last 3 days to write a program. I get some help from github and many more sites but unfortunately it's not going to work. The program mentioned in description.
OriginalGriff 24-Dec-20 1:33am    
Yes, I can.

But what is stopping you from doing it?
You've given us some code, but you haven't told us anything about it.

What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
[no name] 24-Dec-20 1:41am    
I am trying my best from the past few days. But I am not getting the solution. I got many errors which are variable not defined, can't use static/private classes, IOException. That's why I come to the conclusion to get some help.

1 solution

Quote:
I am trying my best from the past few days. But I am not getting the solution. I got many errors which are variable not defined, can't use static/private classes, IOException. That's why I come to the conclusion to get some help.


So start by fixing the errors.
Look at each error message in turn: it will tell you the file name, the line number, (probably) the column on which iut found the problem, and an error message to describe what is wrong.
Most editors support CTRL+G to go directly to a line number, and if you are using an IDE, then almost certainly double clicking the error message will take you straight to the line itself.
Then look at the message - it tells you roughly what the problem is.
"Undefined variable" - that's pretty obvious: a variable with that name does not exist, or does does not exist in the current scope. Check you typed the name the same as you did when you declared it - indeed, check you declared it at all!
Check that the declaration is "in scope" - if x is defined inside function foo then it is only available within that function: you can't use it in function bar unless you pass it to it via a parameter.
A spurious close curly bracket can cause this as well, because it "ends the scope" and stops variables declared in the code it closes being available any more.

When you think you have fixed an error, compile again - many errors can be caused by previous problems, so getting rid of one problem can remove several errors. And always start from the first one - the others may be caused by the same problem!

We can't see your screen, we can't see your error messages - and we can't compile your code to see what they might be!
So stop panicking, and think about each error in isolation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900