?
Solved

Sample Serialized Object Transfer

Posted on 1998-08-17
3
Medium Priority
?
195 Views
Last Modified: 2010-03-30
Could someone show some code on serializing a class across a socket?
0
Comment
Question by:jvb73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
mjenkins earned 480 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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