网络通信需要的东西:IP地址、端口号、协议
端口号:范围0-65535,0-1023为系统保留
IP地址+端口号=socket
一、InetAddress
static:
InetAddress.getLocalHost()
InetAddress.getByName()
方法:
address.getHostName()
address.getHostAddress()
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class Test1 {
public static void main(String[] arg) throws UnknownHostException {
//获取InetAddress实例
InetAddress address=InetAddress.getLocalHost();
//获取计算机名
System.out.println("计算机名:"+address.getHostName());
//获取IP地址
System.out.println("IP地址:"+address.getHostAddress());
//字节数组形式IP地址
byte[] bytes=address.getAddress();
System.out.println("字节数组形式IP地址:"+Arrays.toString(bytes));
//直接打印
System.out.println(address);
//根据机器名获取InetAddress实例
InetAddress address2=InetAddress.getByName("DESKTOP-A4T5UT1");
System.out.println("计算机名:"+address2.getHostName());
System.out.println("IP地址:"+address2.getHostAddress());
//根据IP获取InetAddress实例
InetAddress address3=InetAddress.getByName("192.168.1.108");
System.out.println("计算机名:"+address3.getHostName());
System.out.println("IP地址:"+address3.getHostAddress());
}
}
输出:
image.png
二、URL
方法:
url.getProtocol()
url.getHost()
url.getPort()
url.getPath()
url.getRef()
url.getQuery()
import java.net.MalformedURLException;
import java.net.URL;
public class Test1 {
public static void main(String[] arg) {
try {
//创建URL实例
URL imooc=new URL("http://www.imooc.com/");
//?后面表示参数,#后面表示锚点
URL url=new URL(imooc,"/index.html?username=tom#test");
System.out.println("协议:"+url.getProtocol());
System.out.println("主机:"+url.getHost());
//未指定端口号,使用默认端口号80,返回值为-1
System.out.println("端口:"+url.getPort());
System.out.println("文件路径:"+url.getPath());
System.out.println("文件名称:"+url.getRef());
System.out.println("查询字符串:"+url.getQuery());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出:
image.png
拉取网页:
import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Test1 {
public static void main(String[] arg) {
try {
//创建URL实例
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出:
image.png
为网页的html
三、socket
image.png
服务端:
public static void main(String[] arg) {
try{
//1.创建一个服务器端的Socket,即ServerSocket,指定绑定的端口
ServerSocket ss= new ServerSocket(8880);
//2.调用accept方法开始监听,等待客户端的连接
System.out.println("服务器即将启动,等待客户端的连接...");
Socket so=ss.accept();//accept方法返回Socket实例
//3.获取一个输入流,并读取客户端信息
InputStream is=so.getInputStream();//字节输入流
InputStreamReader isr=new InputStreamReader(is);//将字节输入流包装成字符输入流
BufferedReader br=new BufferedReader(isr);//加上缓冲流,提高效率
String info=null;
while((info=br.readLine())!=null){//循环读取客户端信息
System.out.println("我是服务器,客户端说:"+info);
}
so.shutdownInput();//关闭输入流
//4.获取一个输出流,向客户端输出信息,响应客户端的请求
OutputStream os=(OutputStream) so.getOutputStream();//字节输出流
PrintWriter pw=new PrintWriter(os);//字符输出流
BufferedWriter bw=new BufferedWriter(pw);//缓冲输出流
bw.write("欢迎您!");
bw.newLine();
bw.flush();
//5.关闭资源
os.close();
pw.close();
bw.close();
br.close();
isr.close();
is.close();
so.close();
ss.close();
}catch(IOException e){
e.printStackTrace();
}
}
客户端:
public static void main(String arg[]) throws IOException{
//1.创建客户端Socket,指定服务器地址和端口
Socket so=new Socket("localhost", 8880);//端口号要和服务器端相同
//2.获取输出流,向服务器端发送登录的信息
OutputStream os=(OutputStream) so.getOutputStream();//字节输出流
PrintWriter pw=new PrintWriter(os);//字符输出流
BufferedWriter bw=new BufferedWriter(pw);//加上缓冲流
bw.write("用户名:admin;密码:123");
bw.flush();
so.shutdownOutput();//关闭输出流
//3.获取输入流,得到服务端的响应信息
InputStream is=(InputStream) so.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String info=null;
while((info=br.readLine())!=null){
System.out.println("我是客户端,服务器说:"+info);
}
//4.关闭资源
bw.close();
pw.close();
os.close();
so.close();
}
网友评论