Results 1 to 4 of 4
- 02-19-2009, 12:14 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
TCP server-client and serialization
Hi Chaps, I'm having a problem with my TCP server.
Looking at the code below, my client never receives the serialized object back from the threaded server.
Client:
Java Code:package networkTCP; import java.io.ObjectInputStream; import java.io.Serializable; import java.net.*; import java.io.*; import java.util.ArrayList; public class tcpclient implements Serializable { public static ArrayList board = new ArrayList(); public void setupConnection() { } public static ArrayList tcpsend(String coords) throws Exception { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; //start socket, and send console text input System.out.println("pressed"); try { echoSocket = new Socket("127.0.0.1", 2323); out = new PrintWriter(echoSocket.getOutputStream(), true); //send coords to server out.println(coords); System.out.println("sent coords: " + coords); //InputStreamReader strmRead = new //InputStreamReader(echoSocket.getInputStream()); //in = new BufferedReader(strmRead); // wait for reply from server System.out.println("Still alive?"); Socket serialsocket = new Socket("127.0.0.1", 2323); System.out.println("Still alive?"); InputStream instr = serialsocket.getInputStream(); System.out.println("waiting for board object to be returned!"); ObjectInputStream ois = new ObjectInputStream(instr); ois.close(); System.out.println("board object returned"); board = (ArrayList) ois.readObject(); //out.close(); //in.close(); //serialsocket.close(); //echoSocket.close(); //Thread.sleep(1000); int x = 0; while (x != board.size()) { System.out.println(board.get(x)); x++; } x=0; } catch (UnknownHostException e) { System.err.println("Don't know about host: 127.0.0.1."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: 127.0.0.1."); System.exit(1); } return board; } public static void main (String args[]) throws Exception { ArrayList board2 = new ArrayList(); board2 = tcpsend("3,2"); } }
Server thread:
Java Code:package networkTCP; import java.net.*; import java.io.*; import java.util.ArrayList; public class tcpserverthread extends tcpserver { private Socket socket = null; public tcpserverthread(Socket socket) { //super("tcpserverthread"); this.socket = socket; } public void run() { try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); String inputLine = new String(); while((inputLine = in.readLine()) != null) { String[] splitInput = inputLine.split(","); String colStr = new String (splitInput[0]); String rowStr = new String (splitInput[1]); int row = new Integer (Integer.parseInt(rowStr)); int col = new Integer (Integer.parseInt(colStr)); ArrayList finishBoard = new ArrayList(); finishBoard = board.updateBoard(col, row); System.out.println(finishBoard); // Not sending object correctly, fix below Socket serialsocket = socket; System.out.println(serialsocket.getPort()); OutputStream oStream = serialsocket.getOutputStream(); ObjectOutputStream ooStream = new ObjectOutputStream(oStream); ooStream.writeObject(finishBoard); out.println(ooStream); System.out.println("done sending"); //ooStream.close(); inputLine = null; } } catch (IOException e) { e.printStackTrace(); } } }
Server:
Java Code:package networkTCP; import java.net.*; import java.io.*; import java.io.Serializable; import java.util.ArrayList; public class tcpserver implements Serializable { public static gameboard.board board = new gameboard.board(); public static void serverstart() throws IOException, InterruptedException { ServerSocket serverSocket = null; boolean listening = true; System.out.println("test"); try { serverSocket = new ServerSocket(2323); } catch (IOException e) { System.err.println("listen error on port: 2323."); System.exit(1); } while (listening) { new tcpserverthread(serverSocket.accept()).run(); System.out.println(serverSocket.getLocalPort()); } //serverSocket.close(); } public static void main(String[] args) throws IOException, InterruptedException { serverstart(); } }
Sorry about the length of the code..
The output from the client is:
The output from the server is:Java Code:run: pressed sent coords: 3,2 Still alive? Still alive? waiting for board object to be returned!
Java Code:run: test (correct arraylist object here!) 33161 done sending
Cheers for any help.
Pete
- 02-20-2009, 06:14 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Try adding a ooStream.flush(). It might be buffered up.
- 02-20-2009, 01:05 PM #3
I'm not a TCP server-client expert, so if I say something increadably stupid...
shouldn't you be trying to receive info back on the same client socket instance that was used to send to the server?
You used this instance to send:
but you try to receive on this socket instance:Java Code:echoSocket = new Socket("127.0.0.1", 2323); out = new PrintWriter(echoSocket.getOutputStream(), true); out.println(coords);
Also, you close an object and then try to do something with it:Java Code:Socket serialsocket = new Socket("127.0.0.1", 2323);
Luck,Java Code:ois.close(); System.out.println("board object returned"); board = (ArrayList) ois.readObject();
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-20-2009, 06:04 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
Ah I see now, I must have been very tired and low on caffeine to not notice that.
I've changed it toand it has indeed returned the game board.Java Code:InputStream instr = echoSocket.getInputStream();
Now I just need to get more than 1 client connecting to the server.... :D
Similar Threads
-
Client server
By yash in forum NetworkingReplies: 9Last Post: 01-04-2009, 04:33 PM -
client-server app
By shwein in forum NetworkingReplies: 3Last Post: 10-14-2008, 05:20 PM -
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
What is Serialization and de-serialization in Java
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:47 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks