Advertisement
Guest User

SocketServidor

a guest
Oct 14th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1.  
  2. import javax.microedition.io.ServerSocketConnection;
  3. import javax.microedition.io.SocketConnection;
  4. import javax.microedition.io.Connector;
  5.  
  6. public class SocketServidor extends Thread {
  7. public boolean serverActivo =false;
  8. public boolean stopServer =false;
  9. public int lastClient=0;
  10. public String IPPublic="";
  11.  
  12. public String TestData="";
  13.  
  14. private ServerSocketConnection server = null;
  15. private String APNX="";
  16. private String LoginX="";
  17. private String PasswordX="";
  18. private String DNSX="";
  19. private String portoX="";
  20. private int numMaxClientsX=1;
  21. private SocketServidorC client[]=null;
  22. private boolean blockX=true;
  23. private boolean debugX=false;
  24. private int timeoutSocketX=0;
  25. public String imeiX= null;
  26.  
  27.  
  28. public SocketServidor (String APN, String Login, String Password,
  29. String DNS, String porto,int numMaxClients,
  30. int timeoutSocket, boolean block,boolean debug, String imei)
  31. {
  32. APNX=APN;
  33. LoginX=Login;
  34. PasswordX=Password;
  35. DNSX=DNS;
  36. portoX=porto;
  37. numMaxClientsX=numMaxClients;
  38. blockX=block; //true fará com que o socket client fique bloqueado até receber dados
  39. timeoutSocketX=timeoutSocket; //=0 O socket nunca se fechara. >0 Se passar estes segundos sem tráfego de entrada gprs o socket fecha
  40. debugX=debug;
  41. imeiX=imei;
  42. client=new SocketServidorC[numMaxClientsX];
  43. }
  44.  
  45.  
  46. //SERVER RUNNING
  47. public void run()
  48. {
  49. int socketLivre=0;
  50.  
  51. try
  52. {
  53. //O ciclo continua sempre que esta variavel seja falsa
  54. while (stopServer==false)
  55. {
  56. //So o servidor não estiver activo tenta-se abrir
  57. if (serverActivo==false)
  58. {
  59. try
  60. {
  61. //Iniciar sessão GPRS e o sockect listener
  62. server = (ServerSocketConnection) Connector.open("socket://:" + portoX + ";bearer_type=GPRS;access_point=" + APNX + ";username="+ LoginX + ";password=" + PasswordX + ";dns=" + DNSX+ ";timeout=0");
  63. serverActivo=true;
  64. IPPublic=server.getLocalAddress();
  65. //Mostrar IP Publico na saída standard de debug
  66. System.out.println("IP:" + IPPublic);
  67.  
  68.  
  69.  
  70.  
  71. }
  72. catch (Exception e)
  73. {
  74. if (debugX)
  75. {
  76. System.out.println("Excepcão SocketServidor run 1");
  77. System.out.println(e);
  78. }
  79. //No caso de erro, fecha-se o server
  80. closeServer();
  81. }
  82. }
  83. //Se já tiver o servidor activo ...
  84. else
  85. {
  86. try
  87. {
  88. //Nos quedamos esperando una conexión una conexión
  89. SocketConnection sc = (SocketConnection) server.acceptAndOpen();
  90.  
  91. TestData=sc.getAddress();
  92. //Validar Address (Firewall)
  93. System.out.println("HOST CONNECTED:" + TestData);
  94.  
  95.  
  96. //
  97.  
  98.  
  99. //Selecciona um cliente (pode haver numMaxClientsX clientes concurrentes)
  100. socketLivre=selectClient(lastClient);
  101. lastClient=socketLivre;
  102. //Criar e arrancar com um socket
  103. client[lastClient] = new SocketServidorC(sc,timeoutSocketX,blockX,debugX,imeiX);
  104. client[lastClient].start();
  105. }
  106. //Se salta para a excepção fecha-se tudo
  107. catch (Exception e)
  108. {
  109. if (debugX)
  110. {
  111. System.out.println("Excepção no SocketServidor run 2");
  112. System.out.println(e);
  113. }
  114. closeServer();
  115. }
  116. }
  117.  
  118.  
  119. }
  120.  
  121. }
  122. //Se salta para a excepção fecha-se tudo
  123. catch (Exception e)
  124. {
  125. if (debugX)
  126. {
  127. System.out.println("Excepção no SocketServidor run 3");
  128. System.out.println(e);
  129. }
  130. closeServer();
  131. }
  132. }
  133.  
  134.  
  135.  
  136.  
  137. //Método para fechar o servidor
  138. public void closeServer()
  139. {
  140. try
  141. {
  142. stopServer=true;
  143. serverActivo=false;
  144. server.close();
  145. }
  146. catch (Exception e)
  147. {
  148. if (debugX)
  149. {
  150. System.out.println("Excepção no SocketServidor closeServer");
  151. System.out.println(e);
  152. }
  153. }
  154. }
  155.  
  156.  
  157.  
  158.  
  159. //Verifica se um cliente está activo ou não
  160. private int selectClient(int lastClient)
  161. {
  162. int i=0;
  163. try
  164. {
  165. //Procura um cliente livre
  166. for (i=0;i<numMaxClientsX;i++)
  167. {
  168. if (isClienteActivo(client[i])==false)
  169. return i;
  170. }
  171. //Se não houver ninguem livre, considera-se um, forzando o encerramento do último socket
  172. lastClient=(lastClient+1)%numMaxClientsX;
  173. client[lastClient].activo=false;
  174. client[lastClient].interrupt();
  175. client[lastClient].join();
  176.  
  177. return lastClient;
  178. }
  179. catch (Exception e)
  180. {
  181. if (debugX)
  182. {
  183. System.out.println("Excepção no SocketServidor selectClient");
  184. System.out.println(e);
  185. }
  186. return 0;
  187. }
  188. }
  189.  
  190.  
  191. //Verifica se um socket está activo ou não
  192. private boolean isClienteActivo(SocketServidorC socket)
  193. {
  194. try
  195. {
  196. if (socket.activo==true)
  197. return true;
  198. else
  199. return false;
  200. }
  201. catch (Exception e)
  202. {
  203. if (debugX)
  204. {
  205. System.out.println("Excepção no SocketServidor isClienteActivo");
  206. System.out.println(e);
  207. }
  208. return false;
  209. }
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement