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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:53:09+00:00 2026-06-12T13:53:09+00:00

I read many example about Java Socket on Network and all of this has

  • 0

I read many example about Java Socket on Network and all of this has same line of this code for client :

sock = new Socket("localhost", PORT);

So, we just can test server and client on the same computer. When in LAN or ad-hoc, if client has no way to know name of the server. (except hard code, input directly name of server to client code, but this is very very ineffective, for example, if I want to write a LAN game, so name of server we cannot know before running game)

I have though about this solution : when the server is created, it will listen who connect to them, and then send back the name of server. But, again, we meet a paradox : the client cannot connect to server to take this data.

So, how to overcome this hard part. (if there is a way using other java library, it’s okay,too).

  • 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-06-12T13:53:10+00:00Added an answer on June 12, 2026 at 1:53 pm

    No, you can’t. That’s why there are sort of “brokers”, centralized components that can be reached using hard coded information (for example a URL), where the list of available resourcea are available. So you need a central server, that the available game servers register to, and the clients can obtain the list.

    (except hard code, input directly name of server to client code, but this is very very ineffective, for example, if I want to write a LAN game, so name of server we cannot know before running game)

    Why not just ask the user to which server he/she wants to connect to?

    I have though about this solution : when the server is created, it will listen who connect to them, and then send back the name of server.

    I think that once the client found the server, this is not of any use.

    EDIT: I think I misread the question a bit. If there is no requirement for this to work over multiple network segments (for participants not on the same WiFi, or LAN), then the referenced post about multicasting might be a viable option to find the available servers.

    EDIT2: wanted to add as comment, but it didn’t fit…

    I didn’t play LAN games since 10 or more years, so I might be a bit off on this, but I remember, that one player had to act as a host, and the other players joined the game. This qould mean, that that player starts a server (basically listens to a predefined port number), and the other players send out multicast packets to find the servers. Multicasting means that everyone on the network segment will get it, and if a process listens on that port, and is backed by the right software, it will respond.

    So in this case, follow the guide @MichaelB suggested: the servers should open a DatagramSocket, and wait until something arrives, and the other players should send the multicast packets. The server should send something back, that would identify what kind of software it is to be able to tell on the client side, if it is a proper server for the client, or something else, that happens to use the same port…

    Sending is not, but receiving a DatagramPacket from a socket is a bit counter-intuitive:

    byte[] buf = new byte[256];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);
    

    After you have the packet, you can call:

    SocketAddress remoteHostAddress = packet.getSocketAddress(); 
    

    to find out the address of your host.

    This should be of good help: Writing a Datagram Client and Server

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read many posts about this problem like [this link][1] and one solution is
I have read many answers to questions about dynamically resizing NSWindows and nothing has
I've read many answers here about this topic, but everyone suggests the BCP ||
I have read many question about improving the performance of C++ and C code
I already read many article about this issue in here, SO. I just want
I've read many articles about known types and i belive my example should work.
I have read many documentation about static variables. Here is one example which I
I read many topics about this problem but I can't find the solution. I
I use python 2.6 and have read many links about removing new line from
I have read many answers regarding this still i am getting confused if i

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.