Making java online

making an online game in java

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 5045 Views - Last Post: 20 June 2008 - 09:59 AM Rate Topic: -----

#1 thabirdy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-June 08

Making java online

Post icon  Posted 19 June 2008 - 06:56 PM

I've been programming in Java for 2 years now and i have made several programs. I now want to create an game which different poeple from different computer can play. I wonder if someone had any example code or had any ideas on how to start this. As it stands now, i have started but i want to get the online basics done befor i start coding the game.
Is This A Good Question/Topic? 0
  • +

Replies To: Making java online

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Making java online

Posted 19 June 2008 - 07:26 PM

Client/server, research that.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making java online

Posted 19 June 2008 - 07:29 PM

View PostKYA, on 19 Jun, 2008 - 07:26 PM, said:

Client/server, research that.

I even wrote a Snippet about it.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Making java online

Posted 19 June 2008 - 07:32 PM

Search is a powerful tool. Let us rejoice that we know how to use it :)
Was This Post Helpful? 0
  • +
  • -

#5 thabirdy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-June 08

Re: Making java online

Posted 19 June 2008 - 07:59 PM

I understand that. And i did that, and tryed to use sockets. But i failed to use them. I believe that sockets are only for local networks. Hmm perhaps i'm wrong, if i am then can someone please tell what to put when do line
kkSocket = new Socket("name of machine on network", 4444);
i tryed to put my Ip adress but the client still couldn't connect.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making java online

Posted 19 June 2008 - 08:11 PM

View Postthabirdy, on 19 Jun, 2008 - 07:59 PM, said:

I believe that sockets are only for local networks.


No, believe it or not your are posting here using HTTP over a socket.

Check my snippet (cut & paste the code and try it on your system).

http://www.dreaminco...snippet1917.htm

Should work. "localhost" is the name of the local machine but the IP address like 192.168.1.100 will also work.
Was This Post Helpful? 0
  • +
  • -

#7 thabirdy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-June 08

Re: Making java online

Posted 19 June 2008 - 08:16 PM

Yea, but i did change the local machine name to my IP and the client still couldn't connect, i have no firewalls, but i'm on a router, is it a port forwarding issue?

Here is my code...might help...BTW its not my code....its from Java lession


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

public class KnockKnockServer {
	public static void main(String[] args) throws IOException {

		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(4444);
		} catch (IOException e) {
			System.err.println("Could not listen on port: 4444.");
			System.exit(1);
		}

		Socket clientSocket = null;
		try {
			clientSocket = serverSocket.accept();
		} catch (IOException e) {
			System.err.println("Accept failed.");
			System.exit(1);
		}

		PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
		BufferedReader in = new BufferedReader(
								new InputStreamReader(
								clientSocket.getInputStream()));
		String inputLine, outputLine;
		KnockKnockProtocol kkp = new KnockKnockProtocol();

		outputLine = kkp.processInput(null);
		out.println(outputLine);

		while ((inputLine = in.readLine()) != null) {
			 outputLine = kkp.processInput(inputLine);
			 out.println(outputLine);
			 if (outputLine.equals("Bye."))
				break;
		}
		out.close();
		in.close();
		clientSocket.close();
		serverSocket.close();
	}
}




heres the client
import java.io.*;
import java.net.*;

public class KnockKnockClient {
	public static void main(String[] args) throws IOException {

		Socket kkSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;

		try {
			kkSocket = new Socket("taranis", 4444);
			out = new PrintWriter(kkSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host: taranis.");
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for the connection to: taranis.");
			System.exit(1);
		}

		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
		String fromServer;
		String fromUser;

		while ((fromServer = in.readLine()) != null) {
			System.out.println("Server: " + fromServer);
			if (fromServer.equals("Bye."))
				break;
					
			fromUser = stdIn.readLine();
			if (fromUser != null) {
				System.out.println("Client: " + fromUser);
				out.println(fromUser);
			}
		}

		out.close();
		in.close();
		stdIn.close();
		kkSocket.close();
	}
}




and heres a other class i use

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

public class KnockKnockProtocol {
	private static final int WAITING = 0;
	private static final int SENTKNOCKKNOCK = 1;
	private static final int SENTCLUE = 2;
	private static final int ANOTHER = 3;

	private static final int NUMJOKES = 5;

	private int state = WAITING;
	private int currentJoke = 0;

	private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
	private String[] answers = { "Turnip the heat, it's cold in here!",
								 "I didn't know you could yodel!",
								 "Bless you!",
								 "Is there an owl in here?",
								 "Is there an echo in here?" };

