Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.text.SimpleDateFormat;
  4. import java.util.*;
  5.  
  6. public class TCPServer1 {
  7.  
  8. private static int idConn; //id da coneccao
  9. public ArrayList<UserThread> al; // an ArrayList to keep the list of the Client
  10. private SimpleDateFormat sdf;// to System.out.println time
  11. private int porto;//porto tcp para comunicar
  12. private boolean isOn;// switch on/off para o servidor
  13. private boolean isFirst; //para saber qual servidor tcp esta up
  14.  
  15.  
  16. TCPServer1(int porto){
  17. this.porto = porto;
  18. al = new ArrayList<UserThread>();
  19. isFirst = true;
  20. start();
  21. }
  22.  
  23.  
  24.  
  25. private void start(){
  26. isOn = true;
  27. try{
  28. UDPServer udpServer = new UDPServer(7000);
  29. ServerSocket serverSocket = new ServerSocket(porto);
  30. while(isOn){
  31. System.out.println("TCP Waiting on port "+porto+"...");
  32. Socket socket = serverSocket.accept(); //aceita a coneccao
  33. if(!isOn)
  34. break;
  35. UserThread ut = new UserThread(socket);
  36. al.add(ut);
  37. ut.start();
  38. }
  39. //fecha a coneccao
  40. try{
  41. serverSocket.close();
  42. for (int i=0;i<al.size() ;i++ ) {
  43. UserThread ut = al.get(i);
  44. try{
  45. ut.in.close();
  46. ut.out.close();
  47. ut.socket.close();
  48. }
  49. catch(IOException e){e.printStackTrace();}
  50. }
  51. }catch(Exception ex){ex.printStackTrace();}
  52. }catch(IOException e){e.printStackTrace();}
  53.  
  54. }
  55.  
  56. synchronized void remove(int id){
  57. for (int i=0;i<al.size() ;i++ ) {
  58. UserThread ut = al.get(i);
  59. if(ut.id == id){
  60. al.remove(i);
  61. return;
  62. }
  63.  
  64. }
  65. }
  66.  
  67.  
  68.  
  69. private synchronized void sendToAll(String msg){
  70. System.out.println(msg);
  71. for (int i = al.size();--i >=0 ; ) {
  72. UserThread ut = al.get(i);
  73. if(!ut.writeMsg(msg)){
  74. al.remove(i);
  75. System.out.println("Removed " + ut.username);
  76. }
  77.  
  78. }
  79. }
  80.  
  81. public static void main(String[] args) {
  82. //default -> 2000, pode ser especificado outro via terminal
  83. int porto = 2000;
  84. switch(args.length) {
  85. case 1:
  86. try {
  87. porto = Integer.parseInt(args[0]);
  88. }
  89. catch(Exception e) {
  90. System.out.println("Invalid port number.");
  91. return;
  92. }
  93. case 0:
  94. break;
  95. default:
  96. System.out.println("Using default port");
  97. return;
  98.  
  99. }
  100.  
  101. TCPServer1 server = new TCPServer1(porto);
  102.  
  103.  
  104. }
  105.  
  106.  
  107. class UserThread extends Thread {
  108.  
  109. Socket socket;
  110. ObjectInputStream in;
  111. ObjectOutputStream out;
  112. // my unique id (easier for deconnection)
  113. int id;
  114. // the Username of the Client
  115. String username;
  116. // the only type of message a will receive
  117. Message cm;
  118. // the date I connect
  119. String date;
  120.  
  121. // Constructore
  122. UserThread(Socket socket) {
  123. // a unique id
  124. id = ++idConn;
  125. this.socket = socket;
  126.  
  127. try
  128. {
  129. out = new ObjectOutputStream(socket.getOutputStream());
  130. in = new ObjectInputStream(socket.getInputStream());
  131. // read the username
  132. username = (String) in.readObject();
  133. System.out.println(username + " just connected.");
  134. }
  135. catch (IOException e) {
  136. System.out.println("Exception creating new Input/output Streams: " + e);
  137. return;
  138. }
  139. // have to catch ClassNotFoundException
  140. // but I read a String, I am sure it will work
  141. catch (ClassNotFoundException e) {
  142. }
  143.  
  144. }
  145.  
  146. // what will run forever
  147. public void run() {
  148. // to loop until LOGOUT
  149. boolean isOn = true;
  150. while(isOn) {
  151. // read a String (which is an object)
  152. try {
  153. cm = (Message) in.readObject();
  154. }
  155. catch (IOException e) {
  156. System.out.println(username + " Exception reading Streams: " + e);
  157. break;
  158. }
  159. catch(ClassNotFoundException e2) {
  160. break;
  161. }
  162. // the messaage part of the Message
  163. String message = cm.getMessage();
  164.  
  165. // Switch on the type of message receive
  166. switch(cm.getType()) {
  167.  
  168. case Message.msg:
  169. sendToAll(username + ": " + message);
  170. break;
  171. case Message.logout:
  172. System.out.println(username + " disconnected with a LOGOUT message.");
  173. isOn = false;
  174. break;
  175. case Message.users:
  176. writeMsg("List of the users connected at " + sdf.format(new Date()) + "n");
  177. // scan al the users connected
  178. for(int i = 0; i < al.size(); ++i) {
  179. UserThread ct = al.get(i);
  180. writeMsg((i+1) + ") " + ct.username + " since " + ct.date);
  181. }
  182. break;
  183. }
  184. }
  185. // remove myself from the arrayList containing the list of the
  186. // connected Clients
  187. remove(id);
  188. close();
  189. }
  190.  
  191. // try to close everything
  192. private void close() {
  193. // try to close the connection
  194. try {
  195. if(out != null) out.close();
  196. }
  197. catch(Exception e) {}
  198. try {
  199. if(in != null) in.close();
  200. }
  201. catch(Exception e) {};
  202. try {
  203. if(socket != null) socket.close();
  204. }
  205. catch (Exception e) {}
  206. }
  207.  
  208. /*
  209. * Write a String to the Client output stream
  210. */
  211. private boolean writeMsg(String msg) {
  212. // if Client is still connected send the message to it
  213. if(!socket.isConnected()) {
  214. close();
  215. return false;
  216. }
  217. // write the message to the stream
  218. try {
  219. out.writeObject(msg);
  220. }
  221. // if an error occurs, do not abort just inform the user
  222. catch(IOException e) {
  223. System.out.println("Error sending message to " + username);
  224. System.out.println(e.toString());
  225. }
  226. return true;
  227. }
  228. }
  229.  
  230. }
  231.  
  232. Hope to get some help from you, I've searched similar problems but the solutions to these problems don't seem to help much.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement