Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ServerSocket.accept() & Socket subclass

Reply
Thread Tools

ServerSocket.accept() & Socket subclass

 
 
oziris
Guest
Posts: n/a
 
      11-23-2005
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--

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      11-23-2005
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
 
Reply With Quote
 
 
 
 
zero
Guest
Posts: n/a
 
      11-23-2005
"oziris" <(E-Mail Removed)> 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.
 
Reply With Quote
 
oziris
Guest
Posts: n/a
 
      11-23-2005
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--

 
Reply With Quote
 
oziris
Guest
Posts: n/a
 
      11-23-2005
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--

 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      11-23-2005
"oziris" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclass of subclass Fab C++ 0 08-09-2012 09:54 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
subclass a class in the namespace of the that subclass Trans Ruby 8 10-23-2008 07:24 AM
String subclass method returns subclass - bug or feature? S.Volkov Ruby 2 03-12-2006 06:46 PM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM



Advertisments