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
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...
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 =...
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 =...
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...
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...
1. you will just write your name in bytes - once in UTF8 bytes, once in...
1. you will just write your name in bytes - once in UTF8 bytes, once in UTF16 bytes. Name :Andrew Submit: 1. Your .java file 2. A screenshot showing your code ran and gave the right output. ---- /** * In this program you will write your first name in bytes in 2 different encodings. * Then convert the byte array to a String and print it out. * * TODO in lecture: show students how I google to find...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT