Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: A networking quiz game idea

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default A networking quiz game idea

    Hi,

    I have created a java program which is a quiz game that simply test's the user's knowledge based on general questions.

    Now i'd like to take the program to the next level by introducing network play between two user's of the same quiz program. So I would like the program to be able to provide the choice to play a friend over the internet and be able to compete his/her friend/opponent based on the same questions presented between them on real-time network play?

    I am sure that this is very possible using the java language. Could anyone tell me the right direction to go with this?

    Thanks, very much appreciate it


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: A networking quiz game idea

    I think that there exists no better language to do network programming like java. There exist several frameworks and standard specifications in any scale.

    One of the core and basic technics are the sockets. Here are two small examples for showing the basics.

    Client Socket to reach other computer:
    try {
    	String ipAdress = "192.168.222.14";
    	int port = 8080;
    	//Create connection to other host
    	Socket sock = new Socket(ipAdress, port);
    	//Read response from host
    	InputStream in = sock.getInputStream();
    	int len;
    	byte[] b = new byte[100];
     
    	while ((len = in.read(b)) != -1) {
    		System.out.write(b, 0, len);
    	}
    	//clean up
    	in.close();
    	sock.close();
    } catch (IOException e) {
    	System.err.println(e.toString());
    }

    And the other computer cann listen in an way like this:

    try {
    	//create a listener
    	ServerSocket echod = new ServerSocket(8080);
    	//wait for connection
    	Socket socket = echod.accept();
    	// do some stuff
    	InputStream in = socket.getInputStream();
    	OutputStream out = socket.getOutputStream();
    	int c;
    	while ((c = in.read()) != -1) {
    		out.write((char) c);
    		System.out.print((char) c);
    	}
     
    	socket.close();
    	echod.close();
    } catch (IOException e) {
    	System.err.println(e.toString());			
    }

    I just enrich a small example from a tutorial, here. Maybe there are other and better libraries for P2P networking, dont know.

    And I fear you will face several troubles on the way to create such an application, like
    • simply reach the other host through the routers and the internet
    • hold on a statefull connection
    • security stuff from OS and also from java
    • synchronizing both apps


    advanced technics you can find:

  3. The Following User Says Thank You to janpiel For This Useful Post:

    dougie1809 (March 6th, 2013)

  4. #3
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: A networking quiz game idea

    Ok thanks very much for the positive reply.

    Yes I know about sockets. Would each quiz application need to have the client and server sockets's implemented in the code?

    So that if a user requested a game to another user (being the client), and then the receiving user would be the server, right?
    And it could be the other way around, so therefore each quiz application would need the client socket and the server socket implemented in the application, right?

    Thanks again

  5. #4
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: A networking quiz game idea

    I think so. I'm not a java networking expert and I dont know the best practices. Anyway here's a small example

    Client impl:
    package example.socket;
     
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    public class ClientSocketWrapper {
     
    	private Socket socket;
     
    	public ClientSocketWrapper(String ip, int port) {
    		try {
    			socket = new Socket(InetAddress.getByName(ip), port);
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public void sendData(String s) {
    		try {
    			DataOutputStream dataOut = new  DataOutputStream(socket.getOutputStream());
    			dataOut.writeUTF(s);
    			dataOut.flush();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
     
    }

    Listener:

    package example.socket;
     
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    public class ServerSocketWrapper implements Runnable {
     
    	private ServerSocket socket;
    	private Socket client;
    	private InputStream is;
     
    	public ServerSocketWrapper(int port) {
    		try {
    			socket = new ServerSocket(port);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
     
    	public void listen() {
    		try {
    			client = socket.accept();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
     
    	@Override
    	public void run() {
    		try {
    			 DataInputStream in = new DataInputStream(client.getInputStream());
    	         DataOutputStream out = new DataOutputStream(client.getOutputStream());
    	         String results = in.readUTF();
    	         System.out.println("Message from client: " + results);
     
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
     
    	}
     
    }

    One instance:

    package example.socket;
     
    public class Run {
     
    	public static void main(String[] args) {
    		try {
    			ServerSocketWrapper server = new ServerSocketWrapper(9090);
    			server.listen();
    			server.run();
    			ClientSocketWrapper client = new ClientSocketWrapper("192.168.12.68", 9090);
    			client.sendData("Fine");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }

    Other instance:

    package example.socket;
     
    public class Run {
     
    	public static void main(String[] args) {
    		try {
    			ClientSocketWrapper client = new ClientSocketWrapper("192.168.12.173", 9090);
    			client.sendData("How are you?");
    			ServerSocketWrapper server = new ServerSocketWrapper(9090);
    			server.listen();
    			server.run();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }

    Explanation:

    This is a debug example. I use two IDEs on two computers and changed the main(String[]) of the same source(with the two class definitions). The two programs only work if you debug the code step by step on both computer. If you really wanna develop a such an application you must work with different threads and event listeners.

    But think about the challenges you will face. I'm not sure if it's worth to develop it just for fun. But if it's your purpose to learn java then you will learn many many many things:
    network development
    distributed systems
    multithreading
    java memory model
    event processing
    and so on

  6. The Following User Says Thank You to janpiel For This Useful Post:

    dougie1809 (March 7th, 2013)

  7. #5
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: A networking quiz game idea

    Thanks again for the reply. yes that is a positive start. I will look into that. Is it necessarily that I would need threading, because the network play would be between only two users having separate Quiz applications? I'm not going to provide multiple users connecting more than once to the same application.

    One other question. Because this network play will be taking over the internet, what IP address should I provide in order to be able to find your opponent?
    With ports, I could just use any random port range from 1023+, so that is understandable. But not sure how they could connect over the internet?

    Thanks

  8. #6
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: A networking quiz game idea

    The program would be more convenient with different threads because the main thread stops every time you call server.accept() and stream.readUTF() Both methods stop the thread in which they are called. Test it with debugging(you can debug it with one host by using localhost as ip and change the ports of the two programs).

    Regarding the ip adresses: I fear there is no other way then to check the ip adress you have and to set it dynamically every time the program is started(DHCP). Keep also the router in mind. Use its ip-adress and check the ports he blocks.

  9. The Following User Says Thank You to janpiel For This Useful Post:

    dougie1809 (March 8th, 2013)

  10. #7
    Member
    Join Date
    Mar 2013
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: A networking quiz game idea

    Firstly I know coordinates is probably the wrong terminology? I'd assume i'd simply be passing the x and y variables, but coordinates describe it better I feel.

    Now I need to have a Server which can be accessed by 2 clients, it is a racing game and it requires each client to be able to maneuver a racecar simultaniously, each using a different control scheme but that's neither here nor there.

    I was hoping someone would be able to assist me when it came to sending the x and y positions of a racecar to the server and having the server send them onto the next player and vice versa to allow both racecars to move at the same time on each clients window. So far i've only done the simple server stuff, such as the knock knock server on the sun website, and a simple echo server which repeats a string I send to the server.

    When I tried to use int instead of string I recieved an error that the int I wanted to pass was dynamic (obviously changes with each movement) and cannot be passed as static (using readInt and writeInt).

    So any help on how to create the wanted movement on both client windows through the server would be appreciated.

    Thanks

Similar Threads

  1. [SOLVED] Logo quiz
    By big daan1234 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 15th, 2012, 10:57 AM
  2. Networking a game
    By bigtan in forum Java Networking
    Replies: 3
    Last Post: April 30th, 2012, 12:46 PM
  3. Need a good rpg game idea..
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 5
    Last Post: January 3rd, 2012, 10:44 PM
  4. Game Networking
    By Parsnips in forum Java Networking
    Replies: 15
    Last Post: December 1st, 2011, 02:57 PM
  5. Quiz application
    By JonoF in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 10th, 2010, 06:06 AM