I want to transmit a serialized object over a socket channel. I want make "Hi friend" string as serialized object and then write this object in socket channel while in the other end i want to read the same object and retrieve the data.
All these things I want to do using Java SocketChannel
. How to do this? I have tried like below, but did not get any data in the recipient side.
private static void writeObject(Object obj, SelectionKey selectionKey) { ObjectOutputStream oos; try { SocketChannel channel = (SocketChannel) selectionKey.channel(); oos = new ObjectOutputStream(Channels.newOutputStream(channel)); oos.writeObject(obj); } catch (IOException ex) { ex.printStackTrace(); } } private static Object readObject(SelectionKey selectionKey) { ObjectInputStream ois; Object obj = new Object(); SocketChannel channel = (SocketChannel) selectionKey.channel(); try { ois = new ObjectInputStream(Channels.newInputStream(channel)); obj = ois.readObject(); } catch (Exception ex) { ex.printStackTrace(); } return obj; }
Yes you can transfer a Serialized object via network because Java serialized object remains in form of bytes which can be transmitter via network.
To send an object thru a stream you need to serialize it ie.. the object should implement the serializable interface; this is a 'marker' interface hence you dont need to define any methods. All objects that support the java. io. Serializable interface can be written to streams.
You don't always require serialization/deserialization. If you're sending an object over a network, then it gets serialized to send it via a byte stream. That's the point of serialization, precisely so that you CAN send via a network.
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
Your SocketChannel handling seems to be incomplete, see this complete example for SocketChannels transferring a byte:
/* * Writer */ import java.io.IOException; import java.io.ObjectOutputStream; import java.net.InetSocketAddress; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; public class Sender { public static void main(String[] args) throws IOException { System.out.println("Sender Start"); ServerSocketChannel ssChannel = ServerSocketChannel.open(); ssChannel.configureBlocking(true); int port = 12345; ssChannel.socket().bind(new InetSocketAddress(port)); String obj ="testtext"; while (true) { SocketChannel sChannel = ssChannel.accept(); ObjectOutputStream oos = new ObjectOutputStream(sChannel.socket().getOutputStream()); oos.writeObject(obj); oos.close(); System.out.println("Connection ended"); } } }
And the Reader
/* * Reader */ import java.io.IOException; import java.io.ObjectInputStream; import java.net.InetSocketAddress; import java.nio.channels.SocketChannel; public class Receiver { public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("Receiver Start"); SocketChannel sChannel = SocketChannel.open(); sChannel.configureBlocking(true); if (sChannel.connect(new InetSocketAddress("localhost", 12345))) { ObjectInputStream ois = new ObjectInputStream(sChannel.socket().getInputStream()); String s = (String)ois.readObject(); System.out.println("String is: '" + s + "'"); } System.out.println("End Receiver"); } }
When you first start the Server, then the Receiver, you'll get the following output:
Server's console
Sender Start Connection ended
Receiver's console
Receiver Start String is: 'testtext' End Receiver
This is not the best solution, but follows your use of Java's ServerSocketChannel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With