Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright 2003 Nokia Corporation.
- //
- // THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
- // EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
- // FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
- // OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
- // ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
- // OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
- // SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
- // RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
- // OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
- // BY THIRD PARTIES
- //
- // Furthermore, information provided in this source code is preliminary,
- // and may be changed substantially prior to final release. Nokia Corporation
- // retains the right to make changes to this source code at
- // any time, without notice. This source code is provided for informational
- // purposes only.
- //
- // Nokia and Nokia Connecting People are registered trademarks of Nokia
- // Corporation.
- // Java and all Java-based marks are trademarks or registered trademarks of
- // Sun Microsystems, Inc.
- // Other product and company names mentioned herein may be trademarks or
- // trade names of their respective owners.
- //
- // A non-exclusive, non-transferable, worldwide, limited license is hereby
- // granted to the Licensee to download, print, reproduce and modify the
- // source code. The licensee has the right to market, sell, distribute and
- // make available the source code in original or modified form only when
- // incorporated into the programs developed by the Licensee. No other
- // license, express or implied, by estoppel or otherwise, to any other
- // intellectual property rights is granted herein.
- // unnamed package
- import java.io.*;
- import java.net.*;
- import java.util.Vector;
- // This is a very simple server intended for simple testing only
- public class Server
- extends Thread
- {
- private static final int BUFFER_SIZE = 65536;
- private static boolean quietMode = false;
- private static boolean useNagle = false;
- private final Socket socket;
- private final InetAddress remoteAddress;
- private final int remotePort;
- private final InputStream in;
- private final OutputStream out;
- private static Vector<Server> activeThreads = new Vector<Server>();
- public static void main(String[] args)
- {
- int localPort = 2000;
- try
- {
- ServerSocket serverSocket = new ServerSocket(localPort);
- while (true)
- {
- log("Entering accept:");
- Socket socket = serverSocket.accept();
- if (!useNagle)
- {
- socket.setTcpNoDelay(true);
- }
- log("Connection opened");
- Server conn = new Server(socket);
- conn.start();
- activeThreads.add(conn);
- }
- }
- catch (IOException ex)
- {
- System.err.println("I/O Exception: " + ex.getMessage());
- ex.printStackTrace(System.err);
- System.exit(1);
- }
- System.exit(0);
- }
- private Server(Socket socket)
- throws IOException
- {
- this.socket = socket;
- remoteAddress = socket.getInetAddress();
- remotePort = socket.getPort();
- in = socket.getInputStream();
- out = socket.getOutputStream();
- }
- public void run()
- {
- byte[] receiveBuffer = new byte[BUFFER_SIZE];
- try
- {
- while (true)
- {
- int length = in.read(receiveBuffer);
- if (length == -1) // EOF
- {
- break;
- }
- else
- {
- log(remoteAddress, remotePort, receiveBuffer, length);
- Server.publishMessage(printable(receiveBuffer, 0, length));
- }
- }
- }
- catch (IOException ex)
- {
- log("I/O Exception: " + ex.getMessage());
- log(ex);
- }
- finally
- {
- try
- {
- out.close();
- }
- catch (IOException ex)
- {
- // ignore
- }
- try
- {
- in.close();
- }
- catch (IOException ex)
- {
- // ignore
- }
- try
- {
- socket.close();
- }
- catch (IOException ex)
- {
- // ignore
- }
- Server.activeThreads.remove(this);
- }
- }
- public static void publishMessage(String msg)
- {
- for( Server t : activeThreads )
- {
- t.sendMessage(msg);
- }
- }
- public void sendMessage(String msg)
- {
- try
- {
- System.out.println("sending Message: " + msg );
- out.write(msg.getBytes(), 0, msg.length());
- out.flush();
- } catch (IOException ex)
- {
- log("I/O Exception: " + ex.getMessage());
- log(ex);
- }
- }
- @SuppressWarnings("unused")
- private static String[] shift(String[] args)
- {
- String[] newArgs;
- if (args.length == 0)
- {
- newArgs = args;
- }
- else
- {
- newArgs = new String[args.length - 1];
- System.arraycopy(args, 1, newArgs, 0, args.length - 1);
- }
- return newArgs;
- }
- private static void log(String str)
- {
- if (!quietMode)
- {
- System.out.println(str);
- }
- }
- private static void log(Throwable ex)
- {
- if (!quietMode)
- {
- ex.printStackTrace(System.out);
- }
- }
- private static void log(InetAddress remoteAddress,
- int remotePort,
- byte[] data,
- int length)
- {
- if (!quietMode)
- {
- System.out.println(" From address: " + remoteAddress);
- System.out.println(" From port: " + remotePort);
- System.out.println(" Data length: " + length);
- System.out.println(" Data: \"" +
- printable(data, 0, length) + "\"");
- }
- }
- // very conservative method for printing a byte array
- private static String printable(byte[] data, int offset, int length)
- {
- StringBuffer buf = new StringBuffer(length);
- for (int i = offset; i < length; ++i)
- {
- char ch = (char)(data[i] & 0xFF);
- if ((ch >= 0x20) && (ch <= 0x7E)) // Only US-ASCII
- {
- buf.append(ch);
- }
- else
- {
- buf.append('?');
- }
- }
- return buf.toString();
- }
- }
Add Comment
Please, Sign In to add comment