URL
package com.intenet.ljb;
import java.io.*;
import java.net.URL;
public class URLTest {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.baidu.com");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
int rd = br.read();
while(rd != -1) {
System.out.print((char)rd);
rd = br.read();
}
br.close();
System.out.println("获取该 URL 的协议名:"+url.getProtocol());
System.out.println("获取该 URL 的授权名:"+url.getAuthority());
System.out.println("获取该 URL 的内容:"+url.getContent());
System.out.println("获取该 URL 的主机名:"+url.getHost());
System.out.println("获取该 URL 的文件路径:"+url.getPath());
System.out.println("获取该 URL 的端口号:"+url.getPort());
System.out.println("获取该 URL 的默认端口号:"+url.getDefaultPort());
System.out.println("获取该 URL 的查询名:"+url.getQuery());
System.out.println("获取该 URL 在文件中的相对位置:"+url.getRef());
}
}
InetAdress
package com.intenet.ljb;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAdressTest {
public static void main(String[] args) throws UnknownHostException {
InetAddress adder = InetAddress.getByName("www.baidu.com");
System.out.println("网站/IP地址:" + adder);
System.out.println("IP地址:" + adder.getHostAddress());
System.out.println("ip地址:" + adder.getCanonicalHostName());
System.out.println("本机域名:" + adder.getLocalHost());
System.out.println("获取本地IP地址:" + adder.getLoopbackAddress());
}
}
URLConnection 类
package com.intenet.ljb;
import java.io.*;
import java.net.*;
public class URALConnTest {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.baidu.com");
URLConnection conn = url.openConnection();
System.out.println("获取上次修改时间:"+conn.getLastModified());
System.out.println("获取内容长度:"+conn.getContentLength());
System.out.println("获取内容类型:"+conn.getContentType());
System.out.println("获取内容编码:"+conn.getContentEncoding());
System.out.println("获取数据:" + conn.getDate());
}
}
TCP
server
package com.tcp.ljb;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Serverljb {
public static void main(String[] args) throws IOException {
System.out.println("服务器端,等待客户端传送来的信息");
ServerSocket ss = new ServerSocket(1512);
while(true) {
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br.readLine();
System.out.println(str);
PrintWriter pw = new PrintWriter(s.getOutputStream());
System.out.println("请输入信息");
Scanner sc =new Scanner(System.in);
String str2 = sc.next();
pw.println(str2);
pw.close();
}
}
}
Client
package com.tcp.ljb;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Clientljb {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("localhost",1512);
PrintWriter pr = new PrintWriter(s.getOutputStream());
System.out.println("已连接服务器");
System.out.println("input content:");
Scanner sc =new Scanner(System.in);
String str2 = sc.next();
pr.println(str2);
pr.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str3 = br.readLine();
System.out.println(str3);
br.close();
}
}
UDP
server
package com.udp.ljb;
import java.io.IOException;
import java.net.*;
public class Serversudp {
public static void main(String[] args) throws IOException {
System.out.println("等待接收数组");
DatagramSocket ds = new DatagramSocket(4545);
byte[] buf = new byte[1024];
DatagramPacket dp =new DatagramPacket(buf, 1024);
while(true) {
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
if(str.equals("exit")) {
ds.close();
break;
}else {
System.out.println(str);
}
}
}
}
Client
package com.udp.ljb;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class Clienttudp {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(1513);
System.out.println("请输入发送的数据:");
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String str = sc.next();
if(str.equals("exit")) {
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
InetAddress.getByName("localhost"),4545);
ds.send(dp);
ds.close();
break;
}else {
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
InetAddress.getByName("localhost"),4545);
System.out.println("再次输入:");
ds.send(dp);
}
}
}
}
|