Hazem Hamam

Greenhorn
+ Follow
since Apr 22, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Hazem Hamam

Hello again and Thx for the reply,

I tried changing 3 images with no luck, Used JPG the three times and the same problem. Do I need them to be at certain resoultion, extension or any other paramter for that matter to work?
18 years ago
Might be the easiest question there is but I tried alot of things and it didnt work. I am trying to paint a background picture on a JPanel. After many tries and failures on my own, I found the following example code on the web which many said is working fine:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class BackgroundImage extends JFrame
{
JScrollPane scrollPane;
ImageIcon icon;
Image image;

public BackgroundImage()
{
icon = new ImageIcon("???.jpg");

JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
// Dispaly image at at full size
g.drawImage(icon.getImage(), 0, 0, null);

// Scale image to size of component
//Dimension d = getSize();
//g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);

// Fix the image position in the scroll pane
//Point p = scrollPane.getViewport().getViewPosition();
//g.drawImage(icon.getImage(), p.x, p.y, null);

super.paintComponent(g);
}
};
panel.setOpaque( false );
panel.setPreferredSize( new Dimension(400, 400) );
scrollPane = new JScrollPane( panel );
getContentPane().add( scrollPane );

JButton button = new JButton( "Hello" );
panel.add( button );
}

public static void main(String [] args)
{
BackgroundImage frame = new BackgroundImage();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}

I am supposed to switch the ???.jpg with the name of the image I am trying to use and I did. I am still getting a JPanel with the button but no background. I placed the image in the root of the package, and in the same package as is the JFrame class am using to display the JPanel. Still no luck.

It might be related to my class not finding the image or not able to read it or something I don't know and am out of ideas. Any help is greatly appreciated

Hazem,
18 years ago
Thanks alot,

I will try to work it out without closing the streams.
Hello everyone,

Suppose I have created a socket and a ServerSocket, established the link between them and created input and output streams like this:
Input = new ObjectInputStream(comSocket.getInputStream());
output = new ObjectOutputStream(comSocket.getOutputStream());

I am done reading or writing to the socket now and I decided to close the streams so I only called:
input.close();
output.close();

I did not call Socket.close() on either side.

Now I decided I need to read or write again, when I try to create new I/O streams over the socket I get an exception: Socket is Closed.

What I think is that calling input.close() or output.close() closes the socket as well.

1. Is my assumption correct?
2. Is there a way to avoid closing the socket when closing streams on it?
3. Is it better to keep input and output unclosed and just pass them to other methods or classes for use until I decide I should close the socket?

Thanks
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();
}
}
}
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
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
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();
}
}
}