1、 网络的相关概念
1.1、 网络通信
概念两台设备之间通过网络实现数据传输 网络通信将数据通过网络从一台设备传输到另外一台设备 java.net包下提供了一系列的类和接口,供程序员使用,完成网络通信
1.2、网络
概念两台或多台设备之间通过一定物理设备连接起来构成网络
1.3、ip地址
概念用来表示网络中的唯一一台计算机
2、 InetAddress 类
2.1、相关方法
(1)getLocalHost() 获取本机InteAddress对象 (2)getByName()根据指定主机名/域名获取ip地址对象 (3)getHostName() 获取InetAddress对象的主机名 (4)getHostAddress()获取InetAddress对象的地址
package com.francis.api;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddress_ {
public static void main(String[] args) throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
InetAddress host2 = InetAddress.getByName("Francis");
System.out.println(host2);
InetAddress host3 = InetAddress.getByName("www.baidu.com");
System.out.println(host3);
String host3Name = host3.getHostName();
System.out.println(host3Name);
String host3Address = host3.getHostAddress();
System.out.println(host3Address);
}
}
3、Socket
3.1、基本介绍
(1)套接字(Socket)开发网络应用程序被广泛应用,以至于称为事实上的标准 (2)通信的两端都要有Socket,是两台机器通信的断点 (3)Socket允许把网络连接当成一个流,数据在两个Socket间通过IO传输 (4)一般主动发起通信的一端称为客户端,等待通信请求的一端称为服务端
4、TCP 网络通信编程
4.1、基本介绍
(1)基于客户端-服务端的网络通信 (2)底层使用的是TCP/IP协议 (3)基于TCP的socket编程
4.2、应用案例 1(使用字节流)
(1) 编写一个服务端、一个客户端 (2)服务器在9999端口监听 (3)客户端连接到服务器,发送“hello server”,然后退出 (4)服务端接受到客户端发送的消息,输出并退出
package com.francis.socket;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端在9999端口监听,等待连接");
Socket socket = serverSocket.accept();
System.out.println("服务端 socket=" + socket.getClass());
InputStream inputStream = socket.getInputStream();
byte[] buff = new byte[1024];
int readLen = 0;
String content = "";
while ((readLen = inputStream.read(buff)) != -1) {
System.out.println(new String(buff, 0, readLen));
content += new String(buff, 0, readLen);
}
System.out.println(content);
inputStream.close();
socket.close();
serverSocket.close();
}
}
package com.francis.socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端 socket 返回="+socket.getClass());
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello server".getBytes());
outputStream.close();
socket.close();
}
}
4.3、应用案例 2(使用字节流)
(1)编写一个服务端、一个客户端 (2)服务器在9999端口监听 (3)客户端连接到服务器,发送“hello server”,并接收服务器发回的“hello client”,然后退出 (4)服务端接受到客户端发送的消息,输出,并向客户端发送“hello client” ,然后退出
package com.francis.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketTCP02Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务器端正在监听9999端口");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] buff = new byte[1024];
int readLen = 0;
while((readLen = inputStream.read(buff))!=-1){
System.out.println(new String(buff,0,readLen));
}
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello client".getBytes());
socket.shutdownOutput();
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
package com.francis.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class SocketTCP02Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLoopbackAddress(), 9999);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello server" .getBytes());
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
byte[] buff = new byte[1024];
int readLen = 0;
while((readLen = inputStream.read(buff))!=-1){
System.out.println(new String(buff,0,readLen));
}
inputStream.close();
outputStream.close();
socket.close();
}
}
4.4、 应用案例 3(使用字符流)
(1)编写一个服务端、一个客户端 (2)服务器在9999端口监听 (3)客户端连接到服务器,发送“hello server 字符流”,并接收服务器发回的“hello client”,然后退出 (4)服务端接受到客户端发送的消息,输出,并向客户端发送“hello client 字符流” ,然后退出
package com.francis.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketTCP03Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在 9999 端口监听,等待连接..");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello client 字符流");
bufferedWriter.newLine();
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
socket.close();
serverSocket.close();
}
}
package com.francis.socket;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketTCP03Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端 socket 返回="+socket.getClass());
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello server 字符流");
bufferedWriter.newLine();
bufferedWriter.flush();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
inputStream.close();
outputStream.close();
socket.close();
}
}
4.5、复制图片
(1)编写一个服务端和客户端 (2)服务端监听8888端口 (3)服务器端接收到客户端发送的图片,保存到src下,发送“收到图片”再退出
package com.francis.socket.upload_picture;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TCPFileUploadClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
String filePath = "f:\\1.jpg";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
byte[] bytes = StreamUtils.streamToByteArray(bis);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(bytes);
bis.close();
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
String s = StreamUtils.streamToString(inputStream);
System.out.println(s);
inputStream.close();
bos.close();
socket.close();
}
}
package com.francis.socket.upload_picture;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPFileUploadServer {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务端在 8888 端口监听....");
Socket socket = serverSocket.accept();
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamToByteArray(bis);
String destFilePath = "f:\\111.jpg";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
bos.write(bytes);
bos.close();
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write("收到图片");
writer.flush();
writer.close();
bis.close();
socket.close();
serverSocket.close();
}
}
package com.francis.socket.upload_picture;
import com.sun.org.apache.bcel.internal.generic.NEW;
import java.io.*;
public class StreamUtils {
public static byte[] streamToByteArray(InputStream is) throws IOException, Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
bos.write(b, 0, len);
}
byte[] array = bos.toByteArray();
bos.close();
return array;
}
public static String streamToString(InputStream is) throws IOException, Exception {
BufferedReader reader =
new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String line;
while ((line
= reader.readLine()) != null) {
builder.append(line + "\r\n");
}
return builder.toString();
}
}
4.6、netstat指令
(1)netstat -an可以查看当前主机的网络情况,包括端口监听情况和网络连接情况 (2)netstat -an | more 可以分页显示
5、UDP 网络通信编程
5.1、基本介绍
(1)类DatagramSocket和DatagramPacket实现了基于UDP网络协议程序 (2)UDP数据报通过数据报套接字DatagramSocket发送和接收,系统不保证数据报一定能够安全的送达目的地,也不确定什么时候可以送达 (3)DatagramPacket对象封装了UDP数据报,在数据报中包含了发送端的IP地址和端口号以及接收端的ip地址和端口号 (4)UDP协议中每个数据报都给出了完整的地址信息,因此无需建立发送方和接接收方的连接
5.2、基本流程
5.3、UDP应用案例
package com.francis.socket.upload_picture.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPReceiverA {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9999);
byte[] buf = new byte[64*1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
System.out.println("接收端 A 等待接收数据..");
socket.receive(packet);
int length = packet.getLength();
byte[] data = packet.getData();
String s = new String(data, 0, length);
System.out.println(s);
data = "好的, 明天见".getBytes();
packet = new DatagramPacket(data, data.length, InetAddress.getByName(
"192.168.5.1"), 9998);
socket.send(packet);
socket.close();
System.out.println("A 端退出...");
}
}
package com.francis.socket.upload_picture.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSenderB {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9998);
byte[] data = "hello 明天吃火锅~".getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length,
InetAddress.getByName("192.168.5.1"), 9999);
socket.send(packet);
byte[] buf = new byte[1024];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
int length = packet.getLength();
data = packet.getData();
String s = new String(data, 0, length);
System.out.println(s);
socket.close();
System.out.println("B 端退出");
}
}
|