Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  Host and Client not establishing input and Output streams  (Read 937 times)
0 Members and 1 Guest are viewing this topic.
Offline kingroka123

JGO Ninja


Medals: 41
Projects: 6
Exp: 1 year


Gamer's Helmet


« Posted 2014-01-02 04:15:46 »

Hey guys,

in my simple client hos program, my Host and Client wont create ObjectInputStreams and ObjectOutputStreams with each other.
Here is my code:

Host.java
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  
public class Host extends JFrame {
   static JFrame frame;
   private static final long serialVersionUID = 1L;
   ServerSocket server;
   ArrayList<Integer> x = new ArrayList<Integer>();
   ArrayList<Integer> y = new ArrayList<Integer>();
   public ArrayList<ObjectInputStream> clientIn = new ArrayList<ObjectInputStream>();
   public ArrayList<ObjectOutputStream> clientOut = new ArrayList<ObjectOutputStream>();
   
   int connected;
   String clientMove;
   Object play = null;

   @SuppressWarnings("deprecation")
   public Host() {
      try {

         server = new ServerSocket(2020);
         System.out.println("Created Socket...");

      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      while (true) {
         try {
            System.out.println("Waiting for Client...");
            Socket clientSocket = server.accept();
            System.out.println("Found Client...");
            connected++;
            System.out.println("Establishing Input Stream With Client...");
            clientIn.add(new ObjectInputStream(clientSocket.getInputStream()));
            System.out.println("Done!");
            System.out.println("Establishing Output Stream With Client...");
            clientOut.add(new ObjectOutputStream(clientSocket.getOutputStream()));
            System.out.println("Done!");
            try {
               System.out.println("Wating for Player...");
               try {
                  play = clientIn.get(connected).readObject();
                  System.out.println("Found Player!");
               } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         
         for (int i = 0; i < clientIn.size(); i++) {
            System.out.println("Writing Player to Clients...");
            try {
               clientOut.get(i).writeObject(play);
               clientOut.get(i).flush();
            } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
           
            System.out.println("Done!");
         }
         
      }

   }

   public static void main(String[] args) {

      frame = new JFrame("Host");
      frame.setSize(250, 250);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
      frame.setVisible(true);
      new Host();
   }

}


Client.java

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  
public class Client extends JFrame {
   private static final long serialVersionUID = 1L;
   // window dimensions
   static int width = 280;
   static int height = 280;
   static JFrame frame;
   InetAddress hostIP;
   public static ObjectInputStream hostIn;
   public static ObjectOutputStream hostOut;
   public static int connected;

   public Client(String ip) {
      while (true) {
         try {
            hostIP = InetAddress.getByName(ip);
            System.out.println("Valid IP Trying to Connect...");
            Socket hostSocket = new Socket(hostIP, 2020);
            System.out.println(hostSocket.getInetAddress());
            System.out.println("Connected!");
            System.out.println("Establishing Input Stream With Host...");
            hostIn = new ObjectInputStream(hostSocket.getInputStream());
            System.out.println("Done!");
            System.out.println("Establishing Output Stream With Host...");
            hostOut = new ObjectOutputStream(hostSocket.getOutputStream());
            System.out.println("Done!");

         } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Did Not Connect...Retrying");
            //e.printStackTrace();
         }
      }
   }

   public static void main(String args[]) {
      Game game = new Game();
      frame = new JFrame("Client");
      frame.setSize(width, height);
      frame.add(game);
     
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
      frame.setVisible(true);
      new Client("localhost");

   }

}


Thanks for any help  Grin

Edit: I forgot to mention that the client does connect to the host

Offline noblemaster

« JGO Spiffy Duke »


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #1 - Posted 2014-01-02 04:27:57 »

So, what's the outputs your are getting?

Offline kingroka123

JGO Ninja


Medals: 41
Projects: 6
Exp: 1 year


Gamer's Helmet


« Reply #2 - Posted 2014-01-02 04:30:01 »

Oh sorry I meant to add that

for Host:
1  
2  
3  
4  
Created Socket...
Waiting for Client...
Found Client...
Establishing Input Stream With Client...


for Client:
1  
2  
3  
4  
Valid IP Trying to Connect...
localhost/127.0.0.1
Connected!
Establishing Input Stream With Host...


Edit: It just stops there

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 835
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2014-01-02 07:25:28 »

The ObjectInputStream constructor (due to a clusterf**k of design mistakes) does read bytes from the InputStream it receives as a parameter. As both your server and client first create the ObjectInputStream, they will both we waiting for bytes from the other. So... create the ObjectOutputStream first (which writes bytes in its constructor), and the 'deadlock' does not occur anymore.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
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!