	public String processInput(String theInput) {
		String theOutput = null;

		if (state == WAITING) {
			theOutput = "Knock! Knock!";
			state = SENTKNOCKKNOCK;
		} else if (state == SENTKNOCKKNOCK) {
			if (theInput.equalsIgnoreCase("Who's there?")) {
				theOutput = clues[currentJoke];
				state = SENTCLUE;
			} else {
				theOutput = "You're supposed to say \"Who's there?\"! " +
							"Try again. Knock! Knock!";
			}
		} else if (state == SENTCLUE) {
			if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
				theOutput = answers[currentJoke] + " Want another? (y/n)";
				state = ANOTHER;
			} else {
				theOutput = "You're supposed to say \"" + 
							clues[currentJoke] + 
							" who?\"" + 
							"! Try again. Knock! Knock!";
				state = SENTKNOCKKNOCK;
			}
		} else if (state == ANOTHER) {
			if (theInput.equalsIgnoreCase("y")) {
				theOutput = "Knock! Knock!";
				if (currentJoke == (NUMJOKES - 1))
					currentJoke = 0;
				else
					currentJoke++;
				state = SENTKNOCKKNOCK;
			} else {
				theOutput = "Bye.";
				state = WAITING;
			}
		}
		return theOutput;
	}
}



I think it most be an forwarding port issue

This post has been edited by thabirdy: 19 June 2008 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making java online

Posted 19 June 2008 - 08:25 PM

Client side:
kkSocket = new Socket("taranis", 4444);



And you have a DNS server that says that "tanaris" is your server ?
Try

kkSocket = new Socket("localhost", 4444);
just for fun :D

This post has been edited by pbl: 19 June 2008 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 thabirdy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-June 08

Re: Making java online

Posted 19 June 2008 - 08:32 PM

Sorry about that...thats code off of the website...i changed that line to "kkSocket = new Socket(192.168.2.10, 4444);"...sorry about that.

I even tryed using code from your link. But i change the server name again and the port on your code...so that line 18 (your code) is now socket = new Socket("192.168.2.10",1500);

This post has been edited by thabirdy: 19 June 2008 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making java online

Posted 19 June 2008 - 08:40 PM

View Postthabirdy, on 19 Jun, 2008 - 08:32 PM, said:

Sorry about that...thats code off of the website...i changed that line to "kkSocket = new Socket(192.168.2.10, 4444);"...sorry about that.

I even tryed using code from your link. But i change the server name again and the port on your code...so that line 18 (your code) is now socket = new Socket("192.168.2.10",1500);


Cut & pasted your code
Test it with: localhost and 192.168.1.100 which is my address
worked well with both

P.S.
Thabirdy ignore the following line it is an inline joke not for you :)
Not sure that KnockKnockProtocol is as good as FTP 1.0 from Kualam Lupur but it works.
Was This Post Helpful? 0
  • +
  • -

#11 thabirdy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-June 08

Re: Making java online

Posted 19 June 2008 - 08:46 PM

local host it broke...and when tryed your ip i got a timed out connect after it ran for abit...So...here the code i'm using...

//The client code Client.java:
 
 
import java.net.*;
import java.io.*;
 
public class Client {
 
		ObjectInputStream Sinput;				// to read the socker
		ObjectOutputStream Soutput;		// towrite on the socket
		Socket socket;
 
		// Constructor connection receiving a socket number
		Client(int port) {
				// we use "localhost" as host name, the server is on the same machine
				// but you can put the "real" server name or IP address
				try {
						socket = new Socket("192.168.1.100", 1500);
				}
				catch(Exception e) {
						System.out.println("Error connectiong to server:" + e);
						return;
				}
				System.out.println("Connection accepted " +
								socket.getInetAddress() + ":" +
								socket.getPort());
 
				/* Creating both Data Stream */
				try
				{
						Sinput  = new ObjectInputStream(socket.getInputStream());
						Soutput = new ObjectOutputStream(socket.getOutputStream());
				}
				catch (IOException e) {
						System.out.println("Exception creating new Input/output Streams: " + e);
						return;
				}
				// now that I have my connection
				String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
				// send the string to the server
				System.out.println("Client sending \"" + test + "\" to serveur");
				try {
						Soutput.writeObject(test);
						Soutput.flush();
				}
				catch(IOException e) {
						System.out.println("Error writting to the socket: " + e);
						return;
				}
				// read back the answer from the server
				String response;
				try {
						response = (String) Sinput.readObject();
						System.out.println("Read back from server: " + response);
				}
				catch(Exception e) {
						System.out.println("Problem reading back from server: " + e);
				}
				
				try{
						Sinput.close();
						Soutput.close();
				}
				catch(Exception e) {}
		}  
 
