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:
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
SERVER PROGRAM
// begin server code - place in file named ReverseServer.java
import java.net.*;
import java.io.*;
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.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
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;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
// end server code
CLIENT PROGRAM
// begin client code - place in file called ReverseClient.java
import java.io.*;
import java.net.*;
public class ReverseClient
{
public static void main(String[] args) throws IOException {
Socket mySocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
// 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.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to this host.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if(fromServer.indexOf("Bye.") != -1)
{
break;
}
fromUser = stdIn.readLine();
if (fromUser != null)
{
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
mySocket.close();
}
}
// 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 :)
Cheers!
answerguru-ga |
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
mohd_fm-ga,
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
Microsystems):
http://java.sun.com/j2se/1.4.2/download.html
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.
answerguru-ga
|
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?
answerguru-ga
|
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.
Thank's
|
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.
answerguru-ga
|
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;
import java.io.*;
import java.net.*;
/**
* <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( System.in ));
String phrase="";
String phrase2="";
String ip ="";
int port=0;
//Ask for the string
System.out.println("Enter a string of your choice: ");
try{
phrase = input.readLine();
}
catch (IOException e) {
System.out.println(e);
}
//Ask for the server information
try{
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) {
System.out.println(e);
}
//TCP socket
Socket client;
//output and input streams
ObjectOutputStream out;
ObjectInputStream in;
//initialize the socket
try{
client = new Socket(InetAddress.getByName(ip), port);
out = new ObjectOutputStream(client.getOutputStream());
in = new ObjectInputStream(client.getInputStream());
out.flush();
//send the string
out.writeObject(phrase);
out.flush();
//wait for the response
try{
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){
e.printStackTrace();
}
}
}
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..
answerguru-ga
|
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.
|