Results 1 to 4 of 4
  1. #1
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Lightbulb 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:

    Java Code:
    run:
    pressed
    sent coords: 3,2
    Still alive?
    Still alive?
    waiting for board object to be returned!
    The output from the server is:

    Java Code:
    run:
    test
     (correct arraylist object here!)
    33161
    done sending

    Cheers for any help.
    Pete

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Try adding a ooStream.flush(). It might be buffered up.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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:
    Java Code:
    echoSocket = new Socket("127.0.0.1", 2323);
    out = new PrintWriter(echoSocket.getOutputStream(), true);
    out.println(coords);
    but you try to receive on this socket instance:
    Java Code:
    Socket serialsocket = new Socket("127.0.0.1", 2323);
    Also, you close an object and then try to do something with it:
    Java Code:
    ois.close();
    System.out.println("board object returned");
    board = (ArrayList) ois.readObject();
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    You used this instance to send:
    Java Code:
    echoSocket = new Socket("127.0.0.1", 2323);
    out = new PrintWriter(echoSocket.getOutputStream(), true);
    out.println(coords);
    but you try to receive on this socket instance:
    Java Code:
    Socket serialsocket = new Socket("127.0.0.1", 2323);

    Ah I see now, I must have been very tired and low on caffeine to not notice that.
    I've changed it to
    Java Code:
    InputStream instr = echoSocket.getInputStream();
    and it has indeed returned the game board.

    Now I just need to get more than 1 client connecting to the server.... :D

Similar Threads

  1. Client server
    By yash in forum Networking
    Replies: 9
    Last Post: 01-04-2009, 05:33 PM
  2. client-server app
    By shwein in forum Networking
    Replies: 3
    Last Post: 10-14-2008, 06:20 PM
  3. Replies: 1
    Last Post: 10-05-2008, 04:12 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:47 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •