网络编程概述
JavaWeb:网页编程 基于B/S架构
网络编程:TCP/IP协议栈 基于C/S架构
网络通信的要素
-
网络编程中有两个主要的问题:
如何准确的定位到网络上的一台或多台主机
找到主机之后如何进行通信 -
网络编程中的要素:
IP和端口号 涉及到IP协议
网络通信协议 涉及到tcp,udp协议
IP
ip地址:InetAddress
唯一定位一台网络上的主机。
端口
端口表示计算机上的一个程序的进程。
不同的进程有不同的端口号,用来区分不同的应用。
端口被规定为一个16位的整数0~65535
端口分类:
公认端口:0~1023
被预先定义的服务通信占用(如HTTP占用端口80,FTP占用端口21,Telnet占用端口23)
注册端口:1024~49151
分配给用户进程或应用程序(如Tomcat占用端口8080,MySQL占用端口3306,Oracle占用端口1521)
动态/私有端口:49152~65535
端口号与IP地址的组合得出一个网络套接字:Socket。
如何实例化InetAddress:
package ip;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPTest {
public static void main(String[] args) {
try {
InetAddress inetAddress = InetAddress.getByName("14.215.177.39");
System.out.println(inetAddress);
InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress1);
//获取本地IP地址
System.out.println(InetAddress.getLocalHost());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
TCP协议:
使用TCP协议之前,必须建立TCP连接,形成传输数据通道;
传输前,采用“三次握手”方式,点对点通信,是可靠的;
TCP协议进行通信的两个应用进程:客户端、服务端;
在连接中可进行大数据量的传输;
传输完毕,需释放已建立的连接,效率低。
UDP协议:
将数据、源、目的封装成数据包,不需要建立连接;
每个数据报的大小限制在64K内;
发送不管对方是否准备好,接收方收到也不确认,故是不可靠的;
可以广播发送;
发送数据结束时无需释放资源,开销小,速度快。
实现TCP协议的网络编程
package Socket_coding;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class IPTest {
//客户端
@Test
public void client() throws IOException {
//1.获取需要和服务器端通信的IP地址和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
Socket socket = new Socket(inet, 8888);
//2.获取一个输出流,用于输出数据
OutputStream os = socket.getOutputStream();
//3.写出数据的操作
os.write("你好,我是客户端".getBytes());
os.close();
socket.close();
}
//服务端
@Test
public void servre() throws IOException {
//1.创建服务器端的ServerSocket,指明自己的端口号
ServerSocket serverSocket = new ServerSocket(8888);
//2.调用accept()方法,表示接收来自客户端的socket
Socket accept = serverSocket.accept();
//3.获取输入流
InputStream is = accept.getInputStream();
//4.获取输入流当中的数据
ByteArrayOutputStream 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("客户端IP地址"+accept.getInetAddress().getHostAddress());
baos.close();
is.close();
accept.close();
serverSocket.close();
}
}
例:服务器端接收并保存客户端发送过来的文件。
package Socket_coding;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class IPTest2 {
//客户端
@Test
public void client() throws IOException {
InetAddress inet = InetAddress.getByName("localhost");
int port = 8888;
Socket socket = new Socket(inet,port);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("赞.png"));
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer))!=-1){
os.write(buffer, 0,len );
}
fis.close();
os.close();
socket.close();
}
//服务端
@Test
public void server() throws IOException {
int port = 8888;
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("接收的赞.png"));
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
例:客户端给服务端发送文本,服务端会将文本转成大写再返回给客户端。
package Socket_coding;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class IPTest4 {
//客户端
@Test
public void client() throws IOException {
InetAddress inet = InetAddress.getByName("localhost");
int port = 8888;
Socket socket = new Socket(inet,port);
OutputStream os = socket.getOutputStream();
os.write("sdlfslfsf".getBytes());
//停止输出流的输出
socket.shutdownOutput();
//等待开始接收服务器返回来的信息
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] buff = new byte[1024];
while((len=is.read(buff))!=-1){
baos.write(buff,0 ,len );
}
System.out.println(baos.toString());
baos.close();
is.close();
os.close();
socket.close();
}
//服务器端
@Test
public void server() throws IOException {
int port = 8888;
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[10];
while((len=is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
//解析结果并转为大写字母
String res = baos.toString().toUpperCase();
//开始将解析结果返回给客户端
OutputStream os = socket.getOutputStream();
os.write(res.getBytes());
os.close();
is.close();
socket.close();
serverSocket.close();
}
}
实现UDP协议的网络编程
-
类DatagramSocket和DatagramPacket实现了基于UDP协议网络程序。
-
UDP数据报通过数据报套接字DatagramSocket发送和接收,系统不保证UDP数据报一定能够安全送到目的地,也不能确定什么时候可以抵达。
-
DatagramPacket对象封装了UDP数据报,在数据报中包含了发送端的IP地址和端口号以及接收端的IP地址和端口号。
-
UDP协议中每个数据报都给出了完整的地址信息,因此无须建立发送方和接收方的连接。如同发快递包裹一样。
package Socket_coding;
import org.junit.Test;
import java.io.IOException;
import java.net.*;
public class UDPTest {
//发送端
@Test
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "我是UDP方式发送的信息";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,8888);
socket.send(packet);
socket.close();
}
//接收端
@Test
public void receiver() throws IOException{
int port = 8888;
DatagramSocket socket = new DatagramSocket(port);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData()));
socket.close();
}
}
URL编程
-
URL:统一资源定位符,它表示Internet上某一资源的地址。
-
它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。
-
通过URL我们可以访问Internet上的各种网络资源,比如最常见的www,ftp站点等等。浏览器通过解 - 析给i定的URL可以在网路上查找相应的文件或其它资源。
-
URL的基本结构由5部分组成:
<传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
例如:
http://192.168.0.1:8080/helloworld/index.jsp#a?username=123&password=123
package Socket_coding;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLTest {
@Test
public void test() throws IOException {
//通过一个链接来获取网络资源
URL url = new URL("http://localhost:8080/examples/zan.jpg");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("newpictures.jpg");
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fos.close();
is.close();
urlConnection.disconnect();
}
}