标签:
package net;import java.net.InetAddress;import java.net.UnknownHostException;public class InetDemo {public static void main(String[] args) throws UnknownHostException {// 获取本机IP对象InetAddress iphost = InetAddress. getLocalHost();String ipstr = iphost.getHostAddress(); // "192.168.1.22"String ipname = iphost.getHostName(); // "YH-PC"// 获取指定IP对象InetAddress ip = InetAddress.getByName("192.168.1.1");ipstr = ip.getHostAddress(); // "192.168.1.1"ipname = ip.getHostName(); // "192.168.1.1"// 获取指定主机的IP对象InetAddress[] ips = InetAddress.getAllByName("www.google.com.hk");for (int i = 0; i < ips.length; i++) {ipstr = ips[i].getHostAddress();ipname = ips[i].getHostName();System.out.println( ipstr);System.out.println( ipname);// 173.194.120.88// www.google.com.hk// 216.239.32.39// www.google.com.hk}}}
package net;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class UDP_Send {public static void main(String[] args) throws IOException {/** UDP发送端*/System.out.println( "UDP发送端");// 获取本地IP地址InetAddress ip = InetAddress.getLocalHost();System.out.println( "Destination IP = " + ip.getHostAddress());// 1. 建立UDP的socketDatagramSocket ds = new DatagramSocket();// 2. 将数据封装到数据包中byte[] buf = "hello UDP!".getBytes();DatagramPacket dp = new DatagramPacket( buf, buf.length , ip , 22222);// 3. 使用Socket对象的send方法将数据包发送出去ds.send(dp);// 4. 关闭资源。ds.close();}}
package net;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;public class UDP_Receive {public static void main(String[] args) throws IOException {/** UDP接收端*/System.out.println( "UDP接收端");// 1. 建立UDP的socketDatagramSocket ds = new DatagramSocket(22222);// 2. 定义数据包方便接收数据byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket( buf, buf.length );// 3. 使用Socket对象的receive方法阻塞接收数据ds.receive(dp);// 4. 获取接收到的数据String ip = dp.getAddress().getHostAddress();int port = dp.getPort();String data = new String( dp.getData(), 0, dp.getLength());System.out.println( "ip = " + ip + "\nport = " + port + "\ndata = " + data );// 4. 关闭资源。ds.close();}}
package net;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;public class Test_ChatByUDP {public static void main(String[] args) throws SocketException, UnknownHostException {// 创建用于发送和接收的套接字,端口33333只用于聊天室接收程序。DatagramSocket sendDs = new DatagramSocket();DatagramSocket receDs = new DatagramSocket(33333);// 创建广播地址InetAddress ip = InetAddress.getByName("192.168.1.255");// 创建发送和接收对象Send sd = new Send( sendDs, ip);Rece rc = new Rece( receDs);new Thread( sd).start();new Thread( rc).start();}}class Send implements Runnable {private DatagramSocket ds;private InetAddress ip;public Send(DatagramSocket ds, InetAddress ip) {super();this.ds = ds;this.ip = ip;}@Overridepublic void run() {// 读取键盘录入BufferedReader br = new BufferedReader( new InputStreamReader(System.in ));String str = null;try {while (( str = br.readLine()) != null) {// 发送键盘录入的字符DatagramPacket dp = new DatagramPacket(str.getBytes(), str .length(), ip , 33333);ds.send( dp);if ( "exit".equals(str )) {break;} else if ( "close".equals(str )) {break;}}ds.close();} catch (IOException e) {e.printStackTrace();}}}class Rece implements Runnable {private DatagramSocket ds;public Rece(DatagramSocket ds) {super();this.ds = ds;}@Overridepublic void run() {while ( true) {byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket( buf, buf.length );try {// 阻塞接收ds.receive( dp);} catch (IOException e) {e.printStackTrace();}// 获取发送方地址String ip = dp.getAddress().getHostAddress();String str = new String( dp.getData(), 0, dp.getLength());int port = dp.getPort();if ("exit".equals( str)) {// 判断发送字符System. out.println(ip + ":" + port + " 离开了聊天室!" );} else if ("close".equals( str)) {System. out.println(ip + ":" + port + " 请求关闭聊天室!" );break;} else {System. out.println(ip + ":" + port + " 说:" + str);}}ds.close();System.out.println( "聊天室已关闭!" );}}
package net;import java.net.InetAddress;import java.net.UnknownHostException;public class InetDemo {public static void main(String[] args) throws UnknownHostException {// 获取本机IP对象InetAddress iphost = InetAddress. getLocalHost();String ipstr = iphost.getHostAddress(); // "192.168.1.22"String ipname = iphost.getHostName(); // "YH-PC"// 获取指定IP对象InetAddress ip = InetAddress.getByName("192.168.1.1");ipstr = ip.getHostAddress(); // "192.168.1.1"ipname = ip.getHostName(); // "192.168.1.1"// 获取指定主机的IP对象InetAddress[] ips = InetAddress.getAllByName("www.google.com.hk");for (int i = 0; i < ips.length; i++) {ipstr = ips[i].getHostAddress();ipname = ips[i].getHostName();System.out.println( ipstr);System.out.println( ipname);// 173.194.120.88// www.google.com.hk// 216.239.32.39// www.google.com.hk}}}
package net;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class UDP_Send {public static void main(String[] args) throws IOException {/** UDP发送端*/System.out.println( "UDP发送端");// 获取本地IP地址InetAddress ip = InetAddress.getLocalHost();System.out.println( "Destination IP = " + ip.getHostAddress());// 1. 建立UDP的socketDatagramSocket ds = new DatagramSocket();// 2. 将数据封装到数据包中byte[] buf = "hello UDP!".getBytes();DatagramPacket dp = new DatagramPacket( buf, buf.length , ip , 22222);// 3. 使用Socket对象的send方法将数据包发送出去ds.send(dp);// 4. 关闭资源。ds.close();}}
package net;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;public class UDP_Receive {public static void main(String[] args) throws IOException {/** UDP接收端*/System.out.println( "UDP接收端");// 1. 建立UDP的socketDatagramSocket ds = new DatagramSocket(22222);// 2. 定义数据包方便接收数据byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket( buf, buf.length );// 3. 使用Socket对象的receive方法阻塞接收数据ds.receive(dp);// 4. 获取接收到的数据String ip = dp.getAddress().getHostAddress();int port = dp.getPort();String data = new String( dp.getData(), 0, dp.getLength());System.out.println( "ip = " + ip + "\nport = " + port + "\ndata = " + data );// 4. 关闭资源。ds.close();}}
package net;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;public class Test_ChatByUDP {public static void main(String[] args) throws SocketException, UnknownHostException {// 创建用于发送和接收的套接字,端口33333只用于聊天室接收程序。DatagramSocket sendDs = new DatagramSocket();DatagramSocket receDs = new DatagramSocket(33333);// 创建广播地址InetAddress ip = InetAddress.getByName("192.168.1.255");// 创建发送和接收对象Send sd = new Send( sendDs, ip);Rece rc = new Rece( receDs);new Thread( sd).start();new Thread( rc).start();}}class Send implements Runnable {private DatagramSocket ds;private InetAddress ip;public Send(DatagramSocket ds, InetAddress ip) {super();this.ds = ds;this.ip = ip;}@Overridepublic void run() {// 读取键盘录入BufferedReader br = new BufferedReader( new InputStreamReader(System.in ));String str = null;try {while (( str = br.readLine()) != null) {// 发送键盘录入的字符DatagramPacket dp = new DatagramPacket(str.getBytes(), str .length(), ip , 33333);ds.send( dp);if ( "exit".equals(str )) {break;} else if ( "close".equals(str )) {break;}}ds.close();} catch (IOException e) {e.printStackTrace();}}}class Rece implements Runnable {private DatagramSocket ds;public Rece(DatagramSocket ds) {super();this.ds = ds;}@Overridepublic void run() {while ( true) {byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket( buf, buf.length );try {// 阻塞接收ds.receive( dp);} catch (IOException e) {e.printStackTrace();}// 获取发送方地址String ip = dp.getAddress().getHostAddress();String str = new String( dp.getData(), 0, dp.getLength());int port = dp.getPort();if ("exit".equals( str)) {// 判断发送字符System. out.println(ip + ":" + port + " 离开了聊天室!" );} else if ("close".equals( str)) {System. out.println(ip + ":" + port + " 请求关闭聊天室!" );break;} else {System. out.println(ip + ":" + port + " 说:" + str);}}ds.close();System.out.println( "聊天室已关闭!" );}}
标签:
原文地址:http://blog.csdn.net/u010388781/article/details/51167757