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

📄 udpecho.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 UDPEcho{    private static final int MAX_PACKET_SIZE = 65507;  // max possible for UDP    private static boolean quietMode = false;    public static void main(String[] args)    {        if ((args.length > 0) && "-q".equals(args[0]))        {            quietMode = true;            args = shift(args);        }        if (args.length != 1)        {            System.err.println("Usage: java UDPEcho [-q] <local-port>");            System.err.println(" -q: quiet mode (don't print out packets)");            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        {            DatagramSocket socket = new DatagramSocket(localPort);            byte[] receiveBuffer = new byte[MAX_PACKET_SIZE];            DatagramPacket receivePacket =                new DatagramPacket(receiveBuffer, receiveBuffer.length);            while (true)            {                log("Entering receive:");                socket.receive(receivePacket);                log("Packet received");                int length = receivePacket.getLength();                InetAddress remoteAddress = receivePacket.getAddress();                int remotePort = receivePacket.getPort();                logPacket(remoteAddress, remotePort, receiveBuffer, length);                // Echo by sending back exactly the same data                DatagramPacket sendPacket =                    new DatagramPacket(receiveBuffer,                                       length,                                       remoteAddress,                                       remotePort);                log("Entering send");                socket.send(sendPacket);                log("Echo packet sent");                receivePacket.setLength(receiveBuffer.length);            }        }        catch (IOException ex)        {            System.err.println("I/O Exception: " + ex.getMessage());            ex.printStackTrace(System.err);            System.exit(1);        }        System.exit(0);    }    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 logPacket(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 + -