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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:52:35+00:00 2026-06-17T08:52:35+00:00

I have this simple server code: public class Server { public static void main(String[]

  • 0

I have this simple server code:

public class Server {

 public static void main(String[] args) {

    try {
        ServerSocket sSocket = null;
        int serverPort = 57293;
        try {

            sSocket = new ServerSocket(serverPort);
            Socket userSocket = sSocket.accept();
            System.out.println(userSocket.getInetAddress().toString());

        } catch (IOException listenEX) {
            System.out.println("Could not listen on port: " + serverPort);
        }

        sSocket.close();
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
      }
    }

 }

the server listens on port 57293 and on IP 0.0.0.0

it just waits for one connection and then prints the IP address of the client. Here is the client code:

 public class Client {

public static void main(String[] args) throws IOException {

    Socket serverSocket = new Socket("192.168.217.1", 57293); 
    DataInputStream in = new DataInputStream(serverSocket.getInputStream());
    DataOutputStream out = new DataOutputStream(serverSocket.getOutputStream());

    }
 }

now as a server IP inside the client I enter the local ip of my computer. If I do so the server will print the same IP as a result which is /192.168.217.1

otherwise if in the client’s code I change the line

Socket serverSocket = new Socket("192.168.217.1", 57293);
to
Socket serverSocket = new Socket("127.0.0.1", 57293);

I just changed the servers IP the server will print the 127.0.0.1 IP to be the client’s IP address.

I don’t understand what’s going on here. It seems like the server is not printing the client’s IP address but the address of itself.

How can I avoid this from happening? I would want the server to print 192.168.217.1 if the client runs locally as well

  • 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-17T08:52:35+00:00Added an answer on June 17, 2026 at 8:52 am

    127.0.0.1 is the loopback address. It is only visible from inside that host. The only way the client can connect to it is to use it itself. So the source and target addresses will be the same.

    I don’t understand why this is a problem for you. 127.0.0.1 always means the local host, regardless of what the host’s external IP might be. There is nothing wrong with the result you are getting. It is a perfectly valid IP address and it correctly identifies the client.

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

Sidebar

Related Questions

I have a really simple Java code: public class Main { public static void
I have this code for server code in c, under unix: /* A simple
I have this simple code to perdiodically access a pop3 server and check if
We have a ubuntu test server that this simple script works fine on where
I have created my web socket server and client using this simple tutorial here
I have this simple piece of code : int a = 1, b =
I have this simple code (for making things shorted, the important bits are probably
I have this simple code, using Joda-time. Works fine, but I have a problem.
I have problem installing simple Windows Service, my code looks like this: using System;
I have the following code in Java: public class ServerInfo { int serverId; int

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.