Google Answers Logo
View Question
Q: Java Programing ( Answered,   0 Comments )
Subject: Java Programing
Category: Computers > Programming
Asked by: mohd_fm-ga
List Price: $50.00
Posted: 16 Feb 2004 20:37 PST
Expires: 17 Mar 2004 20:37 PST
Question ID: 307526
{[[[[[Java for this program}]]]]

Write a program using TCP that performs the following operations: 
Opens a connection to a server through some port of your choice   (> 2048), 
Reads some user input from, 
Sends the input string to the server, 
Obtains the reversed string from the server, 
Prints both 
the original input string and 
the reversed string provided by the server. 

You need to write and run both the client and server programs. 

You should use Java for this program
Subject: Re: Java Programing
Answered By: answerguru-ga on 17 Feb 2004 19:51 PST
Hello mohd_fm-ga,

In writing a client-server application such as this, we need two
seperate applications as well as the ability for the two applications
to interact. The code for your particular request has been adapted
from the Java Sun Tutorial on Socker programming:


// begin server code - place in file named


public class ReverseServer 
public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");

Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
String inputLine = "";
String outputLine = "";

out.println("Please input your string to be reversed: ");

while ((inputLine = in.readLine()) != null) 
int length = inputLine.length(); //lgn=xx.length();

for(int i = 1; i <= length; i++)
outputLine = outputLine + inputLine.substring(length-i,length-i+1);

out.println("Your reversed string: " + outputLine + ". Bye.");

if (outputLine.indexOf("Bye.") != -1) break;


// end server code


// begin client code - place in file called


public class ReverseClient 
public static void main(String[] args) throws IOException {

Socket mySocket = null;
PrintWriter out = null;
BufferedReader in = null;

// change localhost to name of server if being run on different machines
    mySocket = new Socket("localhost", 4444); 
    out = new PrintWriter(mySocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
} catch (UnknownHostException e) {
    System.err.println("Don't know about this host.");
} catch (IOException e) {
    System.err.println("Couldn't get I/O for the connection to this host.");

BufferedReader stdIn = new BufferedReader(new InputStreamReader(;
String fromServer;
String fromUser;

while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);

if(fromServer.indexOf("Bye.") != -1)

fromUser = stdIn.readLine();

if (fromUser != null) 
System.out.println("Client: " + fromUser);


// end server code

You will need to start the server in one command window using:
>java ReverseServer

Then open a new window and execute the client using:
>java ReverseClient

The formatting is a little tricky to get around when posting answers
in the Google Answers environment so I apologize in advance if there
are any skewed code bits. Please do post a clarification request if
any of the above information is unclear :)



Request for Answer Clarification by mohd_fm-ga on 17 Feb 2004 20:51 PST
But I cant ren for my computer.
I thenk i have problem?
Can please send to my e-mail?

Thank you.

Clarification of Answer by answerguru-ga on 17 Feb 2004 21:50 PST

In order to run this program you first need to compile it - I omitted
this information as I assumed you have worked with Java before. You
will need to download the Java Development Kit (free from Sun

Once installed, you need to use the 'javac' command to compile the
.java files that were provided in the original answer as follows:

>javac *.java

This will compile all java files in the current folder, generating two
.class files. From this point you should be able to run the programs.

As a researcher, I cannot correspond with you through email as it
would violate the Researcher Agreement. All correspondences must take
place through this forum.


Request for Answer Clarification by mohd_fm-ga on 18 Feb 2004 22:45 PST
I can't open.
Can you please tech me step-by-step 
how i can dot?

Clarification of Answer by answerguru-ga on 18 Feb 2004 23:14 PST
You're going to have to be more specific - I don't know where you are
having a problem. Were you able to install the JDK?


Request for Answer Clarification by mohd_fm-ga on 21 Feb 2004 09:08 PST
Please answerguru-ga I have 48 hours to give my teacher. But I Can not
run this program I need help.


Clarification of Answer by answerguru-ga on 21 Feb 2004 09:39 PST
Hi mohd_fm,

I would like to help you but I need you to tell me exactly what the
problem is and what you have already done.


Request for Answer Clarification by mohd_fm-ga on 23 Feb 2004 10:55 PST
Hi answerguru-ga ,
What do you think about this program:
package client;


 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0

public class client {

  public client() {
  public static void main(String[] args) {
    client client1 = new client();
    BufferedReader input = new BufferedReader (new InputStreamReader( ));

    String phrase="";
    String phrase2="";
    String ip ="";
    int port=0;
    //Ask for the string
    System.out.println("Enter a string of your choice: ");
     phrase = input.readLine();
     catch (IOException e) {

    //Ask for the server information
      System.out.println("Enter the server address: ");
      ip = input.readLine();
      System.out.println("Enter the destination port number: ");
      port = Integer.parseInt(input.readLine());
    catch (IOException e) {

    //TCP socket
    Socket client;
    //output and input streams
    ObjectOutputStream out;
    ObjectInputStream in;

    //initialize the socket
      client = new Socket(InetAddress.getByName(ip), port);
      out = new ObjectOutputStream(client.getOutputStream());
      in = new ObjectInputStream(client.getInputStream());
      //send the string

      //wait for the response
        phrase2 = (String) in.readObject();
      catch(ClassNotFoundException c)
        System.out.println("received unknown object");

      System.out.println("Original String: "+phrase);
      System.out.println("Returned String: "+phrase2);
    catch(IOException e){



can please compeer between you program and another program?

Thank you.

Clarification of Answer by answerguru-ga on 23 Feb 2004 12:41 PST
Hi mohd_fm-ga,

I'm not sure if you understand how Google Answers
works...clarifications are used just in case there is a question
relating to the answer of a question. You cannot use it to ask new
questions - if you have another question you are welcome to post it.

I cannot answer new questions as part of a clarification request..


Request for Answer Clarification by mohd_fm-ga on 23 Feb 2004 14:06 PST
Thank You.

But I want understand the Quation first and compeer between you
program and another program?

Thank You So much for hellp me.
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy