⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpecho.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
// 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 packageimport java.io.*;import java.net.*;// This is a very simple server intended for simple testing onlypublic class TCPEcho    implements Runnable{    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;    public static void main(String[] args)    {        if ((args.length > 0) && "-q".equals(args[0]))        {            quietMode = true;            args = shift(args);        }        if ((args.length > 0) && "-N".equals(args[0]))        {            useNagle = true;            args = shift(args);        }        if (args.length != 1)        {            System.err.println(                "Usage: java TCPEcho [-q] [-N] <local-port>");            System.err.println(" -q: quiet mode (don't print out data read)");            System.err.println(" -N: use Nagle's TCP algorithm");            System.exit(1);        }        int localPort = 0;        try        {            localPort = Integer.parseInt(args[args.length - 1]);        }        catch (NumberFormatException ex)        {            System.err.println("Invalid number: \"" +                               args[args.length - 1] + "\"");            System.exit(1);        }        try        {            ServerSocket serverSocket = new ServerSocket(localPort);            while (true)            {                log("Entering accept:");                Socket socket = serverSocket.accept();                if (!useNagle)                {                    socket.setTcpNoDelay(true);                }                log("Connection opened");                TCPEcho conn = new TCPEcho(socket);                new Thread(conn).start();            }        }        catch (IOException ex)        {            System.err.println("I/O Exception: " + ex.getMessage());            ex.printStackTrace(System.err);            System.exit(1);        }        System.exit(0);    }    private TCPEcho(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);                    // Echo by writing back exactly the same data                    out.write(receiveBuffer, 0, length);                    out.flush();                }            }        }        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            }        }    }    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();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -