Java: 2 player Network game

Started by
16 comments, last by chris_j_pook 19 years, 2 months ago
Hi,

I call the relevent start method from the main Game class when the player chooses to start a server or join one.

I have changed the Run() to run() and made it implement Runnable but still no joy...

What is strange is the way it works when I take this code and run it away from the game part as single classes rather than threads. Is there some sort of waiting or something I need to do ?

Chris
Advertisement
I've written a few sample programs for my class (I'm also a teacher). They're at: http://www.parisisd.net/compsci/examples.php. Scroll down to Sockets / Threads.

I'm going to copy your code and try to run it myself. Then maybe I can figure out what's up.

Tony
Here are your two programs with all of the threading stuff removed and a few more changes. These work as expected for me.

import java.io.*;import java.net.*;public class Server {	public static void main(String[] args) {		try {			ServerSocket server = new ServerSocket( 8080 );						// Wait for a client to connect.			Socket socket = server.accept();			System.out.println( "Client connected..." );						// Create a reader to take in messages.			BufferedReader in = new BufferedReader(				new InputStreamReader( socket.getInputStream() ) );			// wait for data to appear			while( !in.ready() ) {				// do nothing			}			String line = in.readLine();			// Print out result to test its working			System.out.println( line );			socket.close();		}		catch (IOException e) {			System.out.println( e );		}	}}


import java.io.*;import java.net.*;public class Client {	public static void main(String[] args) {		try {			Socket socket = new Socket( "localhost", 8080 );			System.out.println( "Socket created..." );			// Create an output stream to send requests on.			PrintWriter out = new PrintWriter( 				new BufferedOutputStream( socket.getOutputStream() ) );			out.print( "Hello World!" );			out.flush();			System.out.println( "Data sent." );			socket.close();		}		catch( IOException e ) {			System.out.println( e );		}	}}


Basically, I added some code to the Server to wait for data to appear on the input stream, and I added out.flush() to the Client to make sure that the data gets sent.

Tony
Ok I have put this code in to my game now...

At the start of the game either createServer() or createClient() is called, these contain the code you posted above.

This does work, in that when the Client socket is created it sends a message and the server game prints out Hello World...

However, if I then try to send more messages either using out.print("...") in Game class (I have PrintStream out as a global variable in the Game class) or by calling createClient() again the Server does not seem to receive the message.

I cant think why this would be ...

Any ideas ?

Chris
Note that the server code I provided earlier closes the socket after printing one line of text and then exits. Here's a slightly more advanced server that should handle multiple lines of text on the same socket and will accept another connection after the first one is closed:

import java.io.*;import java.net.*;public class Server {	public static void main(String[] args) {		try {			ServerSocket server = new ServerSocket( 8080 );			// this server will live forever			while ( true ) {							// Wait for a client to connect.				Socket socket = server.accept();				System.out.println( "Client connected..." );							// Create a reader to take in messages.				BufferedReader in = new BufferedReader(					new InputStreamReader( socket.getInputStream() ) );					// wait for data to appear				while( !in.ready() ) { /* do nothing */ }					// read multiple lines				while( true ) {					String line = in.readLine();					if ( line == null ) {						break;					}					System.out.println( line );				}				// close the socket				socket.close();				System.out.println( "Connection closed." );			}		}		catch( Exception e ) {			System.out.println( e );		}	}}


Tony
Ahhhh,

Thankyou... I should have realised the server was only taking one message and closing, I now have my game server and client communicating once per game loop.

Should be no problem to pass game variables between them now.

Thanks for your help

Chris
Have you got a download link for a compiled version? A couple of us did an alpha version of a smashtv clone: http://grexengine.com/sections/games/survivor/ which we mean to finish off sometime (including 8-way or more multiplayer) but haven't got around to yet.

If anyone wants to work on this feel free to get in touch on ceo at grexengine.com (put "survivor" in the subject line and it'll get forwarded on to us).
Yes I have a compiled version I could send you, what is your email address ?

This topic is closed to new replies.

Advertisement