我如何在该代码中添加一个线程以允许每次仅 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();
}
}
}