概述
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、概述
192.168.x.x主要为局域网ip
二、网络通信要素
192.168.x.x主要为局域网ip
1.引入库
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
2.读入数据
代码如下(示例):
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
该处使用的url网络请求的数据。
三、IP
try {
InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getLocalHost();
System.out.println(inetAddress2);
System.out.println(inetAddress1.getAddress());
System.out.println(inetAddress1.getCanonicalHostName());
System.out.println(inetAddress1.getHostAddress());
System.out.println(inetAddress1.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
四、端口
端口范围:0-65535(2的32次方)
TCP、UDP:都有65535个端口
公有端口:0-1023
HTTP:80、HTTPS:443、FTP:21、Telent:23
程序注册端口:1024-49151
Oracle:1521
动态私有的端口:49152-65535
netstat -ano #查看端口
netstat -ano|findstr "80" #查看特定端口
tasklist|findstr "80" #查看制定端口进程
五、通信协议
六、TCP
客户端:
//1.链接服务器Socket 2.发送消息
try {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
int port = 6666;
Socket socket = new Socket(inetAddress,port);
OutputStream os = socket.getOutputStream();
os.write("hello server".getBytes());
os.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
服务端
//服务端:1.链接服务端口 ServerSocket 2.等待用户链接 accept 3.接受用户消息
try {
ServerSocket serverSocket = new ServerSocket(6666);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
ByteArrayOutputStream 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());
baos.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
Tomcat
服务端
自定义S Tomcat服务器S
客户端
自定义C 浏览器B
七、UDP
发送端
DatagramSocket datagramSocket = new DatagramSocket();
//创建包
String msg = "hello world";
int port = 6666;
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,inetAddress,port);
datagramSocket.send(datagramPacket);
datagramSocket.close();
接收端
DatagramSocket datagramSocket = new DatagramSocket(6666);
byte[] buffer = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(buffer, 0, buffer.length);
//阻塞接受
datagramSocket.receive(datagramPacket);
System.out.println(datagramPacket.getAddress().getHostName());
System.out.println(new String(datagramPacket.getData(), 0, datagramPacket.getLength()));
datagramSocket.close();
多线程UDP聊天
//发送端
package com.kuangshen.javaweb;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpTalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public UdpTalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket=new DatagramSocket(this.fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, InetAddress.getByName(this.toIP), this.toPort);
socket.send(packet);
if (data.equals("bye")){
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
//接收端
package com.kuangshen.javaweb;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpTalkRead implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public UdpTalkRead(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket=new DatagramSocket(this.port);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(msgFrom+":"+receiveData);
if (receiveData.equals("bye")){
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
//测试类
package com.kuangshen.javaweb;
public class TalkStudent {
public static void main(String[] args) {
new Thread(new UdpTalkSend(7777,"localhost",9999)).start();
new Thread(new UdpTalkRead(8888,"teacher")).start();
}
}
package com.kuangshen.javaweb;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new UdpTalkSend(5555,"localhost",8888)).start();
new Thread(new UdpTalkRead(9999,"student")).start();
}
}
url获取资源
URL url = new URL("https://m701.music.126.net/20210310113238/2a09d1811b01973a5b5e983f1e80a63c/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/7693264623/6e68/db0e/b090/dc2bd87a4e0b4de39a11c345fb7373d3.m4a");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("music.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();