aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes reading from a socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "reading from a socket" Watch "reading from a socket" New topic
Author

reading from a socket

Hazem Hamam
Greenhorn

Joined: Apr 22, 2006
Posts: 8
Hello ,

I am new to Java networking and am working on this project to get myself through. I created a server program that reads Strings from client sockets and performs operations based on the commands.

The server is supposed to do the following when it reads the String "Save"

Read a Class I designed myself using readObject
save the Class to disk.

the program works, when I click save on the client side it, the Client does a writeObject() followed by a flush(). The server reads the Object and saves it to disk.

The problem is if I made some changes on the report, the client side updates the reportClass fine and writes the Updated report correctly to the Socket. However for some reason the server reads the un-updated report and saves it to Disk....

I checked the report step by step and found that when the server reads again from the Socket, it reads the updated String command "Save" or "Client Exit" correctly, but when it reads the reportClass I passed it reads the old un-updated report again.

I am using only readObject and writeObject on the sockets.

any help on what I might be doing wrong will be very appreciated, Following is the Code am working on:

private void handleMessage(String request) throws IOException, ClassNotFoundException {
if (request.equals("Save")) {
assignedPR = (PageReport)input.readObject();
// ^ reads the report once, it never reads any newly passed report
trialSaveToDB(assignedPR);
}
if (request.equals("User Exit")) {
listener.userExit();
try {
clientSocket.close();
}
catch (IOException IOX) {
IOX.printStackTrace();
}
}
}

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

That's how ObjectOutputStreams work; the first time you write an object, the object's data is written; when you write the same object again, only a note that says "the same object as before" is written; changes in member data won't affect this. To force the Stream to re-send an object as if it were new, call reset() on the ObjectOutputStream in the client.


[Jess in Action][AskingGoodQuestions]
Hazem Hamam
Greenhorn

Joined: Apr 22, 2006
Posts: 8
Hello again,

Thanks for the responce, I called reset on the ObjectOutputStream but it didnt work. This is the code am using:

public void tempSaveFile(PageReport PPR) {
try {
output.writeObject("Save");
output.flush();
output.writeObject(PPR);
output.flush();
output.reset();
}
catch (IOException IOX) {
IOX.printStackTrace();
}
}
output is the ObjectOutputStream of the threaded ClientManager, ClientManager writes String "Save" then flush. Then it writes the pageReport (my report Class) and flush. I added reset right after flush but it didnt work.

The Sequence am thinking is like this:

Client------------------------------------------------Server
writeObject("Save") flush()____________________readObject("Save")
writeObject(PPR) flush()_______________________readObject(PPR)
reset()

I tried it the other way around (with reset before writeObject) and am still getting the same problem.

any more suggestions?

thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No, no more suggestions. I've told you what the problem is: that's how ObjectOutputStreams behave. Now you just have to figure out why the fix didn't "take" -- perhaps you didn't recompile, or are still using old class files in a jar, or something along those lines. But that was the problem -- persevere, and things will work.
Hazem Hamam
Greenhorn

Joined: Apr 22, 2006
Posts: 8
Hello again,

I am still working on it, am sure I am compiling and not using any old classes and its still not working.

The class PageReport is transferred back and forth many times during any most communication cycles, do you think I should call the reset everytime I do writeObject ?

Thanks again for your responce I will try to figure out the rest on my own.

Hazem
Hazem Hamam
Greenhorn

Joined: Apr 22, 2006
Posts: 8
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();
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading from a socket