ServerSocket.accept() & Socket subclass

Discussion in 'Java' started by oziris, Nov 23, 2005.

  1. oziris

    oziris Guest

    hello !

    - Why doing that is incorrect ?

    --- code ---
    ServerSocket serverSocket = new ServerSocket(11000);
    MySocket socket = serverSocket.accept();
    --- /code ---

    where MySocket is a subclass of java.net.Socket.

    - Is there a mean to achieve this without using an encapsulation like

    MySocket mySocket = new MySocket(java.net.Socket socket) ?

    Thanks.

    -o--
     
    oziris, Nov 23, 2005
    #1
    1. Advertising

  2. On 23 Nov 2005 04:58:49 -0800, oziris wrote:
    > I had to write a specific Socket class. So now I would
    > ServerSocket.accept() returns an instance of this class.


    Have you looked at ServerSocket.setSocketFactory()?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Nov 23, 2005
    #2
    1. Advertising

  3. oziris

    zero Guest

    "oziris" <> wrote in news:1132735967.644492.234110
    @z14g2000cwz.googlegroups.com:

    > hello !
    >
    > - Why doing that is incorrect ?
    >
    > --- code ---
    > ServerSocket serverSocket = new ServerSocket(11000);
    > MySocket socket = serverSocket.accept();
    > --- /code ---
    >
    > where MySocket is a subclass of java.net.Socket.
    >


    That looks fine to me. Why do you think it is incorrect?


    > - Is there a mean to achieve this without using an encapsulation like
    >
    > MySocket mySocket = new MySocket(java.net.Socket socket) ?
    >
    > Thanks.
    >


    What are you trying to do here?

    Some more information (and code) would be nice.
     
    zero, Nov 23, 2005
    #3
  4. oziris

    oziris Guest

    I had to write a specific Socket class. So now I would
    ServerSocket.accept() returns an instance of this class.

    The instruction

    MySocket socket = (MySocket)serverSocket.accept();

    throws a ClassCastException.

    According to a reply to the same message on fr.comp.lang.java, the
    means seems to consist in extend SocketImpl and related classes.

    -o--
     
    oziris, Nov 23, 2005
    #4
  5. oziris

    oziris Guest

    Here the reply of Fabien Bergeret on fr.comp.lang.java

    Alors, faut creer une classe MySocket heritant de SocketImpl, puis
    creer
    une classe MySocketImplFactory qui implemente SocketImplFactory, et
    enfin appeler la methode statique setSocketImplFactory de la classe
    Socket.
    C'est tout ! :)

    In English

    You must create a class MySocket which extends SocketImpl and then a
    class MySocketImplFactory which implements SocketImplFactory. And
    finally you have to call the static method Socket.setSocketImplFactory.
    That's all :)

    Subject closed.

    Thanks.

    -o--
     
    oziris, Nov 23, 2005
    #5
  6. oziris

    zero Guest

    "oziris" <> wrote in news:1132750729.714005.108840
    @g47g2000cwa.googlegroups.com:

    > I had to write a specific Socket class. So now I would
    > ServerSocket.accept() returns an instance of this class.
    >
    > The instruction
    >
    > MySocket socket = (MySocket)serverSocket.accept();
    >
    > throws a ClassCastException.
    >
    > According to a reply to the same message on fr.comp.lang.java, the
    > means seems to consist in extend SocketImpl and related classes.
    >
    > -o--
    >


    That's not the same code as what you gave in the original post :)

    This of course fails because serverSocket.accept() returns a Socket, not
    MySocket. MySocket is a Socket, but not the other way around. The
    SocketFactory method mentioned in the other post could work, or you can do
    what you originally said:

    MySocket socket = new MySocket(serverSocket.accept());

    The SocketFactory way is cleaner, but more typing.
     
    zero, Nov 23, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Kuhn
    Replies:
    4
    Views:
    964
    Thomas Kuhn
    Jul 7, 2003
  2. Jan Liße
    Replies:
    15
    Views:
    15,513
    Tony Morris
    Feb 19, 2004
  3. Replies:
    1
    Views:
    874
    Gordon Beaton
    Mar 26, 2007
  4. Paul J. Lucas

    Hanging on ServerSocket.accept()

    Paul J. Lucas, Jul 10, 2008, in forum: Java
    Replies:
    5
    Views:
    2,111
  5. Jozza

    ServerSocket accept

    Jozza, Nov 6, 2008, in forum: Java
    Replies:
    1
    Views:
    408
    Knute Johnson
    Nov 6, 2008
Loading...

Share This Page