Hello,

How do I send a class (that extends from JFrame) back and forth between Client and Server? I would grateful for any pseudocode.

Thank you!

I'm guessing you really mean sending an object, not a class. Is that right? An object can be serialized in a number of ways and sent through a stream. There is a serialization mechanism in Java (google it), but you can also write out the data in an object to XML, for example, and then reconstitute an object from XML on the other end. These are just two of many approaches, assuming I understand correctly what you are asking.

Hello Kramer,

You are right - I should have said, Object! Thanks for the pointers. It helped me to get started.

Regards

Hello Kramer,

Following is what I have so far:

The Server sends a JFrame to the Client and the Client displays it. I read up on Serialization. The Server parts makes sense to me. What I am unclear about is how do I receive the JFrame as a byte stream on the Client and display it?

Server:

import java.net.*;
import java.io.*;
import javax.swing.*;
public class Server {
    public static void main(String[] args) throws IOException, ClassNotFoundException{

        ServerSocket serverSocket = null;
     

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }
       Socket socketOut = serverSocket.accept();
       ObjectOutputStream oos = new ObjectOutputStream(socketOut.getOutputStream()) ;
       
       JFrame m = new JFrame("Hello");
       JTextField n = new JTextField(10);
       n.setText("Hello");
       m.add(n);
       
       Object object = m;
       ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
       oos = new ObjectOutputStream(bos) ;
       oos.writeObject(object);
       oos.close();
      
        socketOut.close();
        serverSocket.close();
    }
}

Client:

import java.net.*;
import java.io.*;

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


public class Client
{
     private static Socket socket  = null;
        
    public static void main (String args[]) throws IOException, ClassNotFoundException    {

       
         try {
            socket = new Socket("localhost", 4444);
             } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost");
            System.exit(1);
        }
         ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
         
        // How do I convert the incoming Object to a byte stream?

        JFrame x = (JFrame) ois.readObject();
              
        x.setVisible(true);
        
        ois.close();
        socket.close();
    
     } 
}

Thank you!

Hello kramer,

Thank you! I was able to get it to work for sending a JFrame. It works only if I create a JPanel inside of the "Server.java", add graphic objects to it and then add the JPanel to the JFrame and send the JFrame to the "Client.java".

If I have a class called Panel (which extends from JPanel) and then use a variable of type of JPanel in the "Server.java", and then add this variable to the JFrame and send the JFrame to the "Client.java", it only shows me the JFrame. Any clue why this is the case?

Following are my files:

The Panel Class:

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

public class Panel extends JPanel {


public Panel()
{
   
       JTextField n = new JTextField(10);
       n.setText("Hello");
       JButton q = new JButton("Who are you?");
       add(n);
       add(q);
       setLayout (new FlowLayout());
 }
 
}

Server:

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class Server {
    public static void main(String[] args) throws IOException, ClassNotFoundException{

        ServerSocket serverSocket = null;
     

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }
       Socket socketOut = serverSocket.accept();
       ObjectOutputStream oos = new ObjectOutputStream(socketOut.getOutputStream()) ;
       
       JFrame m = new JFrame("Hello");
       Panel y = new Panel();
       m.add(y);
       m.setSize(300, 300);
       
       Object object = m;

       oos.writeObject(object);
       oos.flush();
       oos.close();

       
       
       
        socketOut.close();
        serverSocket.close();
    }
}

Client:

import java.net.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;

public class Client
{
     private static Socket socket  = null;
        
    public static void main (String args[]) throws IOException, ClassNotFoundException, EOFException
    {

       
         try {
            socket = new Socket("localhost", 4444);
             } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost");
            System.exit(1);
        }
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        JFrame x = (JFrame) ois.readObject();
        x.setVisible(true);
        
      
        socket.close();
    
     } 
}

----------------------------------------------------------------------------

Thank you!

Are you sending your Panel class to the client too, in addition to the Frame?

Hello kramer,

The Panel class is added to the JFrame and then I am sending the JFrame. Should I have send the Panel separately?

Thank you!

Yes, since you are using JFrame (as opposed to your own custom class that extends JFrame), you need to serialize the Panel and send it to the client as well. Another option is to write your own custom JFrame class, and then you could make it serialize any child components contained inside itself.

Hello Kramer,

That was very clear!! I will try that and let you know the result.

Thank you!

Hello kramer,

I tried serializing the Panel class and I am getting an Exception which I am unable to understand.

Following are the files and the Exception follows:

On the Server side, I have Panel.java(same as before) and Server.java as below:

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class Server {
    public static void main(String[] args) throws IOException, ClassNotFoundException{

        ServerSocket serverSocket = null;
     

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }
       Socket socketOut = serverSocket.accept();
       ObjectOutputStream oos = new ObjectOutputStream(socketOut.getOutputStream()) ;
       
       JFrame m = new JFrame("Hello");
       Panel y = new Panel();
       
       
       
       m.setSize(300, 300);
       
       Object object = m;
       Object object1 = y;

       oos.writeObject(object1);
       oos.flush();
       oos.writeObject(object);
       oos.flush();
       oos.close();

             
       socketOut.close();
       serverSocket.close();
    }
}

Client.java:

import java.net.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;

public class Client
{
     private static Socket socket  = null;
        
    public static void main (String args[]) throws IOException, ClassNotFoundException, EOFException
    {

       
         try {
            socket = new Socket("localhost", 4444);
             } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost");
            System.exit(1);
        }
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
       
        JPanel y = (JPanel)ois.readObject();
        JFrame x = (JFrame) ois.readObject();
        x.add(y);
        x.setVisible(true);
  
        
      
        socket.close();
    
     } 
}

The Exception:

java.lang.ClassNotFoundException: Panel
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:247)
	at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
	at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
	at Client.main(Client.java:24)

Thank you!

In your Client class, line 24, you are constructing a JPanel from the stream. That should be a Panel instead.

Hello Kramer,

I had tried that initially and got the following Exception:

java.lang.ClassNotFoundException: Panel
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:247)
	at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
	at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
	at Client.main(Client.java:24)

When I include the Panel.java file on the Client side also, the program works. But isn't my Client/Server program redundant then? I had intended the Client.java to receive the serialized version of the Panel class, deserialize it, add it to the JFrame and then display it. By including a Panel.java on the Client side, it is as if I received nothing from Server.java. Am I understanding this right?

Thank you!

No. You may be failing to distinguish between a class and an instance of that class.
You need Panel.class ( not java) at the client end so that the JRE understands what a "Panel" is. You can then use ObjectStreams to send/receive individual instances of Panel.

Hello,

James - That indeed was the problem! Thank you.

Kramer - thank you again for your help and advise!!

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.