Java: 2 player Network game

Started by
16 comments, last by chris_j_pook 19 years, 2 months ago
Hi, I have a basic game running in Java similar to SmashTV on the snes (top down shooter). I would like to enable 2 players to play the game on seperate computers using sockets so that both players can see each other on their screns. I have written some code that I think should work but it doesnt seem to be sending messages as I would like. from the main Game class 1 player starts a Server thread ... <CODE> import java.io.*; import java.net.*; import java.util.*; import java.text.*; public class Server extends Thread { Server() { } public void Run() { String line = "Start"; boolean serve = true; BufferedReader in ; PrintStream out ; try { ServerSocket server = new ServerSocket(80); // Open new socket on port 80. Socket client = server.accept(); // Wait for a client to connect. // Create a reader to take in messages. in = new BufferedReader(new InputStreamReader(c ient.getInputStream())); // Create an output stream to send messages back. out = new PrintStream(new BufferedOutputStream(client.getOutputStream())); while(true) { line = in.readLine(); System.out.println(line); // Print out result to test its working } } catch (IOException e) { System.out.println(e); } } public static void main(String[] args) { Server ser = new Server(); } } </CODE> ... The other player then starts a Client thread ... <CODE> import java.io.*; import java.net.*; import java.util.*; import java.text.*; import javax.swing.*; public class Client extends Thread { BufferedReader in ; PrintStream out ; public Client() { } public void Run() { Socket MyClient = null; try { MyClient = new Socket("localhost", 80); in = new BufferedReader(new InputStreamReader(MyClient.getInputStream())); // Create an output stream to send requests on. out = new PrintStream(new BufferedOutputStream(MyClient.getOutputStream())); } catch (IOException e) { System.out.println(e); } } public static void main(String[] args) { Client cli = new Client(); cli.sendMessage("Client started"); } public void sendMessage(String msg) { out.print(msg); System.out.println("Working"); } } </CODE> Surely when I call sendMessage on the client side the Server should recieve the message, however it does not. This is strange because the code works when I take it out of the game and run it alone as 2 classes. Any ideas why, am i doing this totally wrong ??? Cheers Chris
Advertisement

Hi,

Have you checked to see what is actually being sent? May be stick in the following:

public void sendMessage(String msg){     JOptionPane.showMessageDialog(null, msg);     out.print(msg);}


Just a suggestion as of now. I haven't really studied the code to much. I don't know too much about Java networking.

On another note, a good friend and I are creating a SmashTv clone ourselves using the allegro library and C langauge. Maybe you could PM me when finished? I would be interested in your finished product :)
At the moment I am just passing a string "Hello" to the sendMessage function. I think it must be something to do with the way it is running as a thread or being called constantly by the mainGame loop thats causing the problems.
Quote:Original post by chris_j_pook
At the moment I am just passing a string "Hello" to the sendMessage function. I think it must be something to do with the way it is running as a thread or being called constantly by the mainGame loop thats causing the problems.


Threading? I'd recommend putting it all in one thread/main loop for now. If it doesn't work, you've then narrowed your problem. I don't know how much code you've written, but you should be able to isolate the problem. One advantage of Java is that it's very easy to test modules of code. You're probaly going to have to get someone else to help you. I'm not to exceptional at networking (as of yet).

GCS584

[EDIT:] Multi-threading is normally more difficult to debug
have you writen another code besides that or is it your first code of the game?
means- do you already have a ready game and all you need is the socket programing or you started from the socket programming and havent no code yet?
btw i can give you my connection code, it is a chat between 2 people with no graphics, prety much same like your code but mine works :) (lol).
have you writen another code besides that or is it your first code of the game?
means- do you already have a ready game and all you need is the socket programing or you started from the socket programming and havent no code yet?
btw i can give you my connection code, it is a chat between 2 people with no graphics, prety much same like your code but mine works :) (lol).
have you writen another code besides that or is it your first code of the game?
means- do you already have a ready game and all you need is the socket programing or you started from the socket programming and havent no code yet?
btw i can give you my connection code, it is a chat between 2 people with no graphics, prety much same like your code but mine works :) (lol).
i appologize for the 3 replies but every time i tried to reply some agreement piece poped up and i thought it didnt send my reply, wont happen again.
Hi,

Yes I have a fully working game, Im just trying to add multiplayer functionality to it...

Is your code similar to mine, where have I gone wrong ?

Cheers

Chris
I think there are some problems with your threading code. First off, "public void Run()" should be "public void run()" with a lower case 'r'. Also, I don't see where you're calling the start() method. Maybe you just left that out of the example code you provided here?

There's a nice tutorial on threading in Java at: http://java.sun.com/docs/books/tutorial/essential/threads/simple.html.

I prefer to create threads by implementing the Runnable interface. You can read up on that here: http://java.sun.com/docs/books/tutorial/essential/threads/clock.html

Hope this helps.

Tony

This topic is closed to new replies.

Advertisement