Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Socket connection dies after some time being idle w/o firing event

843790Jun 15 2009 — edited Jun 19 2009
Hi,

I use this code to connecto my server from my Client
		if (socket == null){
			try{
				if (pwd.length > 0)
				{				
					System.out.println("opening connection...to "+host+" at port "+port);
					socket = new Socket(host, port);		
					toServer = new ObjectOutputStream(socket.getOutputStream());
					fromServer = new ObjectInputStream(socket.getInputStream());
					System.out.println("Connection established!");
					fireClientEvent(new ClientEvent(this, ClientEvent.CONNECTION_ESTABLISHED, ""));
					this.connected = true;
					Hashtable<String, String> table = new Hashtable<String, String>();
					table.put("action", "login");
					table.put("key", convertToString(pwd));
					sendObject(table);
				}
			}catch(IOException e){
				try{
					if (socket != null)
						socket.close();
					System.out.println("Error while connecting... Server does not exist!");
					this.connected = false;					
					fireClientEvent(new ClientEvent(this, ClientEvent.SERVER_DOES_NOT_EXIST, getHost()+":"+getPort()));
				}catch(IOException x){
					System.out.println("I/O ERROR @ CLIENT CONNECT!");
					removeAllListeners();
					x.printStackTrace();
				}
			}
		}
And this is how my server responds to client requests:
	public void run()
	{
		ServerSocket server = null;
		Socket socket 		= null;
		Hashtable<String, String> ClientInfo = new Hashtable<String, String>();  

		try{
			server = new ServerSocket(getPort());
			//listen for connections
			while (!stopServer)
			{
				try{
					System.out.println("Starting Server at port: "+getPort());
					socket = server.accept();
					ClientInfo.put("remoteip", socket.getInetAddress().toString());
					fireServerEvent(new ServerEvent(ClientInfo, ServerEvent.CONNECTION_REQUESTED, null));
					ThreadedSocket tsocket = getThreadedSocket(socket);
					tsocket.addSocketListener(this);
					tsocket.setMaxSolvingTime(maxSolvingTime);
					Sockets.add(tsocket);
					executor.execute(tsocket);
					fireServerEvent(new ServerEvent(ClientInfo, ServerEvent.CONNECTION_ESTABLISHED, null));
				}catch (IOException ioe){
					fireServerEvent(new ServerEvent(ClientInfo, ServerEvent.UNEXPECTED_CONNECTION_ERROR, null));
					exceptionThrown(ioe);
				}
			}
		}catch(Exception x){
			exceptionThrown(x);
		}finally{
			try{
				server.close();
				socket.close();
			}catch (Exception e){
				exceptionThrown(e);
			}
		}		
	}
It seems that after some time (aprox. 10-15 minutes) being idle, my client gets disconnected withougt firing any event so the Server thinks the client is still connected but it does not get any messages because the client has closed the connection (I think)

Am I doing/thinking something wrong?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 17 2009
Added on Jun 15 2009
17 comments
1,282 views