Results 1 to 6 of 6
Thread: Looping Help Please
- 01-30-2010, 12:20 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
Looping Help Please
Hey guys im new to java but this morning i decided to look into the basics of java networking. i found this amazing tutorial online so i decided to edit it and see what i could do with it.
The tutorial provided a server and a client, the client connects to the server sends a message automatically then sends "bye" to close the connection. i have changed this where it lets me send a message but it only sends one message please take a look at the code below.
message = (String)in.readObject();
System.out.println("server>" + message);
System.out.print("Type Message: ");
Scanner mainMessage = new Scanner(System.in);
String tehMainMessage = mainMessage.next();
sendMessage(tehMainMessage);
how could i loop this untill mainMessage = bye and then when it = bye it will drop out of the loop and carry on with the rest of the code.
Thanks very much and i know i may be into deep before i can swim but how else are ya gunna learn haha.
Thanks very much
Here is the Full code for the client:
import java.io.*;
import java.net.*;
import java.util.Scanner;
//////////////////////////////////////////////////////////////////////////
// THIS IS THE CLIENT //
//////////////////////////////////////////////////////////////////////////
public class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester(){}
void run()
{
try{
Scanner tehMessage = new Scanner(System.in); // Host - Default localhost
Scanner tehPort = new Scanner(System.in); // Port - Default 2004
//1. creating a socket to connect to the server
System.out.print("Welcome, enter IP address: ");
String localHost = tehMessage.next();
System.out.print("Thanks, enter PORT: ");
int porT = tehPort.nextInt();
requestSocket = new Socket(localHost, porT);
System.out.println("Connected to " + localHost + " in port " + porT + ".");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream() );
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
do{
try{
/*
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage("Hi my server");
message = "bye";
sendMessage(message);
*/
message = (String)in.readObject();
System.out.println("server>" + message);
System.out.print("Type Message: ");
Scanner mainMessage = new Scanner(System.in);
String tehMainMessage = mainMessage.next();
sendMessage(tehMainMessage);
/*
message = "bye";
sendMessage(message);
*/
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
client.run();
}
}
- 01-30-2010, 07:19 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
Java Code:do { System.out.print("Type Message: "); Scanner mainMessage = new Scanner(System.in); String tehMainMessage = mainMessage.next(); sendMessage(tehMainMessage); } while(!tehMainMessage.equals("bye"))
- 01-30-2010, 03:39 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
thanks for the help dude but didnt work :( will try though :)
- 01-31-2010, 02:57 AM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
What is the code for the server?
- 01-31-2010, 03:18 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
Hey dude here is the code for the both of them :)
Provider
Java Code:import java.io.*; import java.net.*; ////////////////////////////////////////////////////////////////////////// // THIS IS THE SERVER // ////////////////////////////////////////////////////////////////////////// public class Provider{ ServerSocket providerSocket; Socket connection = null; ObjectOutputStream out; ObjectInputStream in; String message; Provider(){} void run() { try{ //1. creating a server socket providerSocket = new ServerSocket(2004, 10); //2. Wait for connection System.out.println("Waiting for connection..."); connection = providerSocket.accept(); System.out.println("Connection received from " + connection.getInetAddress().getHostName()); //3. get Input and Output streams out = new ObjectOutputStream(connection.getOutputStream()); out.flush(); in = new ObjectInputStream(connection.getInputStream()); sendMessage("Connection successful"); //4. The two parts communicate via the input and output streams do{ try{ message = (String)in.readObject(); System.out.println("client>" + message); if (message.equals("!bye")) sendMessage("bye"); } catch(ClassNotFoundException classnot){ System.err.println("Data received in unknown format"); } }while(!message.equals("bye")); } catch(IOException ioException){ ioException.printStackTrace(); } finally{ //4: Closing connection try{ in.close(); out.close(); providerSocket.close(); } catch(IOException ioException){ ioException.printStackTrace(); } } } void sendMessage(String msg) { try{ out.writeObject(msg); out.flush(); System.out.println("server>" + msg); } catch(IOException ioException){ ioException.printStackTrace(); } } public static void main(String args[]) { Provider server = new Provider(); while(true){ server.run(); } } }
Java Code:import java.io.*; import java.net.*; import java.util.Scanner; ////////////////////////////////////////////////////////////////////////// // THIS IS THE CLIENT // ////////////////////////////////////////////////////////////////////////// public class Requester{ Socket requestSocket; ObjectOutputStream out; ObjectInputStream in; String message; Requester(){} void run() { try{ Scanner tehMessage = new Scanner(System.in); // Host - Default localhost Scanner tehPort = new Scanner(System.in); // Port - Default 2004 //1. creating a socket to connect to the server System.out.print("Welcome, enter IP address: "); String localHost = tehMessage.next(); System.out.print("Thanks, enter PORT: "); int porT = tehPort.nextInt(); requestSocket = new Socket(localHost, porT); System.out.println("Connected to " + localHost + " in port " + porT + "."); //2. get Input and Output streams out = new ObjectOutputStream(requestSocket.getOutputStream()); out.flush(); in = new ObjectInputStream(requestSocket.getInputStream()); //3: Communicating with the server do{ try{ /* message = (String)in.readObject(); System.out.println("server>" + message); sendMessage("Hi my server"); message = "bye"; sendMessage(message); */ message = (String)in.readObject(); System.out.println("server>" + message); System.out.print("Type Message: "); Scanner mainMessage = new Scanner(System.in); String tehMainMessage = mainMessage.next(); sendMessage(tehMainMessage); /* message = "bye"; sendMessage(message); */ } catch(ClassNotFoundException classNot){ System.err.println("data received in unknown format"); } }while(!message.equals("bye")); } catch(UnknownHostException unknownHost){ System.err.println("You are trying to connect to an unknown host!"); } catch(IOException ioException){ ioException.printStackTrace(); } finally{ //4: Closing connection try{ in.close(); out.close(); requestSocket.close(); } catch(IOException ioException){ ioException.printStackTrace(); } } } void sendMessage(String msg) { try{ out.writeObject(msg); out.flush(); System.out.println("client>" + msg); } catch(IOException ioException){ ioException.printStackTrace(); } } public static void main(String args[]) { Requester client = new Requester(); client.run(); } }
- 01-31-2010, 06:57 AM #6
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
Similar Threads
-
Looping and Recurring
By tim in forum New To JavaReplies: 7Last Post: 12-22-2009, 09:52 PM -
looping problems
By Blakester in forum New To JavaReplies: 4Last Post: 10-05-2009, 09:26 PM -
Looping if statements
By rice in forum New To JavaReplies: 10Last Post: 10-02-2009, 03:50 AM -
how to do conditional looping?
By chennee72 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-09-2008, 01:38 PM -
Looping problem
By Tanilo in forum New To JavaReplies: 1Last Post: 08-01-2008, 07:34 AM
Bookmarks