Trouble with Server, Client! Please help!

Ok, so my problem seems basic but I really have no idea how to fix this.

Basically, when I run my java server on my machine, and give my buddy my java client… he cannot connect to my server. The reason being is my computer is behind a router. I know my LAN IP, and I know my WAN IP… how do I get it so that I can have him connect to my computer and port, through my router?
For example, router is IP “123.123.123.123”, my server is on my machine with IP and port: “123.123.123.1: port 1234”. My buddy has the client that is trying to connect to my machine on the port 1234… how do I get him to do this through my router? I’ve done some research and apparently “port-forwarding” is required. How do I do this?
Can someone also please type up an example or a link to a good example? Has anyone come through this problem before? Thank you for reading!

is a setting and the sistem is called NAT. sometimes you can autoset the port-forwarding on your router using UPNP

Open your router’s page (which is the default gateway), and find a page that sets the port-forwarding rules. There you can set a port that your router forwards to your internal IP address.

Just wondering, when I set it up - does it automatically open the port?

I think I sort of got it to work. Only problem is when I run my java program it is able to connect to my server because of port-forwarding, but I still can’t send a message?

Ok. So I finally got my Client to connect to my Server through my router using port-forwarding. But for some reason, I can’t send a message, or should I say, my server can not receive a message. Why not?

Ok. So it’s kind of a weird problem. I can send messages very easily locally. But when I try and send a message externally (through my router), and can connect with the socket but can’t pass messages through it. Or it sends a message, and my router is not sending the message to my server…

Install a packet sniffer or use tcpdump to see if the data is getting through. It is possible that your ISP is blocking the data.

However, it could be from a multitude of possible reasons and we don’t have enough information from your posts to pinpoint the cause.

Can you can give us ip addresses of your server, client, and friend’s machine and the layout of your internal network? (You can mask out/fake the last 2 quads if you want)

can we maybe get a bit of code? cause it might just be that something is wrong in the message sending portion :stuck_out_tongue:

Ya sure. Here’s the code…

Here’s the main for client.


package grid

import java.awt.*;
import java.awt.event.*;

public class Main 
{

    public static void main(String[] args)
    {
        GridClient frame = new GridClient();
	frame.setTitle("Grid Client");
        WindowListener l = new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                        System.exit(0);
                }
        };

        frame.addWindowListener(l);
        frame.pack();
        frame.setVisible(true);
	frame.listenSocket();
    }
}

Here’s the code for my client

package grid;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;

public class GridClient extends JFrame implements ActionListener
{
    JLabel text, clicked, error;
    JButton button;
    JPanel panel;
    JTextField textField;
    Socket socket = null;
    PrintWriter out = null;
    BufferedReader in = null;
        
    GridClient()
    {
        text = new JLabel("Text to send over socket: ");
        error = new JLabel("");
        textField = new JTextField(20);
        button = new JButton("Send");
        button.addActionListener(this);
        
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(Color.WHITE);
        getContentPane().add(panel);
        panel.add("North", text);
        panel.add("East", error);
        panel.add("Center", textField);
        panel.add("South", button);
    }
    
    public void actionPerformed(ActionEvent event)
    {
        Object source = event.getSource();
        
        if (source == button)
        {
            String text = textField.getText();
            out.println(text);
            textField.setText(new String(""));
        }
    }
    
    public void listenSocket()
    {
        
        try
        {
        socket = new Socket("\\Router IP Address\\", 80);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            error.setText("Unknown host.");
        } catch  (IOException e) {
            error.setText("Can not establish connection.");
        }
    }
}

Here’s the code for my server.

package gridserver;

import java.net.*;
import java.io.*;
import java.util.*;
import java.security.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class GridServer extends JFrame implements ActionListener
{
    JButton button;
    JLabel label = new JLabel("Text received over socket: ");
    JPanel panel;
    JTextArea textArea = new JTextArea();
    ServerSocket server = null;
    Socket client = null;
    BufferedReader in = null;
    PrintWriter out = null;
    String line;
    
    GridServer()
    {
        button = new JButton("Receive");
        button.addActionListener(this);
        
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(Color.WHITE);
        getContentPane().add(panel);
        panel.add("North", label);
        panel.add("Center", textArea);
        panel.add("South", button);
    }
    
    public void actionPerformed(ActionEvent event)
    {
        Object source = event.getSource();
        
        if (source == button)
        {
            textArea.setText(line);
            System.out.println(line);
        }
    }
    
    public void listenSocket()
    {
        try
        {
            server = new ServerSocket(19999);
        } catch (IOException e) {}
        
        try
        {
            client = server.accept();
        } catch (IOException e) {}
        
        try
        {
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {}
        
        while (true) 
        {
            try
            {
                line = in.readLine();
            } catch (IOException e) {}
        }
    }
    
    public void finalize()
    {
        try
        {
            in.close();
            out.close();
            server.close();
        } catch (IOException e) {}
    }
    
    public static void main(String[] args)
    {
        GridServer frame = new GridServer();
        frame.setTitle("Grid Server");
        WindowListener l = new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                        System.exit(0);
                }
        };
        frame.addWindowListener(l);
        frame.pack();
        frame.setVisible(true);
	frame.listenSocket();
    }
}

And I’ve already set up the router so port forwarding works. Anything that comes in on port 80 is forwarded to my computer’s port 19999. I’ve even opened my port 19999 as well on my computer (so no firewall should be there). What the heck is the problem then?

AHA!

I found a fix!

So, apparently if the client and the server are running on the same machine it won’t work…
But I got a buddy to test it out and it works BEAUTIFULLY.

LOL! Nice to hear it worked out :slight_smile:

EDIT: wait…your router allows you to set which port to forward to also? I’ve never heard of a router being able to do that, I always thought it forwards it to the same port that was requested.

Welp, my router allows it! It’s the D-Link Dl-524. It’s quite awesome.

yeah, mine does it to, you can choose the port that triggers it, and what port to forward it to on that computer

Wow I hate my router now :frowning: