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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:56:22+00:00 2026-06-17T00:56:22+00:00

I have the following basic server/client java code, that works when server and client

  • 0

I have the following basic server/client java code, that works when server and client are in the same network:-

  1. Connected via the same router.

  2. Client directly connected to Server over Wifi.

The use case desired:
Client knows Server IP/Port (which may be local or anywhere over the internet). Client sends byte array to Server (which is listening to the port). Server processes the received byte array.

Here’s the code (only relevant snippet, omitted try-catch blocks here):

Server.java

public static final int SERVERPORT = 4444;
byte[] inBuffer = new byte[1024];
int bytes

ServerSocket serverSocket = new ServerSocket(SERVERPORT);
Socket client = serverSocket.accept();
InputStream inputStream = client.getInputStream();

bytes = inputStream.read(inBuffer);

/* now process input */

Client.java

OutputStream tmpOut;


/* Server IP and PORT are known to the client, manually entered by user */
InetAddress serverAddress = InetAddress.getByName(ServerIP);
serverSocket = new Socket(serverAddress,SERVERPORT);
tmpOut = serverSocket.getOutputStream();
tmpOut.write(buffer);

This works when server is in same network as client, as expected. If I however, connect server to a different network, and client to another network, and then run the above client code, using the Public IP of the server, it does not connect to the server.

These previous questions suggest that the client-side router can’t find the server, and the server needs to forward it’s port:

Socket Listener to non-local IP

Non-local connection with sockets

Socket server on WAN

Questions (may be naive and basic):

  1. Is there a way to do this without port forwarding and using Java?

  2. If port forwarding is necessary, how do I forward application port from inside the server code. My user will be installing the server code on his/her machine, and I wouldn’t trouble the user to manually forward the port.

  3. How does a known server like Google or Stackoverflow listen to incoming streams from random clients. How are these servers different (by concept) from the above server code I have posted?

  4. Do I need to implement my server as a public web server, just to receive a byte array?

If not code, conceptual insights appreciated. Thanks!

  • 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-17T00:56:23+00:00Added an answer on June 17, 2026 at 12:56 am

    When I searched for NAT via Java, found the following answer on SO:

    Sockets through NAT

    The above led me to hole-punching. I found more conceptual information about hole-punching and firewalls, NATs in the answer to this question:

    Hole Punching and NAT

    More resources based on above discussion:

    P2P across NAT

    These resources have helped me understand the concept completely. I’ll post my code in java when done.

    Thanks paulsm4

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

Sidebar

Related Questions

I have the following scenario: Server: Jetty (with configured JAAS) Client: Jersey invoked via
I have the following code: <input type=text ID=JOB_CODETextBox runat=server value='<%# Eval(JOB_CODE) %>' readonly=readonly onclick=$('#basic-modal-content').modal(
Ok I'm following this tutorial for a basic PHP webservice server/client setup: http://www.java-samples.com/showtutorial.php?tutorialid=1186 Now
I have a basic web service that returns the following object as JSON: public
I have a client application that connects to a server. The server uses hibernate
I have the following PHP code which simply grabs a URL behind HTTP-Basic authentication
I have the following code (almost an exact copy of the Chat server example
I have build a basic .NET server-client infrastructure using TcpListener and SocketClient. It is
I am implementing a really basic server-client model in Java, by using UDP sockets
Say I have the following basic if-statement: if (A ~= 0) % do something

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.