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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:11:33+00:00 2026-06-18T16:11:33+00:00

I’m trying to build a chat program. I wrote the code and everything worked

  • 0

I’m trying to build a chat program. I wrote the code and everything worked and still works great when I’m using my computer and connecting using 127.0.0.1. I also succeeded to connect successfully between my laptop and my computer, which run on the same router. (I used an internal IP address to do this, 10.0.0.3).

Now I’m trying to make a connection between my router and other routers. And to this I’m trying to connect to an external IP address. I do the port forwarding part through my router and I also made a static IP. When I run the code I always get a “connection refused error”.

Here is the code:

MainServer.java:

import java.util.*;
import java.io.*;
import java.net.*;

public class MainServer {
  private ArrayList<Socket> sockets;
  public MainServer() {
    ServerSocket server_socket;
    try {
      server_socket = new ServerSocket(5005);
      sockets = new ArrayList<Socket>();
      System.out.println("server is now running");
      while(true) {
        Socket socket = server_socket.accept();
        sockets.add(socket);
        try {
          PrintWriter writer = new PrintWriter(socket.getOutputStream());
          writer.println("---you are connected to the server---\r\n");
          writer.flush();
        } catch(Exception e) {e.printStackTrace();}
        System.out.println("server connected to " + socket.getInetAddress());
        Reader reader = new Reader(socket);
        Thread thread = new Thread(reader);
        thread.start();
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    MainServer server = new MainServer();
  }
  class Reader implements Runnable {
    Socket socket;
    public Reader(Socket socket) {
      this.socket=socket;
    }
    public void run() {
      while(true) {
        try {
          InputStreamReader stream_reader = new InputStreamReader(socket.getInputStream());
          BufferedReader reader = new BufferedReader(stream_reader);
          while(true) {
            String str = reader.readLine();
            if(str==null)
              continue;
            System.out.println("message from the client " + socket.getInetAddress() + ": " + str);
            send_back_message(str);
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
    public void send_back_message(String str) {
      try {
        for(Socket send_to_socket: sockets) {
          PrintWriter writer = new PrintWriter(send_to_socket.getOutputStream());
          writer.println(send_to_socket.getInetAddress()+ ": " + str);
          writer.flush();
        }
      } catch(Exception e) {e.printStackTrace();}
    }
  }
}

Client.java:

public Client() {
  frame = new JFrame();
  JPanel panel = new JPanel();
  chat = new JTextArea(20,40);
  chat.setEditable(false);
  JScrollPane scroll = new JScrollPane(chat);
  text = new JTextField(32);
  JButton send = new JButton("Send");
  send.addActionListener(new SendButtonListener());
  panel.add(scroll);
  panel.add(text);
  panel.add(send);
  frame.getContentPane().add(panel);
  frame.setSize(500,500);
  frame.setVisible(true);
  try {
    socket = new Socket("77.126.189.65",5005);
  } catch (Exception e) {
    e.printStackTrace();
  }
  Thread thread = new Thread(new ClientReader());
  thread.start();
}
public static void main(String[] args) {
  Client client = new Client();
}
class SendButtonListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    try {
      PrintWriter writer = new PrintWriter(socket.getOutputStream());
      writer.println(text.getText());
      writer.flush();
      text.setText("");
    } catch (IOException e1) {
      e1.printStackTrace();
    }
  }
}
class ClientReader implements Runnable {
  public void run() {
    try {
      InputStreamReader stream_reader = new InputStreamReader(socket.getInputStream());
      BufferedReader reader = new BufferedReader(stream_reader);
      while(true) {
        String str = reader.readLine();
        if(str==null)
          continue;
        chat.setText(chat.getText() + str + "\r\n" );
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
}

I also tried this tool. When I run the MainServer file and try the tool, I get a positive answer. I also get a positive on my own Eclipse. When my MainServer makes a successful connection, it prints a message about it using these lines:

Socket socket = server_socket.accept();
System.out.println("server connected to " + socket.getInetAddress());

So every time I’m clicking the “check” button in the tool above, I get a message in my Eclipse console (system.out.println part):

server connected to /69.163.149.200

Therefore I think the problem is probably not connected to the MainServer or the portforwading/firewall/static IP.

I also thought maybe the problem occurs because I’m trying connecting from my own router device to my own router. I will leave the MainServer file open for the next half hour so if someone can run the Client.java on his computer it will be helpful.

  • 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-18T16:11:35+00:00Added an answer on June 18, 2026 at 4:11 pm

    What you trying to do is a bit weird but it is possible.

    You just need to know if your router supports NAT Loopback / NAT Reflection and activate it.

    NAT-loopback

    Also known as NAT-hairpinning or NAT-reflection. NAT-loopback is a
    feature in many consumer routers which allows a user to connect to
    its own public IP address from inside the LAN-network. This is
    especially useful when a website (with domain) is hosted at that IP
    address.

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

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place

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.