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

    Default Keep my TCP connection alive

    Hi again,

    I'm having trouble keeping my TCP connection between my server and client alive.

    Please take a look at my code and tell me what I am doing wrong:


    TCP CLIENT

    Java Code:
    package networkTCP;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.PrintWriter;
    import java.io.Serializable;
    import java.net.*;
    import java.util.ArrayList;
    
    public class tcpclient implements Serializable
    {
    	public Socket echoSocket = null;
    	public ArrayList board = new ArrayList();
    
    	public void setupConnection() throws Exception
    	{
    		// keep connection alive here
    		echoSocket = new Socket("127.0.0.1", 2323);
    	}
    	public 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
    		{
    			//temp
    			//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 = echoSocket.getInputStream();
    			System.out.println("waiting for board object to be returned!");
    			ObjectInputStream ois = new ObjectInputStream(instr);
    			System.out.println("board object returned");
    			board = (ArrayList) ois.readObject();
    			ois.close();
    			//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
    	{
    		tcpclient client = new tcpclient();
    		client.setupConnection();
    
    		client.tcpsend("0,1");
    		client.tcpsend("1,0");
    	}
    }



    TCP SERVER
    Java Code:
    package networkTCP;
    import java.net.*;
    import java.io.*;
    import java.io.Serializable;
    
    public class tcpserver implements Serializable
    {
    	// create gameboard object
    	public static gameboard.board board = new gameboard.board();
    
    	public static void serverstart() throws IOException,
    		InterruptedException
    	{
    		//what is setTCPNoDelay(); ??
    		System.out.println("Hi, I'm the TCP server!");
    		ServerSocket serverSocket = null;
    		boolean listening = true;
    		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();
    		}
    		//serverSocket.close();
    	}
    
    	public static void main(String[] args) throws IOException,
    		InterruptedException {
    		serverstart();
    	}
    }



    TCP SERVER-THREAD

    Java Code:
    package networkTCP;
    
    import java.net.*;
    import java.io.*;
    import java.util.ArrayList;
    
    public class tcpserverthread extends tcpserver
    {
    	//global socket
    	private Socket clientSocket = null;
    
    	public tcpserverthread(Socket socket)
    	{
    		//super("tcpserverthread");
    		this.clientSocket = socket;
    	}
    	public void run()
    	{
    		try {
    
    			PrintWriter out = new PrintWriter(clientSocket.getOutputStream
    				(), true);
    			BufferedReader in = new BufferedReader(
    				new InputStreamReader(
    				clientSocket.getInputStream()));
    			String inputLine = new String();
    			System.out.println(clientSocket.getPort());
    			OutputStream oStream = clientSocket.
    				getOutputStream();
    			ObjectOutputStream ooStream = new
    			ObjectOutputStream (oStream);
    
    			inputLine = in.readLine();
    			while(true)
    			{
    				System.out.println("in1" + inputLine);
    				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);
    				ooStream.writeObject(finishBoard);
    				out.println(ooStream);
    				System.out.println("done sending");
    				//ooStream.close();
    				System.out.println("in2" + inputLine);
    				//inputLine = null;
    				inputLine = in.readLine();
    			}
    		}
    
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
        }
    }




    These are the errors:
    Server:
    Java Code:
    run:
    Hi, I'm the TCP server!
    58554
    in10,1
    (correct arraylist display was here)
    done sending
    in20,1
    java.net.SocketException: Connection reset
            at java.net.SocketInputStream.read(SocketInputStream.java:168)
            at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
            at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
            at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
            at java.io.InputStreamReader.read(InputStreamReader.java:167)
            at java.io.BufferedReader.fill(BufferedReader.java:136)
            at java.io.BufferedReader.readLine(BufferedReader.java:299)
            at java.io.BufferedReader.readLine(BufferedReader.java:362)
            at networkTCP.tcpserverthread.run(tcpserverthread.java:53)
            at networkTCP.tcpserver.serverstart(tcpserver.java:26)
            at networkTCP.tcpserver.main(tcpserver.java:33)
    BUILD STOPPED (total time: 12 seconds)


    TCP CLIENT:
    Java Code:
    run:
    pressed
    sent coords: 0,1
    Still alive?
    Still alive?
    waiting for board object to be returned!
    board object returned
    pressed
    Couldn't get I/O for the connection to: 127.0.0.1.
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)



    any help is greatly appreciated.

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Why are you using localhost (127.0.0.1) on both sides? This makes zero sense.

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

    Default

    I'm not anymore
    Java Code:
    public void setupConnection(String IPAddy2) throws Exception
    	{
    		InetAddress IPAddy = InetAddress.getByName(IPAddy2);
    		// keep connection alive here
    		echoSocket = new Socket(IPAddy, 2323);
    }
    I have to call
    Java Code:
    setupConnection("10.0.0.1");
    every time i need to send something

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Try debugging the code on one side, and then the other. See what looks funny.

    I'm guessing the client closes the connection after the first pass, and the server gets an error message.

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

    Default

    This is a definite problem, though not necessarily the only one.

    Java Code:
    ois.close();
    When you close the ObjectInputStream, it closes it's underlying resources as well, which includes your Socket's InputStream.

    If you are planning to reuse the same underlying InputStream, you do not need to close any streams wrapped around it (such as ObjectInputStream). Just let them go out of scope.

Similar Threads

  1. connection problem
    By subash in forum JDBC
    Replies: 5
    Last Post: 04-22-2008, 09:17 AM
  2. How to configure "keep alive" in Sockets?
    By rajdotme in forum Networking
    Replies: 1
    Last Post: 04-16-2008, 11:36 PM
  3. no connection
    By even in forum JDBC
    Replies: 15
    Last Post: 01-02-2008, 01:50 PM
  4. JDBC connection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 07:39 PM
  5. connection pool for db2
    By paty in forum JDBC
    Replies: 1
    Last Post: 08-06-2007, 02:43 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
  •