		public static void main(String[] arg) {
				new Client(1500);
		}
}



thats only when connecting to your computer...when try to connect to mine i have a friend who is running the client and its code is
//The client code Client.java:
 
 
import java.net.*;
import java.io.*;
 
public class Client {
 
		ObjectInputStream Sinput;				// to read the socker
		ObjectOutputStream Soutput;		// towrite on the socket
		Socket socket;
 
		// Constructor connection receiving a socket number
		Client(int port) {
				// we use "localhost" as host name, the server is on the same machine
				// but you can put the "real" server name or IP address
				try {
						socket = new Socket("192.168.2.10", 1500);
				}
				catch(Exception e) {
						System.out.println("Error connectiong to server:" + e);
						return;
				}
				System.out.println("Connection accepted " +
								socket.getInetAddress() + ":" +
								socket.getPort());
 
				/* Creating both Data Stream */
				try
				{
						Sinput  = new ObjectInputStream(socket.getInputStream());
						Soutput = new ObjectOutputStream(socket.getOutputStream());
				}
				catch (IOException e) {
						System.out.println("Exception creating new Input/output Streams: " + e);
						return;
				}
				// now that I have my connection
				String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
				// send the string to the server
				System.out.println("Client sending \"" + test + "\" to serveur");
				try {
						Soutput.writeObject(test);
						Soutput.flush();
				}
				catch(IOException e) {
						System.out.println("Error writting to the socket: " + e);
						return;
				}
				// read back the answer from the server
				String response;
				try {
						response = (String) Sinput.readObject();
						System.out.println("Read back from server: " + response);
				}
				catch(Exception e) {
						System.out.println("Problem reading back from server: " + e);
				}
				
				try{
						Sinput.close();
						Soutput.close();
				}
				catch(Exception e) {}
		}  
 
		public static void main(String[] arg) {
				new Client(1500);
		}
}



he gets a timed out error too
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making java online

Posted 19 June 2008 - 08:56 PM

View Postthabirdy, on 19 Jun, 2008 - 08:46 PM, said:

local host it broke...and when tryed your ip i got a timed out connect after it ran for abit...So...here the code i'm using...


Oups... a lot of stuff

OK you cannot connect to my server ... first it is not running second there are million of 192.168.x.x addresses in the world

Addresses 192.168.x.x are for local area ... there are not enough IP addresses for all computer in the world... so your provider got an address like 1.2.3.4 and all its clients have adresses in 192.168.x.x
When you make a request on the Internet the request is send form 192.168.1.44 of 1.2.3.4 and the answer is forwarded there

So go back to YOUR 192.168.2.10 (I think)

Now you have another problem... your Server is listening to port 4444
and in your client you pass the port number as parameter to your client constructor

 
public class Client {
 
		ObjectInputStream Sinput;				// to read the socker
		ObjectOutputStream Soutput;		// towrite on the socket
		Socket socket;
 
		// Constructor connection receiving a socket number
		Client(int port) {
				// we use "localhost" as host name, the server is on the same machine
				// but you can put the "real" server name or IP address
		}  
 
		public static void main(String[] arg) {
				new Client(1500);
		}
}




As far as I see in your main() method you try to connect on port 1500 which is not 4444
Was This Post Helpful? 0
  • +
  • -

#13 thabirdy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-June 08

Re: Making java online

Posted 19 June 2008 - 09:01 PM

In the code i just sent u i set port as 1500...but i get yourpoint...But not once in your code do u call client and pass in the port....so abit confused...but other thing hoq do i find my WHOLE ip adress then...?..and do i leave port as the int...?..sorry if these are stuipd questions
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making java online

Posted 19 June 2008 - 09:08 PM

View Postthabirdy, on 19 Jun, 2008 - 08:16 PM, said:

I think it most be an forwarding port issue

Wrong guess I doubt your server has a forwarding for port 4444 unless you are on really really really secured network.
Do you play on your office computer at DNA, FBI, or CIA ?
Was This Post Helpful? 0
  • +
  • -

#15 thabirdy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-June 08

Re: Making java online

Posted 19 June 2008 - 09:16 PM

How do i find my full ip then...?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2