Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Slick2D Client and Server test (allows unlimited users!)  (Read 4145 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2010-09-29 01:01:47 »

Here it is. the networking I've been working on.

-client-
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

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

import javax.swing.JOptionPane;

public class SlickClient extends BasicGame{
   
   ClientThread ct;
   Vector<Player> players;
   Player me;
   int ALL_KEYS = 0xFF;
   boolean keys[];
   
   public SlickClient()
    {
        super("Test Online Client - by William Starkovich");
    }
   
   public void init(GameContainer gc) throws SlickException {
      try{
         keys = new boolean[ALL_KEYS];
         
         for(int i = 0; i < ALL_KEYS; i++){
            keys[i] = false;
         }
         
         players = new Vector<Player>();
         
         connect();
      }
     
      catch(Exception e){
         
      }
   }
   
   public void connect(){
      String ip = JOptionPane.showInputDialog("Input server IP.");
       ct = new ClientThread(ip);
        ct.start();
        //ct.setPriority(Thread.MAX_PRIORITY);
       
        me = ct.me;
        players = ct.players;
    }
   
   public void update(GameContainer gc, int delta)throws SlickException{
      controls();
     
      players = new Vector<Player>();
     
      System.out.println("ct size: " + ct.players.size());
     
      me = ct.me;
        players = ct.players;
   }
   
   public void render(GameContainer gc, Graphics g) throws SlickException{
      g.setColor(Color.black);
      g.fillRect(0,0,640,480);
     
      for(int i = 0; i < players.size(); i++){
         g.setColor(Color.cyan);
         g.fillRect(players.get(i).x, players.get(i).y, 50, 50);
      }
     
      g.drawString("Players: " + players.size(), 50, 10);
   }
   
   public void keyPressed(int key, char c) {
      keys[key] = true;
   }
   
   public void keyReleased(int key, char c) {
      keys[key] = false;
   }
   
   public void controls(){
      if(keys[Input.KEY_UP]){
         me.y--;
      }
     
      else if(keys[Input.KEY_DOWN]){
         me.y++;
      }
     
      else if(keys[Input.KEY_LEFT]){
         me.x--;
      }
     
      else if(keys[Input.KEY_RIGHT]){
         me.x++;
      }
   }
   
   public static void main(String[] args) throws SlickException{
    AppGameContainer app =
      new AppGameContainer( new SlickClient() );
   
    app.setShowFPS(false);
    app.setAlwaysRender(true);
    app.setTargetFrameRate(60);
    app.setDisplayMode(800, 600, false);
    app.start();
   }
}

class ClientThread extends Thread implements Runnable{
   Socket socket;
   Vector<Player> players;
   int playerID;
   Player me;
   DataOutputStream out;
    ObjectInputStream in;
    boolean loop = true;
   
   @SuppressWarnings("unchecked")
   public ClientThread(String ip){
      super("ClientThread");
     
      try{
         players = new Vector<Player>();
         socket = new Socket(ip, 4444);
         socket.setTcpNoDelay(true);
         out = new DataOutputStream(socket.getOutputStream());
           in = new ObjectInputStream(socket.getInputStream());
         playerID = in.readInt();
         players = (Vector<Player>) in.readObject();
         
         if(players != null)
            System.out.println("Not Null: " + players.size());
         
         boolean b = false;
         for(int i = 0; i < players.size(); i++){
            if(!b){
               if(players.get(i).id == playerID){
                  me = players.get(i);
                  b = true;
               }
            }
         }
      }
     
      catch(Exception e){
         e.printStackTrace();
      }
   }
   
   public void run(){
      try{
         while(loop){
            try{
               if(!socket.isClosed() && socket.isConnected()){
                    out.writeInt(me.x);
                    out.writeInt(me.y);
                    out.flush();
                   
                    //players = new Vector<Player>();
                    players = (Vector<Player>) in.readObject();
                   
                    System.out.println("size" + players.size());
                    sleep(15);
               }
               
               else
                  loop = false;
           
              }
            catch(Exception e){
                   e.printStackTrace();
                   socket.close();
             }  
         }
         
         
         
      }
     
      catch(Exception e){
         e.printStackTrace();
      }
   }
}


-Server-
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
import java.net.*;
import java.util.Vector;
import java.io.*;
import javax.swing.*;

public class SlickServer extends JFrame{
   
   private static final long serialVersionUID = 1L;
   
   JTextArea textArea;
   JScrollPane scrollPane;
   
   public SlickServer(){
      super("Test Online Server - by William Starkovich");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(500, 100);
      setLocationRelativeTo(null);
       
      textArea = new JTextArea();
     
      scrollPane = new JScrollPane(textArea);
     
      //getContentPane().add(textArea);
      getContentPane().add(scrollPane);
   }
   
    public static void main(String[] args) throws IOException {

       Vector<Player> player = new Vector<Player>();
       
       SlickServer ss = new SlickServer();
       ss.setVisible(true);

        ServerSocket serverSocket = new ServerSocket(4444);
        //serverSocket.setSoTimeout(10000);
        boolean listening = true;
       
        int playerID = 0;
       
        while(listening){
           ss.textArea.append("Waiting to connect with player: " + playerID  + "\n");
           new ClientThread(serverSocket.accept(), player, playerID, ss.textArea).start();
           playerID++;
           ss.textArea.append("Players: " + player.size() + "\n");
        }

        serverSocket.close();
        System.exit(0);
    }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.Vector;
import java.io.*;
import javax.swing.*;

public class ClientThread extends Thread implements Runnable{
   int playerID;
   Socket acceptedSocket;
   JTextArea textArea;
   Vector<Player> players;
   Player me;
   
   public ClientThread(Socket acceptedSocket, Vector<Player> players, int playerID, JTextArea textArea){
      super("ClientThread");
      this.acceptedSocket = acceptedSocket;
      this.players = players;
      players.add(new Player(50,50, playerID));
   
      if(players != null)
         System.out.println("Not Null: " + players.size());
     
      boolean b = false;
      for(int i = 0; i < players.size(); i++){
         if(!b){
            if(players.get(i).id == playerID){
               me = players.get(i);
               b = true;
            }
         }
      }
     
      this.playerID = playerID;
      this.textArea = textArea;
   }
   
   public void removeMeAndClose(){
      boolean b = false;
      for(int i = 0; i < players.size(); i++){
         if(!b){
            if(players.get(i).id == playerID){
               players.remove(i);
               b = true;
            }
         }
      }

      try{
         acceptedSocket.close();
      }
     
      catch(Exception ee){
         ee.printStackTrace();
      }
   }
   
   public void run(){
      try{
         Socket clientSocket = acceptedSocket;
         clientSocket.setTcpNoDelay(true);
         textArea.append("Accepted. Now creating I/O.\n");
         DataInputStream in = new DataInputStream(clientSocket.getInputStream());
           ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
           textArea.append("player connected. \n");
           out.writeInt(me.id);
           out.writeObject(players);
           out.flush();
           
           while(!clientSocket.isClosed() && clientSocket.isConnected()){
              try{
                 me.x = in.readInt();
                 me.y = in.readInt();
                 
                 //if(players != null)
                  //System.out.println("Not Null: " + players.size());
                 
                 
                 textArea.append("PlayerID: " + playerID + " Players: " + players.size() + " me.x: " + me.x + " me.y: " + me.y + "\n");
                 
                 out.reset();
                 out.writeObject(players);
                 out.flush();
                   
                sleep(15);
              }
             
              catch(Exception e){
                 removeMeAndClose();
             }
           }

      }
     
      catch(Exception e){
         removeMeAndClose();
      }
       
       
   }
   
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
import java.io.Serializable;

public class Player implements Serializable{
   private static final long serialVersionUID = 1L;
   int x;
   int y;
   int id;
   
   public Player(int x, int y, int id){
      this.x = x;
      this.y = y;
      this.id = id;
   }
}


Enjoy~!
Offline mgm

Junior Newbie





« Reply #1 - Posted 2010-10-05 11:30:31 »

A base to do a Client + Server system game with Slick interest me.
but here I see only the client twice...more one class...

So I think you do a mistake when you copy this... (but I can had misunderstand how it works (if its a P2P system but if it was that you said it and no Client + Server...))
Offline CyanPrime
« Reply #2 - Posted 2010-10-18 20:49:39 »

Oops! Fixed!  Grin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (36 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!