Question

i am trying to wrire a word and change it to capital letters using client-server. im...

i am trying to wrire a word and change it to capital letters using client-server. im not able to write any words when i run the file in netbeans or command promot. is it something with my code?

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package assignment3retake;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

/**

*

*/

public class Capitalizer implements Runnable{

   

    private Socket socket;

   

     Capitalizer(Socket socket) {

        this.socket = socket;

    }

   

    @Override

    public void run() {

        System.out.println("Connected: " + socket);

        try {

            // TODO Create socket reader

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // TODO Create socket writer

            PrintWriter out = new PrintWriter(socket.getOutputStream());

            // Read from the socket and using a loop, change the case to upper and write to the socket

            String sentence = in.readLine();

            while(sentence != null){              

                    sentence.toUpperCase();

                    System.out.println(sentence + in.readLine());

            }

            in.close();

            out.close();

        } catch (Exception e) {

            System.out.println("Error:" + socket);

        } finally {

            try {

                socket.close();

               

            } catch (IOException e) {

            }

            System.out.println("Closed: " + socket);

        }

    }

   

}

package assignment3retake;

import java.net.*;

import java.io.*;

import java.io.IOException;

import java.net.Socket;

/**

*

*/

public class Client {

        public static void main(String[] args){

            try{

                //connect to server

                Socket sock = new Socket("127.0.0.1",59898);

               

                PrintStream sout = new PrintStream(sock.getOutputStream());

               // InputStream in = sock.getInputStream();

                BufferedReader bin = new BufferedReader(new InputStreamReader(sock.getInputStream()));

                BufferedReader uin = new BufferedReader(new InputStreamReader(System.in));

                String sentence;

               

                 while(true){

                   System.out.flush();

                   sentence = uin.readLine();

                  if(sentence == null)break;

                   sout.println(sentence);

                   sentence = bin.readLine();

                      

                   if(sentence == null){

                       //close the socket

                       sock.close();

                       System.out.println("Connection closed");                      

                       break;

                   }

                   System.out.println(sentence);                 

                }

            }catch(IOException ioe){

                System.err.println(ioe);

            }

           

        }

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package assignment3retake;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

/**

*

*/

public class Server {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) throws Exception {

        // TODO code application logic here

        try (ServerSocket listener = new ServerSocket(59898)) {

            System.out.println("The capitalization server is running...");

           // TODO Create a thread pool of 10 clients

            ExecutorService pool = Executors.newFixedThreadPool(10);

            while (true) {

                Socket s = listener.accept();

                // TODO call pool.execute() with a parameter which is an instance of the Capitalizer class

                pool.execute(new Capitalizer(s));

            }

        }

    }

}

Homework Answers

Answer #1

I have change your code. I have used DataInputStream and DataOutputStream to recieve and send data from client to server.

Code is 100% runnable.

Run Server.java at first and then client

-----------------------------------------------------------------------------------------------------------------------------------------

Capitalizer.java


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

/**
*
*
*
*/
public class Capitalizer implements Runnable {

private Socket socket;

Capitalizer(Socket socket) {

this.socket = socket;

}

@Override

public void run() {

System.out.println("Connected: " + socket);

try {

DataInputStream in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
  
String sentence = in.readUTF();
  
// reads message from client until "Over" is sent
while (sentence!=null)
{
try
{
sentence=sentence.toUpperCase();
System.out.println(sentence);
sentence = in.readUTF();
  
  
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
  
// close connection

in.close();
  

} catch (Exception e) {

System.out.println("Error:" + socket);

} finally {

try {

socket.close();

} catch (IOException e) {

}

System.out.println("Closed: " + socket);

}

}

}

----------------------------------------------------------------------------------------------------------------------------------------------

Client.java


import java.net.*;

import java.io.*;

import java.io.IOException;

import java.net.Socket;

/**
*
*
*
*/
public class Client {

public static void main(String[] args) {

try {

//connect to server
Socket sock = new Socket("127.0.0.1", 59898);

// takes input from terminal
DataInputStream input = new DataInputStream(System.in);

// sends output to the socket
DataOutputStream out = new DataOutputStream(sock.getOutputStream());

String sentence;

while (true) {

System.out.flush();

sentence = input.readLine();
out.writeUTF(sentence);

if (sentence == null) {

//close the socket
sock.close();

System.out.println("Connection closed");

break;

}

System.out.println(sentence);

}

} catch (IOException ioe) {

System.err.println(ioe);

}

}

}
----------------------------------------------------------------------------------------------------------------------------------------------

Server.java


import java.net.ServerSocket;

import java.net.Socket;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

/**

*

*/

public class Server {

/**

* @param args the command line arguments

*/

public static void main(String[] args) throws Exception {

// TODO code application logic here

try (ServerSocket listener = new ServerSocket(59898)) {

System.out.println("The capitalization server is running...");

// TODO Create a thread pool of 10 clients

ExecutorService pool = Executors.newFixedThreadPool(10);

while (true) {

Socket s = listener.accept();

// TODO call pool.execute() with a parameter which is an instance of the Capitalizer class

pool.execute(new Capitalizer(s));

}

}

}

}

--------------------------------------------------------------------------OUTPUT---------------------------------------------------------------

Server Side

Client 1

Client 2

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each...
Whenever I try to run this program a window appears with Class not Found in Main...
Whenever I try to run this program a window appears with Class not Found in Main project. Thanks in Advance. * * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Assignment10; /** * * @author goodf */ public class Assignment10{ public class SimpleGeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;    /**...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of the SAX parser, I am having trouble changing my code in order to utilize the DOM parser to extract the XML data into the tree structure. Would you be able to explain what changes should be made to the code in order to use the DOM parser instead of the SAX parser? // Java Packages //      import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
1. Paste the code below into your Java IDE and make sure it runs correctly. 2....
1. Paste the code below into your Java IDE and make sure it runs correctly. 2. Create a program that receives numeric data from the user and then adds it to a queue. 3. Create a program that receives text based data and then adds it to a queue. 4. Create a program that automatically populates a queue on start up with 5 pieces of data and then displays each entry in the queue after receiving a command from the...
I cannot for the life of me get this program to run properly. I don't know...
I cannot for the life of me get this program to run properly. I don't know what I'm doing wrong. Could the format of my text files be the issue? Edit: The program works this way, you choose a year and a gender and then enter a name. When you press the button its should give you the ranking or popularity of the name. There are 5 files that I have to search through, named 2006.txt to 2010.txt. First the...