Reputation: 1647
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
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();
}
}
}
Edit for the answer of user2511414
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
2) When i connect client1, maintain the confirmdialog open, and try connect the second client i recive this error in 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)
and in the server i recive this one:
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)
and the Client2 doesnt recive a "Server is busy message"
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... :(
and everything start again, im using this in CLIENT's to do the connection:
in a method to call the connection:
Requester client = new Requester();
client.run();
Class Requester:
}
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();
}
}
}
Upvotes: 1
Views: 333
Reputation:
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!
out = new ObjectOutputStream(this.connection.getOutputStream());
out.flush();
so how do you see your application run parallel?!
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.
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.
}
...
}
also you would have a simple thread tutorial here. have a good parallel program dude :)
UPDATE: 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.
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();
// }
// }
}
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.
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();
}
}
Upvotes: 2