Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.microedition.io.ServerSocketConnection;
- import javax.microedition.io.SocketConnection;
- import javax.microedition.io.Connector;
- public class SocketServidor extends Thread {
- public boolean serverActivo =false;
- public boolean stopServer =false;
- public int lastClient=0;
- public String IPPublic="";
- public String TestData="";
- private ServerSocketConnection server = null;
- private String APNX="";
- private String LoginX="";
- private String PasswordX="";
- private String DNSX="";
- private String portoX="";
- private int numMaxClientsX=1;
- private SocketServidorC client[]=null;
- private boolean blockX=true;
- private boolean debugX=false;
- private int timeoutSocketX=0;
- public String imeiX= null;
- public SocketServidor (String APN, String Login, String Password,
- String DNS, String porto,int numMaxClients,
- int timeoutSocket, boolean block,boolean debug, String imei)
- {
- APNX=APN;
- LoginX=Login;
- PasswordX=Password;
- DNSX=DNS;
- portoX=porto;
- numMaxClientsX=numMaxClients;
- blockX=block; //true fará com que o socket client fique bloqueado até receber dados
- timeoutSocketX=timeoutSocket; //=0 O socket nunca se fechara. >0 Se passar estes segundos sem tráfego de entrada gprs o socket fecha
- debugX=debug;
- imeiX=imei;
- client=new SocketServidorC[numMaxClientsX];
- }
- //SERVER RUNNING
- public void run()
- {
- int socketLivre=0;
- try
- {
- //O ciclo continua sempre que esta variavel seja falsa
- while (stopServer==false)
- {
- //So o servidor não estiver activo tenta-se abrir
- if (serverActivo==false)
- {
- try
- {
- //Iniciar sessão GPRS e o sockect listener
- server = (ServerSocketConnection) Connector.open("socket://:" + portoX + ";bearer_type=GPRS;access_point=" + APNX + ";username="+ LoginX + ";password=" + PasswordX + ";dns=" + DNSX+ ";timeout=0");
- serverActivo=true;
- IPPublic=server.getLocalAddress();
- //Mostrar IP Publico na saída standard de debug
- System.out.println("IP:" + IPPublic);
- }
- catch (Exception e)
- {
- if (debugX)
- {
- System.out.println("Excepcão SocketServidor run 1");
- System.out.println(e);
- }
- //No caso de erro, fecha-se o server
- closeServer();
- }
- }
- //Se já tiver o servidor activo ...
- else
- {
- try
- {
- //Nos quedamos esperando una conexión una conexión
- SocketConnection sc = (SocketConnection) server.acceptAndOpen();
- TestData=sc.getAddress();
- //Validar Address (Firewall)
- System.out.println("HOST CONNECTED:" + TestData);
- //
- //Selecciona um cliente (pode haver numMaxClientsX clientes concurrentes)
- socketLivre=selectClient(lastClient);
- lastClient=socketLivre;
- //Criar e arrancar com um socket
- client[lastClient] = new SocketServidorC(sc,timeoutSocketX,blockX,debugX,imeiX);
- client[lastClient].start();
- }
- //Se salta para a excepção fecha-se tudo
- catch (Exception e)
- {
- if (debugX)
- {
- System.out.println("Excepção no SocketServidor run 2");
- System.out.println(e);
- }
- closeServer();
- }
- }
- }
- }
- //Se salta para a excepção fecha-se tudo
- catch (Exception e)
- {
- if (debugX)
- {
- System.out.println("Excepção no SocketServidor run 3");
- System.out.println(e);
- }
- closeServer();
- }
- }
- //Método para fechar o servidor
- public void closeServer()
- {
- try
- {
- stopServer=true;
- serverActivo=false;
- server.close();
- }
- catch (Exception e)
- {
- if (debugX)
- {
- System.out.println("Excepção no SocketServidor closeServer");
- System.out.println(e);
- }
- }
- }
- //Verifica se um cliente está activo ou não
- private int selectClient(int lastClient)
- {
- int i=0;
- try
- {
- //Procura um cliente livre
- for (i=0;i<numMaxClientsX;i++)
- {
- if (isClienteActivo(client[i])==false)
- return i;
- }
- //Se não houver ninguem livre, considera-se um, forzando o encerramento do último socket
- lastClient=(lastClient+1)%numMaxClientsX;
- client[lastClient].activo=false;
- client[lastClient].interrupt();
- client[lastClient].join();
- return lastClient;
- }
- catch (Exception e)
- {
- if (debugX)
- {
- System.out.println("Excepção no SocketServidor selectClient");
- System.out.println(e);
- }
- return 0;
- }
- }
- //Verifica se um socket está activo ou não
- private boolean isClienteActivo(SocketServidorC socket)
- {
- try
- {
- if (socket.activo==true)
- return true;
- else
- return false;
- }
- catch (Exception e)
- {
- if (debugX)
- {
- System.out.println("Excepção no SocketServidor isClienteActivo");
- System.out.println(e);
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement