3 Replies - 294 Views - Last Post: 19 May 2014 - 04:56 PM Rate Topic: -----

#1 MarvinRio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 18-October 13

Java server disconnects inactive users after 5 minutes

Posted 19 May 2014 - 05:45 AM

I'm using this methodology for Java connection between client and server, I copied it from the internet, and helped me a lot, but the server allows the user to stay connected while it is sending messages, but if you stay an average time of 5 minutes without send anything it is disconnected, I need to increase that time to about 30 minutes or disable this disconnection for inactivity, anything you know to help me a lot.

I'm Brazilian,the code have some parts in Portuguese ok ? And forgive my bad english.

Stay in Peace
thank you very much

Settings:
Netbeans 8.0
Windows Seven 64 Bits Ultimante


The following code:

package redes;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChatCliente extends JFrame {  
	
	JTextField textoParaEnviar;
	PrintWriter escritor;
	Socket socket;
	String nome;
	JTextArea textoRecebido;
	Scanner leitor;
	
	
 private class EscutaServidor implements Runnable{
     
	 public void run() {
		 try{
		 String texto;
		 while((texto=leitor.nextLine()) != null){
			 textoRecebido.append(texto+"\n");
		 }
		 }catch(Exception x){}
		
	}
	 
 }
 
 
 
	public ChatCliente(){
		
		super("Chat");
	    Font fonte = new Font("Sefif", Font.PLAIN,26);
        textoParaEnviar = new JTextField();
		textoParaEnviar.setFont(fonte);
		JButton botao = new JButton("Enviar");
		botao.setFont(fonte);
		botao.addActionListener(new EnviarListener());
		
		Container envio = new JPanel();
		envio.setLayout(new BorderLayout());
		envio.add(BorderLayout.CENTER, textoParaEnviar);
		envio.add(BorderLayout.EAST, botao);
		getContentPane().add(BorderLayout.SOUTH,envio);
		
		textoRecebido= new JTextArea();
		textoRecebido.setFont(fonte);
		JScrollPane scroll= new JScrollPane(textoRecebido);
		getContentPane().add(BorderLayout.CENTER, scroll);
		
		
	    configurarRede();
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,500);
		setVisible(true);
		      }
	
	private class EnviarListener implements ActionListener{
        @Override
		public void actionPerformed(ActionEvent e) {
		escritor.println(nome+" : "+textoParaEnviar.getText());
		escritor.flush();
		textoParaEnviar.setText("");
		textoParaEnviar.requestFocus();
	}
   }
	
	
	private void configurarRede(){
		try{
        socket = new Socket("127.0.0.1",5000);
	    escritor = new PrintWriter(socket.getOutputStream());
	    leitor=new Scanner(socket.getInputStream());
	    new Thread(new EscutaServidor()).start();
		}catch(Exception e){}
		}

	
	public static void main(String[] args) {
	new ChatCliente();
		}

	
}





---------------------------------------------------------------------------------------------------------------------




package redes;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ChatServer {
	List<PrintWriter> escritores =  new ArrayList<>();
	
	public ChatServer(){
			 
    class EscutaCliente implements Runnable{
			
		     Scanner leitor;
             public EscutaCliente(Socket socket){
            	 try{
                    leitor =  new Scanner (socket.getInputStream());
            	 }catch(Exception e){
            		
            	 }
             }
			
             
             void encaminharParaTodos(String texto){
 				for(PrintWriter w: escritores){
 					try{
 					w.println(texto);
 					w.flush();
 				} catch(Exception x){}
 			}
 			}
 	         
             
             public void run() {
				try{
			    	String texto;
				    while((texto=leitor.nextLine()) != null){
					System.out.println(texto);
					encaminharParaTodos(texto);
		    		}
		 		}catch(Exception x){}
				
			}
		}
		
		
 
		ServerSocket server;
		try {
			server = new ServerSocket(5000);
			while(true){
			Socket socket = server.accept();
			new Thread(new EscutaCliente(socket)).start();
			PrintWriter p = new PrintWriter(socket.getOutputStream());
			escritores.add(p);
			}
			
		} catch (IOException e) {}
		
		
		
}
		
	public static void main(String[] args) {
		  ChatServer c= new ChatServer();
			}
		

}
	
	



------------------------------------------------------------------------------------------------------------------------



package redes;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ConselhoCliente {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException, IOException {
		//Socket conexão de rede
		Socket socket = new Socket("127.0.0.1",5000); 
	Scanner s = new Scanner (socket.getInputStream());
	System.out.println("Mensagem"+s.nextLine());
	s.close();

	}

}






------------------------------------------------------------------------------------------------------------------------


package redes;

import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ConselhoServidor {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		ServerSocket server = new ServerSocket(5000);
		
		//aguarda chegada de novos clientes
		while(true){
			Socket socket=server.accept();
			try (PrintWriter w = new PrintWriter(socket.getOutputStream())){
			w.println("Aprenda Java");
			}
		}

	}

}






Is This A Good Question/Topic? 0
  • +

Replies To: Java server disconnects inactive users after 5 minutes

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Java server disconnects inactive users after 5 minutes

Posted 19 May 2014 - 09:20 AM

It's not that java code causing the problem. That's a synchronous connection via socket. Something else on your end is the problem.
Was This Post Helpful? 0
  • +
  • -

#3 ForteGS  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 21
  • Joined: 06-January 14

Re: Java server disconnects inactive users after 5 minutes

Posted 19 May 2014 - 09:24 AM

You might find it helpful to read the Java docs for Socket and ServerSocket classes.

http://download.java.net/jdk7/archive/b123/docs/api/java/net/ServerSocket.html#setPerformancePreferences%28int,%20int,%20int%29

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,438
  • Joined: 20-September 08

Re: Java server disconnects inactive users after 5 minutes

Posted 19 May 2014 - 04:56 PM

Quote

but the server allows the user to stay connected while it is sending messages,

There's some confusion there: the 'user' (client) does not stay connected. On the contrary, it seems to retrieve a message from the server then immediately disconnects itself

Quote

        //Socket conexão de rede
        Socket socket = new Socket("127.0.0.1", 5000);
        Scanner s = new Scanner(socket.getInputStream());
        System.out.println("Mensagem" + s.nextLine());
        s.close();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1