ServerSocket.accept() & Socket subclass

O

oziris

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--
 
G

Gordon Beaton

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
 
Z

zero

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.
 
O

oziris

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--
 
O

oziris

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--
 
Z

zero

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top