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 4033620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:52:09+00:00 2026-05-20T11:52:09+00:00

I have a class ClientSocket that extends class Socket. The reason I extend class

  • 0

I have a class ClientSocket that extends class Socket. The reason I extend class Socket, is that I want to include a String clientId that uniquely identifies a client on the other side of the socket. I also have a method called ‘getClientId’ that returns the clientId.

I have a ServerSocket instance running on my server that accepts socket connections like so:

public Socket acceptNextConnection() throws IOException {
    Socket socket = serverSocket.accept();

    // put the client socket into the map
    ClientSocket clientSocket = (ClientSocket) socket;
    clientConnections.put(clientSocket.getClientId(), clientSocket);

    return clientSocket;
}

As you can see, my cast here is incorrect as a Socket instance IS NOT a ClientSocket instance and so I get a class cast exception, as I expected. As is evident in the code, I want to call ‘getClientId’ on class ClientSocket. However I can’t do this because the serverSocket.accept method returns a Socket object at runtime, not a ClientSocket. My question is, is there a way around this? If not, is there a neater way of doing what I want, which is being able to uniquely identify a socket that is bound to a client? An ip address can be subject to change.

Thanks for the help.

  • 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-20T11:52:10+00:00Added an answer on May 20, 2026 at 11:52 am

    You can have a ServerClientSocket which return a ClientSocket from accept. Something like

    public class ServerClientSocket extends ServerSocket {
      public ClientSocket accept() throws IOException {
      if (isClosed())
        throw new SocketException("Socket is closed");
      if (!isBound())
        throw new SocketException("Socket is not bound yet");
      ClientSocket s = new ClientSocket((SocketImpl) null);
      implAccept(s);
      return s;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class that extend FragmentActivity in it I add fragment to layout as
I have an very strange error: when I want to use the SocialServer::Client class
I have created this asynchronous client socket that connects to a server and keeps
I have a sample TCP client and server application that I want to run
I have class grades that I need to check if a specific grade is
I have class with back reference: public class Employee : Entity { private string
I have class Meat extends Food { $var = Food::foodFunction… } I need to
I have class that looks like this: class A { public: class variables_map vm
I have a simple socket class: MySocketLib.py .. import socket class socklib(): def create_tcp_serversocket(self,port):
I have class ParentClass that observes an NSNotification. ParentClass handles the notification. ChildClass inherits

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.