完成网络通信的两点要素
*:
- 确定通信双方的地址:InetAddress
-
> 域名:www.atguigu.com
-
> IP 地址:116.117.158.48
- 若需要可靠高效的完成数据的传输,必须满足一定的规则,即网络通信协议(TCP、UDP)
package atguigu.day28.internet;
import java.net.InetAddress;
import java.net.UnknownHostException;
/*
* 一、完成网络通信的两点要素:
*
* 1. 确定通信双方的地址:InetAddress
* > 域名:www.atguigu.com
* > IP 地址:116.117.158.48
*
* 2. 若需要可靠高效的完成数据的传输,必须满足一定的规则,即网络通信协议(TCP、UDP)
*/
public class InetAddressTest {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddresses = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddresses);
// InetAddress inet2 = InetAddress.getByName("116.117.158.48");
// System.out.println(inet2);
InetAddress host = InetAddress.getLocalHost();
System.out.println(host);
String name = inetAddresses.getHostName();
String address = inetAddresses.getHostAddress();
System.out.println(name);
System.out.println(address);
}
}
套接字的使用
package atguigu.day28.internet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class TCPDemo {
/*
利用套接字(Socket)开发网络应用程序早已被广泛的采用,以至于成为事实上的标准。
通信的两端都要有Socket,是两台机器间通信的端点
网络通信其实就是Socket间的通信。
Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输。
*/
//客户端
@Test
public void client(){
String str = "hahahaha";
Socket socket = null;
OutputStream os = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 6666);
os = socket.getOutputStream();
os.write(str.getBytes());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
/*
服务器程序的工作过程包含以下四个基本的步骤:
调用 ServerSocket(int port) :创建一个服务器端套接字,并绑定到指定端口上。用于监听客户端的请求。
调用 accept():监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字对象。
调用 该Socket类对象的 getOutputStream() 和 getInputStream ():获取输出流和输入流,开始网络数据的发送和接收。
关闭ServerSocket和Socket对象:客户端访问结束,关闭通信套接字。
*/
@Test
public void Server(){
ServerSocket ss = null;
// 监听并且获取当前连接的那个socket
Socket accept = null;
InputStream is = null;
try {
ss = new ServerSocket(6666);
accept = ss.accept();
is = accept.getInputStream();
byte[] b = new byte[1024];
int len =0 ;
while((len = is.read(b)) != -1){
System.out.println(new String(b,0,len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (accept != null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务与客户端 1(TCP)
package atguigu.day28.internet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
/*
* 1.客户端发送内容给服务端,服务端将内容打印到控制台上。
*/
public class TCPTest1 {
//客户端
@Test
public void client(){
String str = "哈哈那个大大大";
Socket socket = null;
OutputStream os = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 6666);
os = socket.getOutputStream();
os.write(str.getBytes());
}catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void Server(){
//1. 调用 ServerSocket(int port) :创建一个服务器端套接字,并绑定到指定端口上。
ServerSocket ss = null;
//2调用 accept():监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字对象。
Socket accept = null;
//3调用 该Socket类对象的 getOutputStream() 和 getInputStream ():获取输出流和输入流,
InputStream is = null;
try {
ss = new ServerSocket(6666);
accept = ss.accept();
is = accept.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1){
System.out.println(new String(b,0 ,len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (accept !=null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端与客户端 2(TCP)
package atguigu.day28.internet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
/*
* 2.客户端发送内容给服务端,服务端给予反馈。
*/
public class TCPTest2 {
//客户端
@Test
public void client(){
String str = "土豆,土豆,我是地瓜,收到请回答!";
//创建 Socket:根据指定服务端的 IP 地址或端口号构造 Socket 类对象。
Socket socket = null;
//打开连接到 Socket 的输入/出流: 使用 getInputStream()方法获得输入流,使用 getOutputStream()方法获得输出流,进行数据传输
OutputStream os = null;
//接收服务端的反馈
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 6666);
os = socket.getOutputStream();
os.write(str.getBytes());
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b = new byte[1024];
int len =0;
while ((len = is.read(b)) != -1 ){
System.out.println(new String(b,0,len));
}
}catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void Server(){
//创建一个服务器端套接字,并绑定到指定端口上
ServerSocket ss = null;
//监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字对象。
Socket accept = null;
//获取输出流和输入流,开始网络数据的发送和接收。
InputStream is = null;
//向客户端发送 反馈
OutputStream os = null;
try {
ss = new ServerSocket(6666);
accept = ss.accept();
is = accept.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1){
System.out.println(new String(b,0,len));
}
//读完 告诉客户端
accept.shutdownInput();
os = accept.getOutputStream();
os.write("地瓜收到!地瓜收到!".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (accept != null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端与服务端 3(TCP)
package atguigu.day28.internet;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
/*
* 3.从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
*/
public class TCPTest3 {
//客户端
@Test
public void client(){
// 读取本地文件
//2. 通过 Socket 获取输出流,完成网络间数据的传输
Socket socket = null;
OutputStream os = null;
//3. 获取输入流,接收反馈
InputStream is = null;
try {
FileInputStream file = new FileInputStream("C:/Users/a/Desktop/1.jpg");
byte[] b = new byte[1024];
int len =0;
socket = new Socket(InetAddress.getByName("127.1.1.1"), 6666);
os = socket.getOutputStream();
while((len = file.read(b)) != -1){
os.write(b, 0, len);
}
//通知服务端发送完毕
socket.shutdownOutput();
is = socket.getInputStream();
while ((len = is.read(b)) != -1){
System.out.println(new String(b,0,len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void Server(){
//创建一个服务器端套接字,并绑定到指定端口上
ServerSocket ss = null;
//监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字对象。
Socket accept = null;
//1. 获取客户端的数据
InputStream is = null;
//2. 将数据保存到本地磁盘中
FileOutputStream fo = null;
//3. 发送反馈给客户端
OutputStream os = null;
try {
ss = new ServerSocket(6666);
accept = ss.accept();
is = accept.getInputStream();
byte[] b = new byte[1024];
int len = 0;
fo = new FileOutputStream("C:/Users/a/Desktop/2.jpg");
while((len = is.read(b)) != -1){
fo.write(b, 0, len);
}
accept.shutdownInput();
os = accept.getOutputStream();
os.write("接收数据成功!".getBytes());
}catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fo != null){
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (accept != null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
UDP练习
package atguigu.day28.internet;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import org.junit.Test;
public class UDPTest {
// 发送端
@Test
public void send() throws Exception {
String str = "使用 UDP 发送数据给接收端";
DatagramSocket ds = new DatagramSocket();
byte[] buf = str.getBytes();
int i = 0;
while (i < 100) {
// 注意:该数据包大小必须控制在 64kb 内
DatagramPacket dp = new DatagramPacket(buf, 0, buf.length, InetAddress.getByName("127.0.0.1"), 6666);
ds.send(dp);
System.out.println("发送端发送了" + i++ + "个数据包");
}
ds.close();
}
// 接收端
@Test
public void receive() throws Exception {
DatagramSocket ds = new DatagramSocket(6666);
byte[] b = new byte[1024];
int i = 0;
while (i < 100) {
DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
System.out.println(new String(dp.getData(), 0, dp.getLength()));
System.out.println("接收端接收了" + i++ + "个数据包");
}
ds.close();
}
}
URL练习
package atguigu.day28.internet;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLTest {
public static void main(String[] args) {
InputStream inputStream = null;
FileOutputStream fs = null;
try {
URL url = new URL("https://bing.ioliu.cn/?p=5");
URLConnection open = url.openConnection();
inputStream = open.getInputStream();
fs = new FileOutputStream("D:\\atguigu\\iotest\\test");
byte[] b = new byte[1024];
int len = 0;
while ((len = inputStream.read(b)) != -1){
fs.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fs != null){
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}