Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Newbie & Debugging Questions / Re: How to search a text file?! Please help!
|
on: 2011-11-18 00: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 ); } } } |
|
|
|
|
|
3
|
Game Development / Newbie & Debugging Questions / [SOLVED] How to search a text file?! Please help!
|
on: 2011-11-17 22: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."); } } } |
|
|
|
|
|
6
|
Game Development / Networking & Multiplayer / Re: Trouble with Server, Client! Please help!
|
on: 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?
|
|
|
|
|
10
|
Game Development / Networking & Multiplayer / Trouble with Server, Client! Please help!
|
on: 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!
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|