Hello Again,
I hope am not annoying
I just want to undrestand this to the best.
I couldnt put the actual project am working on and Still having trouble with cuz its so big (I sorta get out of control when I want to learn something
)
I just created a whole new server and client based on the same communication technique, and to my surprise they work, regardless of calling reset or not......
I tried it with reset and without reset..........it works.
My current questions:
1. Is calling reset on the ObjectOutputStream important?
2. Well it cause the problem mentioned earlier (Not sending the actual instance just sending a note "Same instance sent")?
Here are the classes am using, they work fine regardless of calling reset or not.
------------------------------------------The Client Class---------------------------------
package Server;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import java.awt.*;
public class TrialClient implements Runnable{
Socket socket;
ObjectInputStream input;
ObjectOutputStream output;;
public TrialClient() {
}
public static void main(String [] args) {
new TrialClient().run();
}
public void run() {
try {
socket = new Socket("localhost",5000);
input = new ObjectInputStream(socket.getInputStream());
output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
String message = "Hazem";
while (!message.equals("CLOSE")){
message = JOptionPane.showInputDialog(this,"Enter the new message");
PageReport PR = new PageReport();
PR.name = message;
output.writeObject(PR);
output.flush();
outout.reset(); // try deleting this line it shouldnt affect the operation.
}
System.exit(0);
}
catch (UnknownHostException UHE) {
UHE.printStackTrace();
}
catch (IOException IOX) {
IOX.printStackTrace();
}
}
}
--------------------------------------The Report Class-----------------------------------------
package Server;
import java.io.Serializable;
public class PageReport implements Serializable{
public String name = "Hazem";
public PageReport() {
}
}
------------------------------Following is the server Class-------------------------------------------
package Server;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import Client.*;
public class ServerManager implements Runnable {
ObjectInputStream input;
ObjectOutputStream output;
Socket clientSocket;
public ServerManager(Socket passedSocket, ObjectInputStream passedInput, ObjectOutputStream passedOutput) {
input = passedInput;
output = passedOutput;
clientSocket = passedSocket;
}
public void run() {
String message = "Whatever";
while(!message.equals("CLOSE")) {
try {
PageReport RPR = (PageReport)input.readObject();
message = RPR.name;
System.out.println("This is the Server, I just read " + message + " from the Client");
}
catch (InterruptedIOException IIOE){
continue;
}
catch (IOException IOX) {
IOX.printStackTrace();
break;
}
catch (ClassNotFoundException CNFE) {
CNFE.printStackTrace();
}
}
try {
input.close();
output.close();
clientSocket.close();
}
catch (IOException IOX) {
IOX.printStackTrace();
}
}
}
---------------------------------------------Following is the server manager class-----------------------------
package Server;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import Client.*;
public class ServerManager implements Runnable {
ObjectInputStream input;
ObjectOutputStream output;
Socket clientSocket;
public ServerManager(Socket passedSocket, ObjectInputStream passedInput, ObjectOutputStream passedOutput) {
input = passedInput;
output = passedOutput;
clientSocket = passedSocket;
}
public void run() {
String message = "Whatever";
while(!message.equals("CLOSE")) { //listen for actions all the time
try {
PageReport RPR = (PageReport)input.readObject();
message = RPR.name;
System.out.println("This is the Server, I just read " + message + " from the Client");
}
catch (InterruptedIOException IIOE){
continue;
}
catch (IOException IOX) {
IOX.printStackTrace();
break;
}
catch (ClassNotFoundException CNFE) {
CNFE.printStackTrace();
}
}
try {
input.close();
output.close();
clientSocket.close();
}
catch (IOException IOX) {
IOX.printStackTrace();
}
}
}