Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement accept(SocketImpl s) and object copying

Tags:

java

copy

sockets

I'm implementing a Positive Acknowledgement and Retransmission transport protocol in terms of RAW sockets and in such cases a subclass of SocketImpl is needed. When implementing acceptmethod I need something like this:

protected void accept(SocketImpl s) {
    ...
    s.copy(socket);
}

where socket is an already computed SocketImpl object that must be copied to an already initialized SocketImpl object s .i.e I need a mutable copy method s.copy(socket) that would copy source socket to target s. I know there exists Object clone() but it returns a new Object and what I need is to mutate s.

BTW some of you think that mutating a parameter, in this case s, is a broken design. It's not the only example in Java standard libraries. implAccept(Socket s) of ServerSocket class is another example. But this is how Sun/Oracle engineers have designed it. I'd like to know why this is such a bad design

At first sight it would seem that it has some sense to provide for a generic shallow target.copy(source) where target = souce.clone() would be equivalent to target = new TheClass(); target.copy(source); but since it doesn't exist (possibly for good reason, please explain) the only way to go is to program a custom field-by-field copy method.

Am I right? Thanks.

like image 471
francesc Avatar asked Aug 18 '26 02:08

francesc


1 Answers

You have misunderstood what this method is for and how it works. I've done this many times. The SocketImpl provided to implAccept() is for the newly accepted socket, not the ServerSocket. So you don't need a second copy. All you need to do is wrap this SocketImpl in a Socket, or your own derived class of Socket. There is even a Socket constructor provided for the purpose, curiously enough.

like image 171
user207421 Avatar answered Aug 20 '26 15:08

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!