Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  Errors with networking, please help!  (Read 531 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2010-09-28 05:17:00 »

Okay, so I'm writing a test program to send a vector of objects from server to client.

-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  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
import java.net.*;
import java.util.Vector;
import java.io.*;

public class TestServer{
    public static void main(String[] args) throws IOException {

       Vector<Obj> obj = new Vector<Obj>();
       obj.add(new Obj(5));
       obj.add(new Obj(4));
       obj.add(new Obj(7));
       obj.add(new Obj(8));
       obj.add(new Obj(2));

        ServerSocket serverSocket = new ServerSocket(4444);
        boolean listening = true;
       
        while(listening){
           new ClientThread(serverSocket.accept(), obj).start();
        }

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

class ClientThread extends Thread implements Runnable{
   Socket acceptedSocket;
   Vector<Obj> obj;
   
   public ClientThread(Socket acceptedSocket, Vector<Obj> obj){
      super("ClientThread");
      this.acceptedSocket = acceptedSocket;
      this.obj = obj;
   }
   
   public void run(){
      try{
         Socket clientSocket = acceptedSocket;
         clientSocket.setTcpNoDelay(true);
         System.out.println("Accepted. Now creating I/O.\n");
         DataInputStream in = new DataInputStream(clientSocket.getInputStream());
           ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
           System.out.println("player connected. \n");
           out.writeObject(obj);
           out.flush();
           
           while(true){
              out.writeObject(obj);
              out.flush();
               
             sleep(15);
           }

      }
     
      catch(Exception e){
         e.printStackTrace();
         System.exit(1);
      }
       
       
   }
}

class Obj implements Serializable{
   int i;
   
   public Obj(int i){
      this.i = i;
   }
}


-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  
import java.net.*;
import java.util.Vector;
import java.io.*;

import javax.swing.JOptionPane;


public class TestClient {
   
   static Vector<Obj> obj;
   static ClientThread ct;
   
   public static void main(String[] args){
      obj = new Vector<Obj>();
      connect();
     
      for(int i = 0; i < obj.size(); i++){
         System.out.println("Obj: " + obj.get(i).i);
      }
     
      System.exit(0);
   }
   
   public static void connect(){
       String ip = JOptionPane.showInputDialog("Input server IP.");
       ct = new ClientThread(ip, obj);
        ct.start();
        ct.setPriority(Thread.MAX_PRIORITY);
    }
}

class Obj implements Serializable{
   int i;
   
   public Obj(int i){
      this.i = i;
   }
}

class ClientThread extends Thread implements Runnable{
   Socket socket;
   Vector<Obj> obj;
   DataOutputStream out;
    ObjectInputStream in;
    boolean loop = true;
   
   @SuppressWarnings("unchecked")
   public ClientThread(String ip, Vector<Obj> obj){
      super("ClientThread");
     
      try{
         socket = new Socket(ip, 4444);
         socket.setTcpNoDelay(true);
         out = new DataOutputStream(socket.getOutputStream());
           in = new ObjectInputStream(socket.getInputStream());
         this.obj = obj;
         this.obj = (Vector<Obj>) in.readObject();
      }
     
      catch(Exception e){
         e.printStackTrace();
      }
   }
   
   public void run(){
      try{
         while(loop){
            try{
               if(!socket.isClosed() && socket.isConnected()){          
                    obj = (Vector<Obj>) in.readObject();
                    this.sleep(15);
               }
               
               else
                  loop = false;
           
              }
            catch(Exception e){
                   e.printStackTrace();
                   socket.close();
             }  
         }
         
         
         
      }
     
      catch(Exception e){
         e.printStackTrace();
      }
   }
}


Port 4444 has been opened.

Getting this error:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
Accepted. Now creating I/O.

player connected.

java.net.SocketException: Connection reset by peer: socket write error
   at java.net.SocketOutputStream.socketWrite0(Native Method)
   at java.net.SocketOutputStream.socketWrite(Unknown Source)
   at java.net.SocketOutputStream.write(Unknown Source)
   at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
   at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
   at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
   at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
   at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
   at java.io.ObjectOutputStream.writeObject0(Unknown Source)
   at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
   at java.io.ObjectOutputStream.writeObject(Unknown Source)
   at ClientThread.run(TestServer.java:49)
Offline moogie

JGO Knight


Medals: 13
Projects: 6
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2010-09-28 06:48:05 »

There is nothing wrong with your networking code Smiley it is simply that your client is exiting execution immediately after creating the ClientThread thread. Put some sort of wait loop, or wait/notify after you start your client thread.
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.

SHC (24 views)
2014-11-25 12:00:59

SHC (23 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

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

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

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

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

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

kovacsa (68 views)
2014-11-07 19:57:14
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!