Solved

Sample Serialized Object Transfer

Posted on 1998-08-17
3
173 Views
Last Modified: 2010-03-30
Could someone show some code on serializing a class across a socket?
0
Comment
Question by:jvb73
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
mjenkins earned 120 total points
ID: 1221969
Here's a quickie example I have laying around.

import java.net.*;
import java.io.*;
 
public class Client
{
        Socket s;
 
        public static void main( String args[] )
        {
                Client c = new Client();
                c.sendObject();
                c.close();
                System.out.println( "Done Sending" );
        }
 
        public Client()
        {
                try
                {
                        s = new Socket( "localhost", 10888 );
                }
                catch( Exception e )
                {
                        e.printStackTrace();
                        System.exit(0);
                }
        }
 
        public void sendObject()
        {
                try
                {
                        OutputStream out = s.getOutputStream();
                        ObjectOutputStream output = new ObjectOutputStream( out );
                        output.writeObject(new Character('a'));
                        output.writeObject(new Long(12345));
                        output.writeObject(new Double(123.456));
                        output.flush();
                        output.close();
                }
                catch( IOException e )
                {
                        e.printStackTrace();
                }
        }
 
        public void close()
        {
                try
                {
                        s.close();
                }
                catch( Exception e )
                {
                        e.printStackTrace();
                        System.exit(0);
                }
        }
}

import java.net.*;
import java.io.*;
 
public class Server
{
        ServerSocket s;
 
        public static void main( String args[] )
        {
                Server c = new Server();
                c.recvObject();
                System.out.println( "Done Recieving" );
        }
 
        public Server()
        {
                try
                {
                        s = new ServerSocket( 10888, 10 );
                }
                catch( Exception e )
                {
                        e.printStackTrace();
                        System.exit(0);
                }
        }
 
        public void recvObject()
        {
 
                try
                {
                        Socket s1 = s.accept();
                        InputStream in = s1.getInputStream();
                        ObjectInputStream input = new ObjectInputStream( in );
                        Character c = (Character)input.readObject();
                        Long l = (Long)input.readObject();
                        Double d = (Double)input.readObject();
                        input.close();
                        s1.close();
                        System.out.println( "SERVER RECIEVED: " + c + " " + l + " " + d );
                }
                catch( Exception e )
                {
                        e.printStackTrace();
                }
        }
}

0
 

Author Comment

by:jvb73
ID: 1221970
Thank you that was very fast. Why Character, Long , Double?
0
 
LVL 3

Expert Comment

by:mjenkins
ID: 1221971
It was from an example I gave for a different question. I just copies the source I had.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now