Guest User

Untitled

a guest
Jun 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. // Copyright 2003 Nokia Corporation.
  2. //
  3. // THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
  4. // EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
  5. // FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
  6. // OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
  7. // ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
  8. // OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
  9. // SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
  10. // RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
  11. // OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
  12. // BY THIRD PARTIES
  13. //
  14. // Furthermore, information provided in this source code is preliminary,
  15. // and may be changed substantially prior to final release. Nokia Corporation
  16. // retains the right to make changes to this source code at
  17. // any time, without notice. This source code is provided for informational
  18. // purposes only.
  19. //
  20. // Nokia and Nokia Connecting People are registered trademarks of Nokia
  21. // Corporation.
  22. // Java and all Java-based marks are trademarks or registered trademarks of
  23. // Sun Microsystems, Inc.
  24. // Other product and company names mentioned herein may be trademarks or
  25. // trade names of their respective owners.
  26. //
  27. // A non-exclusive, non-transferable, worldwide, limited license is hereby
  28. // granted to the Licensee to download, print, reproduce and modify the
  29. // source code. The licensee has the right to market, sell, distribute and
  30. // make available the source code in original or modified form only when
  31. // incorporated into the programs developed by the Licensee. No other
  32. // license, express or implied, by estoppel or otherwise, to any other
  33. // intellectual property rights is granted herein.
  34.  
  35.  
  36. // unnamed package
  37.  
  38. import java.io.*;
  39. import java.net.*;
  40. import java.util.Vector;
  41.  
  42. // This is a very simple server intended for simple testing only
  43.  
  44. public class Server
  45. extends Thread
  46. {
  47. private static final int BUFFER_SIZE = 65536;
  48. private static boolean quietMode = false;
  49. private static boolean useNagle = false;
  50. private final Socket socket;
  51. private final InetAddress remoteAddress;
  52. private final int remotePort;
  53. private final InputStream in;
  54. private final OutputStream out;
  55. private static Vector<Server> activeThreads = new Vector<Server>();
  56.  
  57.  
  58. public static void main(String[] args)
  59. {
  60.  
  61. int localPort = 2000;
  62.  
  63. try
  64. {
  65. ServerSocket serverSocket = new ServerSocket(localPort);
  66. while (true)
  67. {
  68. log("Entering accept:");
  69. Socket socket = serverSocket.accept();
  70. if (!useNagle)
  71. {
  72. socket.setTcpNoDelay(true);
  73. }
  74. log("Connection opened");
  75.  
  76. Server conn = new Server(socket);
  77. conn.start();
  78. activeThreads.add(conn);
  79. }
  80. }
  81. catch (IOException ex)
  82. {
  83. System.err.println("I/O Exception: " + ex.getMessage());
  84. ex.printStackTrace(System.err);
  85. System.exit(1);
  86. }
  87.  
  88. System.exit(0);
  89. }
  90.  
  91.  
  92. private Server(Socket socket)
  93. throws IOException
  94. {
  95. this.socket = socket;
  96. remoteAddress = socket.getInetAddress();
  97. remotePort = socket.getPort();
  98. in = socket.getInputStream();
  99. out = socket.getOutputStream();
  100. }
  101.  
  102.  
  103. public void run()
  104. {
  105. byte[] receiveBuffer = new byte[BUFFER_SIZE];
  106. try
  107. {
  108. while (true)
  109. {
  110. int length = in.read(receiveBuffer);
  111. if (length == -1) // EOF
  112. {
  113. break;
  114. }
  115. else
  116. {
  117. log(remoteAddress, remotePort, receiveBuffer, length);
  118. Server.publishMessage(printable(receiveBuffer, 0, length));
  119. }
  120. }
  121. }
  122. catch (IOException ex)
  123. {
  124. log("I/O Exception: " + ex.getMessage());
  125. log(ex);
  126. }
  127. finally
  128. {
  129. try
  130. {
  131. out.close();
  132. }
  133. catch (IOException ex)
  134. {
  135. // ignore
  136. }
  137. try
  138. {
  139. in.close();
  140. }
  141. catch (IOException ex)
  142. {
  143. // ignore
  144. }
  145. try
  146. {
  147. socket.close();
  148. }
  149. catch (IOException ex)
  150. {
  151. // ignore
  152. }
  153. Server.activeThreads.remove(this);
  154. }
  155. }
  156.  
  157. public static void publishMessage(String msg)
  158. {
  159. for( Server t : activeThreads )
  160. {
  161. t.sendMessage(msg);
  162. }
  163. }
  164.  
  165. public void sendMessage(String msg)
  166. {
  167. try
  168. {
  169. System.out.println("sending Message: " + msg );
  170. out.write(msg.getBytes(), 0, msg.length());
  171. out.flush();
  172. } catch (IOException ex)
  173. {
  174. log("I/O Exception: " + ex.getMessage());
  175. log(ex);
  176. }
  177. }
  178.  
  179.  
  180. @SuppressWarnings("unused")
  181. private static String[] shift(String[] args)
  182. {
  183. String[] newArgs;
  184. if (args.length == 0)
  185. {
  186. newArgs = args;
  187. }
  188. else
  189. {
  190. newArgs = new String[args.length - 1];
  191. System.arraycopy(args, 1, newArgs, 0, args.length - 1);
  192. }
  193. return newArgs;
  194. }
  195. private static void log(String str)
  196. {
  197. if (!quietMode)
  198. {
  199. System.out.println(str);
  200. }
  201. }
  202.  
  203. private static void log(Throwable ex)
  204. {
  205. if (!quietMode)
  206. {
  207. ex.printStackTrace(System.out);
  208. }
  209. }
  210.  
  211. private static void log(InetAddress remoteAddress,
  212. int remotePort,
  213. byte[] data,
  214. int length)
  215. {
  216. if (!quietMode)
  217. {
  218. System.out.println(" From address: " + remoteAddress);
  219. System.out.println(" From port: " + remotePort);
  220. System.out.println(" Data length: " + length);
  221. System.out.println(" Data: \"" +
  222. printable(data, 0, length) + "\"");
  223. }
  224. }
  225.  
  226. // very conservative method for printing a byte array
  227. private static String printable(byte[] data, int offset, int length)
  228. {
  229. StringBuffer buf = new StringBuffer(length);
  230. for (int i = offset; i < length; ++i)
  231. {
  232. char ch = (char)(data[i] & 0xFF);
  233. if ((ch >= 0x20) && (ch <= 0x7E)) // Only US-ASCII
  234. {
  235. buf.append(ch);
  236. }
  237. else
  238. {
  239. buf.append('?');
  240. }
  241. }
  242. return buf.toString();
  243. }
  244. }
Add Comment
Please, Sign In to add comment