Java网络编程
Java网络编程
OSI 应用层、表示层、会话层、传输层、网络层、数据链路层、物理层
TCP/IP 应用层(HTTP,FTP,DNS..)、传输层(TCP,UDP..)、网络层(IP,ICMP)、数据链路层+物理层
通信三要素 一 IP和端口号,组合得网络套接字Socket(传输节点)
1、IP唯一标识Internet上计算机
2、JAVA中InetAddress类代表IP
3、IP分类,IPv4和IPv6 万维网 和局域网(192.168.0.0...192.168.255.255,本地地址127.0.0.1,localhost
4、端口号标识正在计算机上运行的进程,0~65535,公认端口 0~1023,注册端口 102449151,动态私有端口4915165535
5、TCP/IP 传输层 传输控制协议(TCP)和网络层 网络互联协议(IP)
//IP地址
InetAddress inet1 = InetAddress.getByName("127.0.0.0");
//域名
InetAddress inet2 = InetAddress.getByName("www.baidu.com");
//本地地址
InetAddress inet3 = InetAddress.getLocalHost();
//获得域名和地址
inet1.getHostName();
inet1.getHostAddress();
TCP和UDP
TCP
1、使用TCP协议,先建立TCP连接,形成数据通道
2、传输前,采用三次握手,点对点通信,是可靠的
3、TCP协议包括两个进程 客户端和服务端
4、连接中可以进行大数据量的传输
5、传输完毕,需释放已建立的连接,效率低
UDP
1、将数据,源,目的封装成数据包,不需要建立连接
2、每个数据报的大小限制在64k内
3、发送不管对方是否准备好,接受方不确认,不可靠
4、可以广播发送
5、发送数据后无需释放资源,开销小,速度快
案例1:TCP通信
public class C_S {
@Test
public void client() {
Socket s1 = null;
OutputStream os = null;
try {
InetAddress ip = InetAddress.getByName("127.0.0.1");
s1= new Socket(ip,8899);
os= s1.getOutputStream();
os.write("你好,我是客户端".getBytes());
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
os.close();
s1.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
@Test
public void server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
ss= new ServerSocket(8899);
s = ss.accept();
is = s.getInputStream();
// byte[] buffer = new byte[20];
// <u>int</u> <u>len</u>;
// while((<u>len</u> = is.read(buffer))!= -1) {
// String <u>str</u> = new String(buffer,0,<u>len</u>);
// System.out.println(<u>str</u>);
// }
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while((len = is.read(buffer))!= -1) {
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
System.out.println(s.getInetAddress().getHostAddress());
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
baos.close();
is.close();
s.close();
ss.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
案例2:UDP通信
@Test
public void send() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "UDP发送的数据";
byte[] data = str.getBytes();
InetAddress localHost = InetAddress.getLocalHost();
DatagramPacket packet = new
DatagramPacket(data,0,data.length,localHost,9000);
socket.send(packet);
socket.close();
}
@Test
public void receive() throws IOException {
DatagramSocket socket = new DatagramSocket(9000);
byte[] buffer = new byte[100];
DatagramPacket packet = new
DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new
String(packet.getData(),0,packet.getLength()));
socket.close();
}
URL编程
URL 统一资源定位符,表示Internet上某一资源的地址 (种子),代表服务器一个资源。
<传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
URL url = new URL("网址");
//public String getProtocol() 获取URL协议名
//public String getHost() 获取URL主机名
//public String getPort() 获取URL端口号
//public String getPath() 获取文件路径
//public String getFile() 获取URL文件名
//public String getQuery() 获取URL的查询名
//获取流
HttpURLConnection c =(HttpURLConnection) url.openConnection();
c.connect();
InputStream =c.getInputStream(0;
浙公网安备 33010602011771号