Results 1 to 7 of 7
- 06-27-2012, 12:45 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 32
- Rep Power
- 0
ObjectInputStream not reading my object via networking
My problem is that some how the object that i send from the server to the client does not get to the client side, or the client does not read it, String messages get read withouth a problem, but when i send a ArrayList it just does not read it. Here is my server code:
and my client:Java Code:package server2; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.EOFException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.*; import java.util.ArrayList; import javax.swing.JTextArea; import javax.swing.Timer; public class Connection implements Runnable ,ActionListener { private Socket connection; private ObjectOutputStream output; private ObjectInputStream input; private Server server; private String name; private PlayerData pd; private Timer autoSend; public Connection(Socket connection, JTextArea chatWindow, Server server) { this.connection = connection; this.server = server; this.autoSend = new Timer(50, this); } public void run() { try { setupStreams(); whileChatting(); } catch(EOFException eofEx) {} catch (IOException e) { e.printStackTrace(); closeRunning(); } finally { closeRunning(); } } //get stream to send and recieve data private void setupStreams() throws IOException { output = new ObjectOutputStream(connection.getOutputStream()); output.flush(); output.reset(); input = new ObjectInputStream(connection.getInputStream()); } //during the chat conversation public void whileChatting() throws IOException { try { name = (String) input.readObject(); this.server.addConnection(this.name, this); this.autoSend.start(); //here we make a playerdata object pd = new PlayerData(10, 10, 20, Color.green, name); } catch (ClassNotFoundException e) { e.printStackTrace(); } String message = " You are now connected! "; do { //have a conversation try { message = (String) input.readObject(); if(message.substring(0, 1).equals("m"))//we want to move the character { String values = message.substring(0); int index = values.indexOf(','); String valueX = values.substring(0, index); String valueY = values.substring(index + 1); System.out.println("X = " + valueX + "| Y = " + valueY); } } catch(ClassNotFoundException CNFEx) {} } while(!message.equals("CLIENT - END")); } //close streams and sockets after you are done chatting private void closeRunning() { try { this.server.removeConnection(this.name, this); this.autoSend.stop(); output.close(); input.close(); connection.close(); } catch(IOException ex) { ex.printStackTrace(); } } //send a message to client private void sendMessage(String message) { try { output.writeObject("SERVER - " + message); output.flush(); } catch(IOException ex) { //cant send the message } } private void sendMessage(ArrayList<PlayerData> pl) { try { output.writeObject(pl); output.flush(); System.out.println("Sending info"); } catch(IOException ex) { //cant send the message System.out.println("cant send: " + ex); } } public PlayerData getPd() { return pd; } public void setPd(PlayerData pd) { this.pd = pd; } @Override public void actionPerformed(ActionEvent arg0) { this.sendGeneralData(); } public void sendGeneralData() { ArrayList<PlayerData> pl = new ArrayList<PlayerData>(); ArrayList<Connection> cl = this.server.getConnectionList(); for(Connection c: cl) { pl.add(c.getPd()); } this.sendMessage(pl); } }
I hope you guys can help me with this.Java Code:package client2; import java.io.*; import java.net.*; import java.util.ArrayList; import javax.swing.*; @SuppressWarnings("serial") public class Client extends JFrame { private ObjectOutputStream output; private ObjectInputStream input; private String message = ""; private String serverIP; private Socket connection; private String name= ""; private ClientPanel panel; //constructor public Client(String host) { this.setTitle("Client F-IM"); this.name = JOptionPane.showInputDialog(null, "Wat is je naam?"); this.serverIP = host; this.setSize(800, 600); this.panel = new ClientPanel(); this.add(this.panel); this.setLocationRelativeTo(null); this.setVisible(true); } //connect to server public void startRunning() { try { connectToServer(); setupStreams(); whileChatting(); } catch(EOFException EOFEx) {} catch(IOException ex) { ex.printStackTrace(); } finally { closeRunning(); } } //connect to server private void connectToServer() throws IOException { connection = new Socket(InetAddress.getByName(serverIP), 6789); } //set up streams to send and recieve messages private void setupStreams() throws IOException { output = new ObjectOutputStream(connection.getOutputStream()); output.flush(); input = new ObjectInputStream(connection.getInputStream()); //send the basic data this.sendName(name); } private void sendName(String name2) { try { output.writeObject(name2); output.flush(); } catch(IOException ex) { } } //while chatting with server @SuppressWarnings("unchecked") private void whileChatting() throws IOException { //hier ontvangen we alle data this.sendData("m10,500"); do { try { //message = (String) input.readObject(); //System.out.println(message); ArrayList<PlayerData> playerList = (ArrayList<PlayerData>) input.readObject(); System.out.println("recieved player data"); this.panel.drawPlayers(playerList); } catch(ClassNotFoundException ex) { //class not found } } while(!message.equals(name + " - END")); } //close the streams and sockets private void closeRunning() { try { output.close(); input.close(); connection.close(); } catch(IOException ex) { ex.printStackTrace(); } } //send messages to server private void sendData(String message) { try { output.writeObject(message); output.flush(); } catch(IOException ex) { } } }
- 06-27-2012, 02:36 AM #2
Re: ObjectInputStream not reading my object via networking
Can you explain what happens and post any error messages?it just does not read it.
The posted code does not compile because of missing classes. Can you make a small program that compiles, executes and shows the problem?Last edited by Norm; 06-27-2012 at 02:41 AM.
If you don't understand my response, don't ignore it, ask a question.
- 06-27-2012, 09:07 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 32
- Rep Power
- 0
Re: ObjectInputStream not reading my object via networking
yeah that is the problem, there are no error :/, but i will send all of the classes
i put in a system.out.println when the client recieves the player list, but it does not show up.
the server files:
http://localhostr.com/files/8kDLVkk/ServerTest.java
http://localhostr.com/files/PeHMQlt/Server.java
http://localhostr.com/files/XzePNQu/PlayerData.java
http://localhostr.com/files/15a8YHZ/Connection.java
the client files:
http://localhostr.com/files/4P8CALY/ClientTest.java
http://localhostr.com/files/W5s34XT/Client.java
http://localhostr.com/files/SyNsdeE/ClientPanel.java
http://localhostr.com/files/f6IpyW0/PlayerData.java
- 06-27-2012, 10:06 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: ObjectInputStream not reading my object via networking
andJava Code:catch(ClassNotFoundException ex) { //class not found }
You're eating exceptions all over the place.Java Code:catch(IOException ex) { }Please do not ask for code as refusal often offends.
- 06-27-2012, 10:21 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 32
- Rep Power
- 0
Re: ObjectInputStream not reading my object via networking
that is not the problem right now, the program is not finished, but the point is that the cleint does not recieve the object
- 06-27-2012, 10:23 AM #6
Member
- Join Date
- Jul 2011
- Posts
- 32
- Rep Power
- 0
Re: ObjectInputStream not reading my object via networking
I added a System.out.println to the client class not found exception when recieving data, and it seems that the exception is thrown, this is the error
class not found: server2.PlayerData
so somehow i need to find a way to import the PlayerData class from my server application
- 06-27-2012, 11:02 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Similar Threads
-
Trouble with reading csv into object array
By shaunstruwig in forum New To JavaReplies: 1Last Post: 06-07-2012, 03:36 PM -
writing and Reading a serialized java object
By KunCode in forum New To JavaReplies: 2Last Post: 05-20-2012, 06:51 PM -
Reading object in multithreaded socket server
By dario111cro in forum NetworkingReplies: 1Last Post: 02-06-2012, 11:04 PM -
Serialization example, reading the data back into the object
By merik in forum New To JavaReplies: 3Last Post: 11-17-2010, 05:47 PM -
Reading from ObjectInputStream
By deepthought015 in forum NetworkingReplies: 8Last Post: 04-28-2009, 05:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks