网络编程概述
网络通信要是概述
通信要素1:IP和端口号
IP
try {
InetAddress inetAddress = InetAddress.getByName("192.186.0.1");
System.out.println(inetAddress);
InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getLocalHost();
System.out.println(inetAddress2);
InetAddress inetAddress3 = InetAddress.getLoopbackAddress();
System.out.println(inetAddress3);
} catch (UnknownHostException e) {
e.printStackTrace();
}
端口号
端口号标识正在计算机上运行的进程(程序)
- 不同的进程有不同的端口号
- 被规定为一个16位的整数0~65535
- 端口分类:
- 公认端口:0~1023(被预先定义的服务通信占用)。HTTP占用端口80,FTP专用端口21,Telnet占用端口23
- 注册端口:1024~49151。分配给用户进程或应用程序。(如:Tomcat占用端口8080,MySQL占用端口3306,Oracle占用端口1521)
- 动态/私有端口:49152~65535
- 端口号与IP地址组合得出一个网络套接字:Socket
通信要素2:网络协议
- TCP传输控制协议:可靠的数据传输(三次握手),进行大量数据量的传输,效率低
- UDP用户数据报协议:不可靠,以数据报形式发送,数据报限定为64k,效率高
TCP网络编程
TCP传输控制协议
例子1:客户端发送信息给服务器端,服务器端将数据显示在控制台上
package com.net.TCPTest;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 例子1:客户端发送信息给服务器端,服务器端将数据显示在控制台上
*
* @author Chen
*/
public class TCPTest1 {
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
try {
// 1、创建socket对象,指明服务器端的IP和端口号
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress, 8899);
// 2、获取一个输出流,用于输出数据
outputStream = socket.getOutputStream();
// 3、写出数据的操作
outputStream.write("你好,我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 服务器端
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
try {
// 1、创建服务器端的ServerSocket,指明自己端口号
serverSocket = new ServerSocket(8899);
// 2、调用accept()方法,接收来自客户端的socket
socket = serverSocket.accept();
// 3、获取输入流
inputStream = socket.getInputStream();
// 4、读取输入流当中的数据
// 不建议这样写。可能有乱码
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
String string = new String(buffer, 0, len);
System.out.println(string);
}
System.out.println("收到了来自于" + socket.getInetAddress().getHostAddress() + "的信息");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
例子2:客户端发送文件给服务器端,服务器端将文件保存在本地
package com.net.TCPTest;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 例子2:客户端发送文件给服务器端,服务器端将文件保存在本地
*
* @author Chen
*/
public class TCPTest2 {
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
try {
// 1、创建socket对象,指明服务器端的IP和端口号
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress, 9090);
// 2、获取一个输出流,用于输出数据
outputStream = socket.getOutputStream();
// 3、读本地文件
fileInputStream = new FileInputStream(new File("src\\com\\net\\TCPTest\\进击的巨人.png"));
// 4、读写数据的操作
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 服务器端
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
// 1、创建服务器端的ServerSocket,指明自己端口号
serverSocket = new ServerSocket(9090);
// 2、调用accept()方法,接收来自客户端的socket
socket = serverSocket.accept();
// 3、获取输入流
inputStream = socket.getInputStream();
// 4、保存文件在本地
fileOutputStream = new FileOutputStream(new File("src\\com\\net\\TCPTest\\进击的巨人1.png"));
// 4、读取输入流当中的数据
// 不建议这样写。可能有乱码
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
System.out.println("收到了来自于" + socket.getInetAddress().getHostAddress() + "的信息");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
例子3:客户端发送文件给服务器端,服务器端将文件保存在本地,并返回“发送成功”给客户端。并关闭相应的连接。
package com.net.TCPTest;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 例子3:客户端发送文件给服务器端,服务器端将文件保存在本地,并返回“发送成功”给客户端。并关闭相应的连接。
*
* @author Chen
*/
public class TCPTest3 {
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
InputStream inputStream = null;
try {
// 1、创建socket对象,指明服务器端的IP和端口号
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress, 9090);
// 2、获取一个输出流,用于输出数据
outputStream = socket.getOutputStream();
// 3、读本地文件
fileInputStream = new FileInputStream(new File("src\\com\\net\\TCPTest\\进击的巨人.png"));
// 4、读写数据的操作
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
// 注意阻塞:关闭数据的输出,才会执行下面的5
socket.shutdownOutput();
// 5、接收来自服务器端的数据,并显示到控制台上
inputStream = socket.getInputStream();
byte[] buffer1 = new byte[1024];
int len1;
while ((len1 = inputStream.read(buffer1)) != -1) {
String s = new String(buffer1, 0, len1);
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 服务器端
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
OutputStream outputStream = null;
try {
// 1、创建服务器端的ServerSocket,指明自己端口号
serverSocket = new ServerSocket(9090);
// 2、调用accept()方法,接收来自客户端的socket
socket = serverSocket.accept();
// 3、获取输入流
inputStream = socket.getInputStream();
// 4、保存文件在本地
fileOutputStream = new FileOutputStream(new File("src\\com\\net\\TCPTest\\进击的巨人1.png"));
// 5、读取输入流当中的数据
// 不建议这样写。可能有乱码
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
System.out.println("收到了来自于" + socket.getInetAddress().getHostAddress() + "的信息");
// 6、服务器端给客户端的反馈
outputStream = socket.getOutputStream();
outputStream.write("你好,照片已经收到!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
UDP网络编程
UDP用户数据报协议
例子1:发送端向接收端发送数据
package com.net.UDPTest;
import org.junit.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/** @author Chen */
public class UDPTest {
// 发送端
@Test
public void sender() throws IOException {
// 1、创建DatagramSocket
DatagramSocket datagramSocket = new DatagramSocket();
// 2、发送的数据
byte[] data = "我是UDP方式发送的导弹".getBytes();
InetAddress localHost = InetAddress.getLocalHost();
// 3、创建DatagramPacket,指明发送的数据,地址,端口号
DatagramPacket datagramPacket = new DatagramPacket(data, 0, data.length, localHost, 9090);
// 4、发送send()
datagramSocket.send(datagramPacket);
// 5、关闭资源
datagramSocket.close();
}
// 接收端
@Test
public void receiver() throws IOException {
// 1、创建DatagramSocket,指明端口号
DatagramSocket socket = new DatagramSocket(9090);
// 2、创建DatagramPacket,用于存放接收到的数据
byte[] buffers = new byte[100];
DatagramPacket datagramPacket = new DatagramPacket(buffers, 0, buffers.length);
// 3、接收数据,并存放于创建DatagramPacket中
socket.receive(datagramPacket);
// 4、输出创建DatagramPacket中的内容
System.out.println(new String(datagramPacket.getData(), 0, datagramPacket.getLength()));
// 5、关闭资源
socket.close();
}
}
URL编程
下载图片
package com.net.URLTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/** @author Chen */
public class URLTest1 {
public static void main(String[] args) {
//
HttpURLConnection urlconnection = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url =
new URL(
"https://image.baidu.com/search/detail?ct=50331xxxxxxxxxxxxxxxxx");
urlconnection = (HttpURLConnection) url.openConnection();
// 连接
urlconnection.connect();
// 获取流
inputStream = urlconnection.getInputStream();
fileOutputStream = new FileOutputStream("src/com/net/URLTest/风景.jpeg");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
System.out.println("下载完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (urlconnection != null) {
urlconnection.disconnect();
}
}
}
}