Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see your code single thread, no sign of other thread, everything is run by main thread, second I a little confused about some parts, first you flush the output just after you get it, I mean you do not send anything but flush it!</p> <pre><code>out = new ObjectOutputStream(this.connection.getOutputStream()); out.flush(); </code></pre> <p>so how do you see your application run parallel?!</p> <p>and about the running the connection process in another thread(and single), it's not a very hard job, first implement the Runnable interface, and let the run method handles the connection processing.</p> <pre><code>public class Provider implements Runnable { ... public void run(){//manages the connection but ONE at a time. while(true){ server.run(); } } public static void main(String[] arg){ Provider server = new Provider(); new Thread(this).start();//start the connection processing with another thread. } ... } </code></pre> <p>also you would have a simple <a href="http://arashmd.blogspot.com/2013/06/java-threading.html" rel="nofollow">thread tutorial here</a>. have a good parallel program dude :)</p> <p><strong>UPDATE:</strong> and for refusing the second ones (with some message) while the first one is in the process, so this is not a hard job too, but the first thing is making the process parallel, for doing this, I put(cut) the main method into another class, and let the Provider class handles the requests, also KEEP the ServerSocket(port) OEPN.</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 implements Runnable { // ServerSocket providerSocket; Socket connection = null; ObjectOutputStream out; String ocupado = "0"; ObjectInputStream in; String caminhodoarquivo; Provider(Socket s){this.connection=s;} @Override public void run() { try{ 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(); connection.close(); } } catch(IOException ioException){ioException.printStackTrace();} finally{ Server.setFree();//Tells server the system is ready for new connection. //4: Closing connection try{ in.close(); out.close(); connection.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> <p>NOTE: this one response "Error -> Server is busy" as a String when server is busy, so client needs to check if server response single string "Error -> Server is busy" it means server didn't response anything else.</p> <pre><code>class Server implements Runnable{ private final String serverBusyMessage="Error -&gt; Server is busy"; private ServerSocket ss; private static volatile boolean busy=false; public static void setFree(){busy=false;} public void run(){ while(true){ try{Socket s=ss.accept(); if(busy){//if there is one connection processing. ObjectOutputStream out=new ObjectOutputStream(s.getOutputStream()); out.writeObject(serverBusyMessage); out.flush(); s.close();continue; } System.out.println("---------------Aguardando por certidões----------------"); new Thread(new Provider(s)).start(); busy=true; }catch(Exception e){} } } private Server(){try{ss=new ServerSocket(2004);}catch(Exception ex){}} public static void main(String[] arg){ Server server = new Server(); new Thread(server).start(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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