Tag:network program

Tag (network program)'s result:

Java URL class

在java.net包中定义了URL类,该类用来处理有关URL的内容。 openStream()方法只能读取网络资源,若要既能读取又能发送数据,则要用到URL类的openConnection()方法来创建一个 URLConnection类的对象,此对象在本地机和URL指定的远程节点建立一条HTTP协议的数据通道,可进行双向数据传输 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class Urltest { public static void main(String[] args) throws IOException { URL baidu = new URL(“http://www.baidu.com”); System.out.println(“protocol: “+baidu.getProtocol()); System.out.println(“authority: “+baidu.getAuthority()); System.out.println(“host: “+baidu.getHost()); System.out.println(“port: “+baidu.getPort()); System.out.println(“path: “+baidu.getPath()); System.out.println(“query: “+baidu.getQuery()); System.out.println(“filename: “+baidu.getFile()); System.out.println(“ref: “+baidu.getRef()); /*URLConnection baiduconn = baidu.openConnection(); in = new BufferedReader(new InputStreamReader(baiduconn.getInputStream())); */ /*BufferedReader in = new BufferedReader(new InputStreamReader(baidu.openStream())); String inputline; while((inputline = in.readLine())!=null) System.out.println(inputline);*/ /*in.close();*/ } } 参考链接: http://www.runoob.com/java/java-url-processing.html http://blog.csdn.net/witsmakemen/article/details/6989010  

Java multicast

MulticastSocketServer.java import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; public class MulticastSocketServer { public static void main(String[] args) throws Exception { InetAddress group = InetAddress.getByName(“224.0.0.4”); //组播地址 int port = 9876; // 端口 MulticastSocket mss = null; try { mss = new MulticastSocket(port); // \1.创建一个用于发送和接收的MulticastSocket组播套接字对象 mss.joinGroup(group); //3.使用组播套接字joinGroup(),将其加入到一个组播 System.out.println(“begining send packet —” + new java.util.Date() + “)”); while (true) { String message = “Hello” + new java.util.Date(); byte[] buffer = message.getBytes(); DatagramPacket dp = new DatagramPacket(buffer, buffer.length, group, port); //2.创建一个指定缓冲区大小及组播地址和端口的DatagramPacket组播数据包对象 mss.send(dp); //4.使用组播套接字的send()方法,将组播数据包对象放入其中,发送组播数据包 // String s = new String(dp.getData(), 0, dp.getLength()); //5.解码组播数据包提取信息,并依据得到的信息作出响应 System.out.println(“发送数据包给” + group + “:” + port); Thread.sleep(1000); } } catch (IOException e) { e.printStackTrace(); } finally{ try { if(mss!=null){ mss.leaveGroup(group); mss.close(); } } catch (Exception e2) { // TODO: handle exception } } } } MulticastSocketClient.java: import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; public class MulticastSocketClient { public static void main(String[] args) throws Exception { InetAddress group = InetAddress.getByName(“224.0.0.4”); //组播地址 int port = 9876; //端口 MulticastSocket msr = null; try { msr = new MulticastSocket(port); //1.创建一个用于发送和接收的MulticastSocket组播套接字对象 msr.joinGroup(group); //3.使用组播套接字joinGroup(),将其加入到一个组播 byte[]……

java socket udp

Udpserver.java: import java.net.DatagramPacket; import java.net.DatagramSocket; public class Udpserver { public static void main(String[] args) throws Exception { System.out.println(“udp server is running……”); String str_send = “hi udpclient”; DatagramSocket se = new DatagramSocket(2016); byte[] by = new byte[1024]; DatagramPacket dp = new DatagramPacket(by,1024); while (true) { se.receive(dp); System.out.println(“the message from client is: \n”); String str_receive = new String(dp.getData(),0,dp.getLength()); System.out.println(str_receive); DatagramPacket se_send = new DatagramPacket(str_send.getBytes(),str_send.length(),dp.getAddress(),dp.getPort()); se.send(se_send); dp.setLength(1024); } //se.close(); } } Udpclient.java: import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Udpclient { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(); String str = “hi server”; DatagramPacket dp = new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName(“localhost”),2016); ds.send(dp); byte[] by = new byte[1024]; DatagramPacket dp2 = new DatagramPacket(by,1024); ds.receive(dp2); System.out.println(new String(by,0,dp2.getLength())); ds.close(); } }