Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: How to search a text file?! Please help! on: 2011-11-17 23:23:23
Ok... I figured it out..

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  
package test;

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

public class TestFileReader
{
    static String getPassword( String userName )
    {
        try
        {
            BufferedReader r = new BufferedReader( new FileReader("file"));
            String line = null;
           
            while ((line = r.readLine()) != null)
            {
                String[] pair = line.split(" +");
               
                if (pair[0].equalsIgnoreCase(userName))
                {
                    r.close();
                    return pair[1];
                }
            }
            return null;
        } catch (IOException e) { throw new RuntimeException( e ); }
    }
}
2  Game Development / Newbie & Debugging Questions / Re: How to search a text file?! Please help! on: 2011-11-17 22:08:44
Really what I want to do is to have something that when it's given a username it finds that username's password...
I don't really need to separate everything out like that.
3  Game Development / Newbie & Debugging Questions / [SOLVED] How to search a text file?! Please help! on: 2011-11-17 21:46:46
I would like to search a simple text file: for instance let's see the following is the contents of my text file...

Edward Password123
Cedrick pASSWORD321

How do I parse this so that I give my scanner object a username and it finds the respective password?  Each username and password are on seperate lines all will be divided by a " " ( a space).  How do I go about doing such a thing?

Here's what I have so far...

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  
package test;

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

public class TestFileReader
{
    public void readFile(String fileName)
    {
        try
        {
            File file = new File(fileName);
            Scanner scan = new Scanner(file);
           
            while (scan.hasNextLine())
            {
                System.out.println(scan.nextLine());
            }
        } catch (FileNotFoundException e) { System.out.println("File not found."); }
    }

    public void writeToFile(String fileName, String Username, String Password)
    {
        try
        {
            FileWriter writer = new FileWriter(fileName, true);
            writer.write("\n" + Username + " " + Password);
            writer.flush();
        } catch (IOException e) {}
    }
   
    public void getPassword(String fileName, String Username)
    {
        try
        {
            File file = new File(fileName);
            Scanner scan = new Scanner(file);
           
        } catch (FileNotFoundException e) { System.out.println("File not found."); }
       
    }
}

4  Game Development / Networking & Multiplayer / Re: Trouble with Server, Client! Please help! on: 2011-11-13 17:29:53
Welp, my router allows it!  It's the D-Link Dl-524.  It's quite awesome.
5  Game Development / Networking & Multiplayer / Re: Trouble with Server, Client! Please help! on: 2011-11-13 16: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.
6  Game Development / Networking & Multiplayer / Re: Trouble with Server, Client! Please help! on: 2011-11-13 14: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?
7  Game Development / Networking & Multiplayer / Re: Trouble with Server, Client! Please help! on: 2011-11-13 04: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...
8  Game Development / Networking & Multiplayer / Re: Trouble with Server, Client! Please help! on: 2011-11-13 03: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?
9  Game Development / Networking & Multiplayer / Re: Trouble with Server, Client! Please help! on: 2011-11-13 03: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?
10  Game Development / Networking & Multiplayer / Trouble with Server, Client! Please help! on: 2011-11-13 02: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!
11  Game Development / Newbie & Debugging Questions / How to have my Progress Bar show how much of game is loaded? on: 2011-11-10 21:01:47
Hey!  I want to have my splash screen start up with a progress bar that shows how much of the game has been loaded.  Once it reaches 100%, the splash screen is replaced with my game!

How do I do this?
How do I monitor how much of the game is loaded?
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (10 views)
2014-12-27 04:03:04

TheDudeFromCI (14 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02

SHC (96 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!