1

我如何在该代码中添加一个线程以允许每次仅 1 个连接...以及何时完成 .close(); 再次打开等待另一个?我尝试了很多其他的东西,比如 WHILE、if 和其他变量,但未能阻止 .accept(); 当连接打开时;/,我不知道如何使它成为一个线程

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();
        }
    }

}

编辑 user2511414 的答案

1)服务器运行正常并完美接收第一个连接并在我关闭确认对话框时完成它,我可以完美打开另一个= OK

2)当我连接client1时,保持confirmdialog打开,并尝试连接第二个客户端我在CLIENT2中收到此错误:

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.<init>(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)

在服务器中,我收到了这个:

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)

并且 Client2 没有收到“服务器正忙消息”

3)虽然client1 ConfirmDialog 仍然打开并且在Client2 失败后我尝试CLIENT3,它成功连接并且我以2 Joption.ShowConfirmDialog 打开... :(

一切重新开始,我在 CLIENT 中使用它来进行连接:

在调用连接的方法中:

Requester client = new Requester();

client.run();

课程请求者:

}
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>" + msg);
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }




}
4

1 回答 1

2

我看到你的代码单线程,没有其他线程的迹象,一切都由主线程运行,其次我对某些部分有点困惑,首先你在得到它之后刷新输出,我的意思是你什么都不发送,而是刷新它!

out = new ObjectOutputStream(this.connection.getOutputStream());
out.flush();

那么您如何看待您的应用程序并行运行?

而关于在另一个线程(和单线程)中运行连接过程,这不是一件很困难的工作,首先实现Runnable接口,让run方法处理连接处理。

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.
  }
  ...
}

你也可以在这里获得一个简单的线程教程。有一个很好的并行程序老兄:)

更新:并且为了在第一个正在处理中时拒绝第二个(带有一些消息),所以这也不是一项艰巨的工作,但第一件事是使过程并行,为此,我放(切)将main方法放入另一个类,让Provider类处理请求,同时保持ServerSocket(port) OEPN。

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();
//        }
//    }

}

注意:当服务器忙时,这个响应“错误->服务器忙”作为字符串,因此客户端需要检查服务器响应单个字符串“错误->服务器忙”这意味着服务器没有响应其他任何内容。

class Server implements Runnable{
  private final String serverBusyMessage="Error -> 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();
  }
}
于 2013-10-04T18:39:33.600 回答