Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 364239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:25:09+00:00 2026-05-12T13:25:09+00:00

I want to transmit a serialized object over a socket channel. I want make

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:25:10+00:00Added an answer on May 12, 2026 at 1:25 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to transmit data over the network, but I don't want to use
want to know why String behaves like value type while using ==. String s1
I want to archive the app and then transmit it to the app store.
I want to make a highchart like this http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-labels/ I have a stored procedure
Want the function to sort the table by HP but if duplicate HPs then
Want to code a key pad for an calculator. What I want to make
in my web application i want to transmit some values through the url between
I want to transmit data from a Queue using Twisted. I currently use a
I have a file on my server or well in this localhost. I want
How to implement java RMI over SSH v2 protocol.Is this possible?.If its not possible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.