1.URL类
InetAddress类
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class InetAddressTest {
public static void main(String[] args) {
try {
//创建本机实例
InetAddress address=InetAddress.getLocalHost();
System.out.println("主机名:"+address.getHostName()); //主机名:WIN-H7CQ7OQ1VQH
System.out.println("IP:"+address.getHostAddress()); //IP:121.248.9.62
byte[] ip=address.getAddress();
System.out.println("IP数组:"+Arrays.toString(ip));//IP数组:[121, -8, 9, 62]
System.out.println(address); //WIN-H7CQ7OQ1VQH/121.248.9.62
//根据机器名创建实例
InetAddress address2=InetAddress.getByName("WIN-H7CQ7OQ1VQH");
System.out.println(address2);
//根据IP创建实例
InetAddress address3=InetAddress.getByAddress(ip);
System.out.println(address3);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
URL
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest {
//URL类使用
public void URLUse(){
try {
URL imooc=new URL("http://www.imooc.com");
//根据现有URL新建URL,?后面表示参数,#后面表示锚点
URL url=new URL(imooc,"/index.html?username=tom#test");
System.out.println("协议名:"+url.getProtocol()); //协议名:http
System.out.println("资源名:"+url.getHost()); //资源名:www.imooc.com
//如果未指定端口号,则使用默认端口号,此时getport()方法返回值为-1
System.out.println("端口名:"+url.getPort());
System.out.println("文件路径:"+url.getPath()); //文件路径:/index.html
System.out.println("文件名:"+url.getFile()); //文件名:/index.html?username=tom
System.out.println("相对路径:"+url.getRef()); //相对路径:test
System.out.println("查询字符串:"+url.getQuery()); //查询字符串:username=tom
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
//使用URL读取网业内容
public void URLRead(){
try {
URL url=new URL("http://www.baidu.com");
//通过URL的openStream方法获取URL对象所表示的资源的字节输入流
InputStream is= url.openStream();
//将字节输入流转换为字符输入流
InputStreamReader isr=new InputStreamReader(is,"utf-8");
//为字符输入流添加缓冲
BufferedReader br=new BufferedReader(isr);
//读取数据
String data=br.readLine();
while(data!=null){
System.out.println(data);
data=br.readLine();
};
br.close();
isr.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
URLTest test=new URLTest();
test.URLRead();
}
}
2.Socket
1)ServerThread 自定义线程处理类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread extends Thread {
Socket socket=null;
public ServerThread(Socket socket){
this.socket=socket;
}
public void run(){
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os=null;
PrintWriter pw=null;
try {
is = socket.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
String data;
while((data=br.readLine())!=null){
System.out.println(data);
}
socket.shutdownInput();
os=socket.getOutputStream();
pw=new PrintWriter(os);
pw.write("我是服务器"+socket.getInetAddress());
pw.flush();
socket.shutdownOutput();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(br!=null)
br.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(pw!=null)
pw.close();
if(os!=null)
os.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
2)server
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket server=null;
Socket socket=null;
try {
server=new ServerSocket(8889);
while(true){
socket=server.accept(); //循环监听
ServerThread thread=new ServerThread(socket); //线程
thread.start(); //启动线程
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
socket.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3)client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Socket socket=new Socket("localhost",8889);
//写
OutputStream os=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.write("我是客户端"+socket.getInetAddress());
pw.flush();
socket.shutdownOutput();
//读
InputStream is=socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String data;
while((data=br.readLine())!=null){
System.out.println(data);
}
socket.shutdownInput();
pw.close();
os.close();
br.close();
isr.close();
is.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}