Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Serialization over Sockets - Java novice 1

Status
Not open for further replies.

phil999

Programmer
Mar 16, 2004
3
0
0
IE
I want to serialize a file (small JPEG, GIF) over sockets and back again. I have found code that serializes an array and passes it across sockets. Can someone edit this code so the customObject being serialized is a file and identify how I save the file to the harddrive.

The following code can be found at:

Code:
CustomObject.java

//
// Serializing Custom Classes Over Sockets - Custom Object
//
import java.io.*;
import java.util.*;

public class CustomObject implements Serializable
{
	private int array[] = null;

	public CustomObject()
	{
	}

	public void setArray(int array[])
	{
		this.array = array;
	}

	public int[] getArray()
	{
		return array;
	}
}

ArrayMultiplierServer.java

//
// Serializing Custom Classes Over Sockets - Server
//
import java.io.*;
import java.net.*;

public class ArrayMultiplierServer
{
	public static void main(String argv[]) throws Exception
	{
		ServerSocket arrayServer;

		arrayServer = new ServerSocket(4000);
		System.out.println("Server listening on port 4000.");

		while(true)
		{
			try
			{
				System.out.println("Waiting for connections.");
				Socket client = arrayServer.accept();
				System.out.println("Accepted a connection from: "+
				client.getInetAddress());

				ClientThread c = new ClientThread(client);
				c.start() ;
			}
			catch(Exception e) {}
		}
	}
}

class ClientThread extends Thread
{
	private Socket client = null;
	private ObjectInputStream ois = null;
	private ObjectOutputStream oos = null;

	public ClientThread(Socket clientSocket)
	{
		client = clientSocket;
	}

	public void run()
	{
		CustomObject x = null;
		CustomObject y = null;
		int dataset1[] = new int[7];
		int dataset2[] = new int[7];
		int result[] = new int[7];

		try
		{
			ois = new ObjectInputStream(client.getInputStream());
			oos = new ObjectOutputStream(client.getOutputStream());

			x = (CustomObject) ois.readObject();
			y = (CustomObject) ois.readObject();

			dataset1 = x.getArray();
			dataset2 = y.getArray();
			// create an array by multiplying two arrays

			for (int i=0;i<dataset1.length;i++)
			{
				result[i] = dataset1[i] * dataset2[i];
			}

			// ship the object to the client
			CustomObject output = new CustomObject();
			output.setArray(result);
			oos.writeObject(output);
			oos.flush();

			// close connections
			ois.close();
			oos.close();
			client.close();
		}
		catch(Exception e)
		{
			System.err.println(e) ;
		}
	}

}







ArrayMultiplierClient.java

//
// Serializing Custom Classes Over Sockets - Client
//
import java.io.*;
import java.net.*;

public class ArrayMultiplierClient
{
	public static void main(String argv[])
	{
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		// two arrays
		int dataset1[] = {3, 3, 3, 3, 3, 3, 3};
		int dataset2[] = {5, 5, 5, 5, 5, 5, 5};

		try
		{
			// open a socket connection
			Socket socket = new Socket("localhost", 4000);

			// open I/O streams for objects - serialization streams
			oos = new ObjectOutputStream(socket.getOutputStream());
			ois = new ObjectInputStream(socket.getInputStream());

			// create two custom serialized objects
			CustomObject so1 = new CustomObject();
			CustomObject so2 = new CustomObject();
	
			int outArray[] = new int[7];
			so1.setArray(dataset1);
			so2.setArray(dataset2);

			// write the objects to the server
			oos.writeObject(so1);
			oos.writeObject(so2);
			oos.flush();

			// read an object from the server - cast to appropriate 
                  // object type
			CustomObject result = (CustomObject) ois.readObject();
			outArray = result.getArray();
			System.out.print("The new array is: ");

			// after unpacking the array, iterate through it
			for (int i=0;i<outArray.length;i++)
			{
				System.out.print(outArray[i] + " ");
			}

			oos.close();
			ois.close();

		} catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}
 
Why bother serializing a class that holds a file ? Just read the file into a byte[] array, and send it over the socket.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top