Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Trouble with Server, Client! Please help!  (Read 1731 times)
0 Members and 1 Guest are viewing this topic.
Offline HyperReal

Senior Newbie





« Posted 2011-11-13 03:15:24 »

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!
Offline lesto

Senior Newbie





« Reply #1 - Posted 2011-11-13 03:38:09 »

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

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-11-13 03:59:34 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HyperReal

Senior Newbie





« Reply #3 - Posted 2011-11-13 04:03:33 »

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?
Offline HyperReal

Senior Newbie





« Reply #4 - Posted 2011-11-13 04:29:49 »

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?
Offline HyperReal

Senior Newbie





« Reply #5 - Posted 2011-11-13 05:02:55 »

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...
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #6 - Posted 2011-11-13 05:28:24 »

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)
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #7 - Posted 2011-11-13 14:25:26 »

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

Senior Newbie





« Reply #8 - Posted 2011-11-13 15:57:30 »

Ya sure.  Here's the code..

Here's the main for client.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
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?
Offline HyperReal

Senior Newbie





« Reply #9 - Posted 2011-11-13 17:28:37 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-11-13 18:16:22 »

LOL! Nice to hear it worked out Smiley

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.

Offline HyperReal

Senior Newbie





« Reply #11 - Posted 2011-11-13 18:29:53 »

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

JGO Coder


Medals: 5



« Reply #12 - Posted 2011-11-13 18:46:10 »

LOL! Nice to hear it worked out Smiley

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.
yeah, mine does it to, you can choose the port that triggers it, and what port to forward it to on that computer
Online ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-11-13 19:09:34 »

Wow I hate my router now Sad

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!