一.网络编程三要素:
IP地址:每个设备在网络中的唯一标识。
端口号:每个程序在设备上的唯一标识。
协议:为计算机网络中进行数据交换而建立的规则或约定的集合。
UDP: 面向无连接,数据不安全,速度快,不区分客户与服务端。
TCP: 面向连接(三次握手),数据安全,速度略低,分客户端和服务端。
二.Socket编程:(Java网络编程的主要内容)
套接字使用TCP提供了两台计算机之间的通信机制。 客户端程序创建一个套接字,并尝试连接服务器的套接字。当连接建立时,服务器会创建一个 Socket 对象。客户端和服务器现在可以通过对 Socket 对象的写入和读取来进行通信。
java.net.Socket 类代表一个套接字,并且 java.net.ServerSocket 类为服务器程序提供了一种来监听客户端,并与他们建立连接的机制。
以下步骤是两台计算机之间使用套接字建立TCP连接步骤:
1.服务器实例化一个 ServerSocket 对象,表示通过服务器上的端口通信。
2.服务器调用 ServerSocket 类的 accept() 方法,该方法将一直等待,直到客户端连接到服务器上给定的端口。
3.服务器正在等待时,一个客户端实例化一个 Socket 对象,指定服务器名称和端口号来请求连接。
4.Socket 类的构造函数试图将客户端连接到指定的服务器和端口号。如果通信被建立,则在客户端创建一个 Socket 对象能够与服务器进行通信。
5.在服务器端,accept() 方法返回服务器上一个新的 socket 引用,该 socket 连接到客户端的 socket。
6.连接建立后,通过使用 I/O 流在进行通信,每一个socket都有一个输出流和一个输入流,客户端的输出流连接到服务器端的输入流,而客户端的输入流连接到服务器端的输出流。
TCP 是一个双向的通信协议,因此数据可以通过两个数据流在同一时间发送.以下是一些类提供的一套完整的有用的方法来实现 socket。
案例1:TCP实现聊天
客户端:
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket=null;
OutputStream os=null;
try {
//1.要知道服务器的地址,端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port=9999;
//2.创建一个socket连接
socket = new Socket(serverIP, port);
//3.发送消息 IO流
os= socket.getOutputStream();
os.write("你好,欢迎学习JAVA".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器端:
public class TcpServerDemo01 {
public static void main(String[] args) {
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream baos=null;
try {
//1.先有一个地址
ServerSocket serverSocket = new ServerSocket(9999);
while (true){
//2.等待客户端连接过来
socket = serverSocket.accept();
//3.读取客户端的信息
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
案例二:TCP文件上传
客户端:
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception{
//1.创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis=new FileInputStream(new File("1.png"));
//4.写文件
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//通知服务器,我已经结束了
socket.shutdownOutput();
//确定服务器接受完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
//5关闭资源
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
服务器端:
public class TcpServerDemo02 {
public static void main(String[] args) throws Exception{
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直
//3.获取输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("2.png"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//通知客户端我接受完了
OutputStream os = socket.getOutputStream();
os.write("我接受完毕了,你可以断开了".getBytes());
//5关闭资源
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
通过url来下载网络上的歌曲
public class urlDown {
public static void main(String[] args) throws Exception{
//1.下载地址
URL url = new URL("https://m701.music.126.net/20211026134147/91627e411f3c28e0ed949545a5763b0c/jdyyaac/0452/040f/020b/3824c057663ec7e7feda5aa256cc0344.m4a");
//2.连接到这个资源HTTP
URLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("4.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
((HttpURLConnection) urlConnection).disconnect();
}
}


