Java 1.6 Networking My New Game

warmwaffles

Programmer
Joined
Jan 15, 2004
Messages
2,255
Location
Texas
I am currently working on a new game I call "Asteroids On Roids" :lol: its going to be a multiplayer version of asteroids but with a ton of stuff I have been wanting to add.

I have the base of the game built and runs very smoothly and now I need to start implementing network play, which aught to be a hoot :confused:

Does anyone have any idea where I can find a tutorial on how to send data via from the host "Server" to the multiple clients I plan to have? I already checked Sun's web page and that was little to no help :( .
 
Buy Networking with Java for Dummies or Java Networking Bible or one of the other 1000 page books on java networking, they should be in your local bookshop, PC shop or library.
 
Here's some sample code that I did for a simple client server test:

Code:
package alex.gw;

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

class Client
{
    public static void main(String[] args)
	throws Exception
    {
	Socket s;
	//InetSocketAddress isa;
	String string;
	OutputStream os;

	//isa=new InetSocketAddress("localhost", 5000);

	//s=new Socket("localhost", 5000);
	s=new Socket(InetAddress.getLocalHost(), 5000);

	os=s.getOutputStream();

	string="Hi there. Current time is "+(new Date());

	os.write(string.getBytes());
	os.flush();

	//Thread.sleep(10000);

	os.close();
	s.close();
    }

}



Code:
package alex.gw;

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

class Server
{
    public static void  main(String[] args)  throws Exception {
	ServerSocket ss;
	Socket s;
	InputStream is;
	int charsRead;
	BufferedReader br;
	InputStreamReader isr;
	String line;

	byte[] byteArray;

	byteArray=new byte[256];

	System.out.println("Listening to 5000.");

	ss=new ServerSocket(5000, 1, InetAddress.getLocalHost());

	//System.out.println("Binding...");
	//ss.bind(new InetSocketAddress("localhost", 5000));

	while (true) {

	    System.out.println("Accepting new connections.");
	    s=ss.accept();

	    System.out.println("Trying to get a new input stream.");
	    is=s.getInputStream();

	    isr=new InputStreamReader(is);
	    br=new BufferedReader(isr);

	    while ((line=br.readLine())!=null) {
		System.out.println(line);
	    }
	    br.close();
	    isr.close();
	    is.close();
	    s.close();
	}
    }

}

Fire up the server first. Then run the client.

To modify this sort of thing to keep the clients, you just simply store the Socket object you got in the server from a connecting client someplace. This part:

System.out.println("Accepting new connections.");
s=ss.accept();

You can modify it to something like this:

Vector<Socket> allClients;
...
System.out.println("Accepting new connections.");
s=ss.accept();
allClients.addElement(s);

Then give the Socket s to some other class to handle input and output.
 
Here's some sample code that I did for a simple client server test:

Code:
package alex.gw;

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

class Client
{
    public static void main(String[] args)
	throws Exception
    {
	Socket s;
	//InetSocketAddress isa;
	String string;
	OutputStream os;

	//isa=new InetSocketAddress("localhost", 5000);

	//s=new Socket("localhost", 5000);
	s=new Socket(InetAddress.getLocalHost(), 5000);

	os=s.getOutputStream();

	string="Hi there. Current time is "+(new Date());

	os.write(string.getBytes());
	os.flush();

	//Thread.sleep(10000);

	os.close();
	s.close();
    }

}



Code:
package alex.gw;

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

class Server
{
    public static void  main(String[] args)  throws Exception {
	ServerSocket ss;
	Socket s;
	InputStream is;
	int charsRead;
	BufferedReader br;
	InputStreamReader isr;
	String line;

	byte[] byteArray;

	byteArray=new byte[256];

	System.out.println("Listening to 5000.");

	ss=new ServerSocket(5000, 1, InetAddress.getLocalHost());

	//System.out.println("Binding...");
	//ss.bind(new InetSocketAddress("localhost", 5000));

	while (true) {

	    System.out.println("Accepting new connections.");
	    s=ss.accept();

	    System.out.println("Trying to get a new input stream.");
	    is=s.getInputStream();

	    isr=new InputStreamReader(is);
	    br=new BufferedReader(isr);

	    while ((line=br.readLine())!=null) {
		System.out.println(line);
	    }
	    br.close();
	    isr.close();
	    is.close();
	    s.close();
	}
    }

}

Fire up the server first. Then run the client.

To modify this sort of thing to keep the clients, you just simply store the Socket object you got in the server from a connecting client someplace. This part:

System.out.println("Accepting new connections.");
s=ss.accept();

You can modify it to something like this:

Vector<Socket> allClients;
...
System.out.println("Accepting new connections.");
s=ss.accept();
allClients.addElement(s);

Then give the Socket s to some other class to handle input and output.

Very nice...infact it looks much better than what me and my friend came up with, though it looks almost the same in some areas because we pretty much took what was on Sun's tutorial and screwed around with it :p
 
Back
Top Bottom