文章作者 100test 发表时间 2007:09:06 11:52:27
来源 100Test.Com百考试题网
import Java.net.*. public class TestNet { public static void main(String[] args) throws Exception { InetAddress ia = InetAddress.getByName("compaq"). String ipAdr = ia.getHostAddress(). System.out.println(ipAdr). } } |
DatagramPacket(byte[] buf, int length, InetAddress address, int port) DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) DatagramPacket(byte[] buf, int length, SocketAddress address) DatagramPacket(byte[] buf, int length) DatagramPacket(byte[] buf, int offset, int length) |
import Java.io.*. import Java.net.*. import Java.util.*. public class TimeServer { final private static int DAYTIME_PORT = 13. public static void main(String args[]) throws IOException { DatagramSocket socket = new DatagramSocket(DAYTIME_PORT). while (true) { byte buffer[] = new byte[256]. DatagramPacket packet =new DatagramPacket(buffer, buffer.length). socket.receive(packet). String date = new Date().toString(). buffer = date.getBytes(). // Get response address/port // for client from packet InetAddress address = packet.getAddress(). int port = packet.getPort(). packet = new DatagramPacket(buffer, buffer.length, address, port). socket.send(packet). } } } import Java.io.*. import Java.net.*. public class GetTime { final private static int DAYTIME_PORT = 13. public static void main(String args[]) throws IOException { if (args.length == 0) { System.err.println("Please specify daytime host"). System.exit(-1). } String host = args[0]. byte message[] = new byte[256]. InetAddress address = InetAddress.getByName(host). System.out.println("Checking at: " address). DatagramPacket packet = new DatagramPacket(message, message.length, address, DAYTIME_PORT). DatagramSocket socket = new DatagramSocket(). socket.send(packet). packet =new DatagramPacket(message, message.length). socket.receive(packet). String time = new String(packet.getData()). System.out.println(The time at " host " is: " time). socket.close(). } } |
相关文章
package与import
Java理论与实践:用弱引用堵住内存泄漏
基于Java的UDP协议程序设计初探
Java中对HashMap的深度分析与比较
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