① IP 的解释
② InetAddress 类
此类的一个对象就代表着一个具体的 IP 地址
getByName(String host)、getLocalhost()
getHostName()/getHostAddress()
要求:不同的进程不同的端口号
范围:被规定为一个 16 位的整数 0~65535
端口号与 IP 地址的组合得出一个网络套接字:Socket
① 分类模型
② TCP 和 UDP 的区别
③ TCP 三次握手和四次挥手
④ 补充
// geByName 确定主机名称的IP地址
InetAddress.getByName("192.168.12.67");
// 获取本地IP
InetAddress.getLocalhost();
// 获取此IP地址的主机名。
inet2.getHostName()
// 返回文本显示中的IP地址字符串
inet2.getHostAddress()
代码示例:客户端发送信息给服务端,服务端将数据显示在控制台上
客户端:
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
try {
// 1.创建Socket对象,指明服务器的ip和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8899);
// 获取一个输出流,用于输出数据
os = socket.getOutputStream();
os.write("你好,我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端:
@Test
public void Server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// 1. 创建服务器=端的ServerSocket,指明自己的端口号
serverSocket = new ServerSocket(8899);
// 2.调用accept()表示接口来自客户端的socket
socket = serverSocket.accept();
// 3.获取输入流
is = socket.getInputStream();
// 4.读取输入流中的数据
baos = new ByteArrayOutputStream();
byte[] bt = new byte[1024];
int len;
while((len = is.read(bt)) != -1){
baos.write(bt, 0, len);
}
System.out.println(baos.toString());
System.out.println("收到了来自:"+socket.getInetAddress().getHostAddress()+"的数据");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(serverSocket != null){
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(baos != null){
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码示例 2:客户端发送文件给服务端,服务端将文件保存在本地。
客户端:
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 8899);
os = socket.getOutputStream();
fis = new FileInputStream("1.jpg");
byte[] bt = new byte[1024];
int len;
while((len = fis.read(bt)) != -1){
os.write(bt, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.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 socket = null;
InputStream is = null;
FileOutputStream fos = null;
try {
ss = new ServerSocket(8899);
socket = ss.accept();
is = socket.getInputStream();
fos = new FileOutputStream(new File("图片2.jpg"));
byte[] bt = new byte[1024];
int len;
while((len = is.read(bt)) != -1){
fos.write(bt, 0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
发送端:
@Test
public void sender(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
String str = "我是UDP方式发送的导弹";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9090);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
socket.close();
}
}
接收端:
@Test
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
}
统一资源定位符,对应着互联网的某一资源地址
URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
方法 | 描述 |
---|---|
public String getProtocol() | 获取该 URL 的协议名 |
public String getHost() | 获取 URL 的主机名 |
public String getPort() | 获取 URL 的端口号 |
public String getPath() | 获取 URL 的文件路径 |
public String getFile() | 获取 URL 的文件名 |
public String getQuery() | 获取 URL 的查询名 |
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("day10\\beauty3.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("下载完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(urlConnection != null){
urlConnection.disconnect();
}
}
}