Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd a thread to handle only 1 socket per time
    primarykey
    data
    text
    <p>How can i add a Thread in that code to allow only 1 connection per Time... and when it finish .close(); open again to wait another? i tryed a lot of others things, like WHILE, if, and others variables, but failed to block the .accept(); while a connection is open ;/, and i dont know how to make it one threaded</p> <pre><code>import java.awt.Desktop; import java.io.*; import java.lang.reflect.Array; import java.net.*; import java.util.ArrayList; import javax.swing.JOptionPane; import javax.swing.JTextField; public class Provider { ServerSocket providerSocket; Socket connection = null; ObjectOutputStream out; String ocupado = "0"; ObjectInputStream in; String caminhodoarquivo; Provider(){} void run() { try{ providerSocket = new ServerSocket(2004, 1); System.out.println("---------------Aguardando por certidões----------------"); connection = providerSocket.accept(); out = new ObjectOutputStream(this.connection.getOutputStream()); out.flush(); sendMessage(ocupado); in = new ObjectInputStream(connection.getInputStream()); System.out.println("Certidão de: " + connection.getInetAddress().getHostName()); String ocupado = "1"; try{ caminhodoarquivo = (String)in.readObject(); System.out.println("Certidão: " + caminhodoarquivo); JTextField paginainicial = new JTextField(); JTextField paginafinal = new JTextField(); Object[] message = { "Número da Primeira Folha: ", paginainicial, "Número Última Folha: ", paginafinal, }; int option = JOptionPane.showConfirmDialog(null, message, "Dados da Certidão", JOptionPane.OK_CANCEL_OPTION); ocupado = "1"; if (option == JOptionPane.OK_OPTION) { String primeirafolha = paginainicial.getText(); String ultimafolha = paginafinal.getText(); metodos metodosBD = new metodos(); metodosBD.atualizafolha(primeirafolha, ultimafolha, caminhodoarquivo); System.out.println("Dados inseridos."); Desktop.getDesktop().print(new File(caminhodoarquivo)); ocupado = "0"; } else { System.out.println("Certidão Cancelada."); } } catch(ClassNotFoundException classnot){ System.err.println("Data received in unknown format"); out.close(); in.close(); providerSocket.close(); } } catch(IOException ioException){ ioException.printStackTrace(); } finally{ //4: Closing connection try{ in.close(); out.close(); providerSocket.close(); } catch(IOException ioException){ ioException.printStackTrace(); } } } void sendMessage(String msg) { try{ out.writeObject(msg); out.flush(); } catch(IOException ioException){ ioException.printStackTrace(); } } public static void main(String args[]) { Provider server = new Provider(); while(true){ server.run(); } } } </code></pre> <blockquote> <p>Edit for the answer of user2511414</p> </blockquote> <p>1) Server is running ok and recieve perfectly the first connection and finish it when i close the confirmdialog, and i can open another perfectly = OK</p> <p>2) When i connect client1, maintain the confirmdialog open, and try connect the second client i recive this error in CLIENT2:</p> <pre><code>java.net.SocketException: Software caused connection abort: recv failed at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source) at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source) at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Sour ce) at java.io.ObjectInputStream.readStreamHeader(Unknown Source) at java.io.ObjectInputStream.&lt;init&gt;(Unknown Source) at certidoesOrganizado.PrimeiroPDF$Requester.run(PrimeiroPDF.java:151) at certidoesOrganizado.PrimeiroPDF.enviacaminho(PrimeiroPDF.java:132) at certidoesOrganizado.PrimeiroPDF.geracertidao(PrimeiroPDF.java:466) at certidoesOrganizado.PrimeiroPDF.pegadados(PrimeiroPDF.java:302) at certidoesOrganizado.gui$6.actionPerformed(gui.java:204) </code></pre> <p>and in the server i recive this one:</p> <pre><code>java.net.SocketException: Socket is closed at java.net.Socket.getOutputStream(Unknown Source) at Provider.run(Provider.java:21) at java.lang.Thread.run(Unknown Source) Exception in thread "Thread-4" java.lang.NullPointerException at Provider.run(Provider.java:59) at java.lang.Thread.run(Unknown Source) </code></pre> <p>and the Client2 doesnt recive a "Server is busy message"</p> <p>3) While the client1 ConfirmDialog is still open and after the fail of Client2 i try the CLIENT3, it connect succefully and i end with 2 Joption.ShowConfirmDialog opened... :(</p> <p>and everything start again, im using this in CLIENT's to do the connection:</p> <p>in a method to call the connection: </p> <pre><code>Requester client = new Requester(); client.run(); </code></pre> <p>Class Requester:</p> <pre><code>} class Requester{ Socket requestSock et; ObjectOutputStream out; ObjectInputStream in; String message; Requester(){} void run() { try{ requestSocket = new Socket("localhost", 2004); System.out.println("Tentando Conexão"); out = new ObjectOutputStream(requestSocket.getOutputStream()); out.flush(); System.out.println("out.flush"); in = new ObjectInputStream(requestSocket.getInputStream()); try { message = (String)in.readObject(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } do{ message = "oi"; sendMessage(arquivonomecompleto); }while(message.equals("bye")); } catch(UnknownHostException unknownHost){ System.err.println("You are trying to connect to an unknown host!"); } catch(IOException ioException){ ioException.printStackTrace(); } finally{ //4: Closing connection try{ in.close(); out.close(); requestSocket.close(); } catch(IOException ioException){ ioException.printStackTrace(); } } } void sendMessage(String msg) { try{ out.writeObject(msg); out.flush(); //System.out.println("client&gt;" + msg); } catch(IOException ioException){ ioException.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload