网络编程中的要素:
1.IP和端口号。
2.网络通信协议:TCP/UDP。
代码示例:
public static void main(String[] args) throws UnknownHostException {
InetAddress byName = InetAddress.getByName("127.0.0.1");
System.out.println(byName);//返回结果:/127.0.0.1
InetAddress byName1 = InetAddress.getByName("www.baidu.com");
System.out.println(byName1);//返回结果:www.baidu.com/61.135.169.125
//获取ip
System.out.println(byName1.getHostAddress());
//获取域名
System.out.println(byName1.getHostName());
}
端口表示计算机上的一个程序的进程:
1.不同的进程有不同的端口号。
2.被规定 0~65535。
3.TCP/UDP 65535*2, TCP:80,UDP:80,单个协议下,端口号不能冲突。
4.端口分类:
公共端口:0~1023
HTTP:80
HTTPS:443
FTP:21
Telent:23
程序注册端口:1024~49151,分配用户或者程序。
Tomcat:8080
MySql:3306
Oracle:1521
动态、私有:49152~65535
TCP:
连接,稳定。
三次握手、四次挥手:最少需要三次,保证稳定连接。至少需要四次,保证断开。
客户端、服务端。
传输完成、释放连接、效率低。
UDP:
不连接,不稳定。
客户端、服务器:没有明确的界限。
客户端
1.连接服务器Socket
2.发送消息
服务器
1.建立服务的端口 ServerSocket
2.等待用的的连接 accept
3.接收消息
客户端代码示例:
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @ClassName TcpClient
* @Description 客户端
* @Author
* @Date 2020-06-15 10:43
* @Version 1.0
**/
public class TcpClient {
public static void main(String[] args) throws Exception {
InetAddress inetAddress = null;
Socket socket = null;
OutputStream outputStream = null;
try {
//调用服务端的地址
inetAddress = InetAddress.getByName("127.0.0.1");
//创建Socket连接
socket = new Socket(inetAddress, 9999);
outputStream = socket.getOutputStream();
outputStream.write("hello world".getBytes());
outputStream.close();
} 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();
}
}
}
}
}
服务端代码示例:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @ClassName TcpServer
* @Description 服务端
* @Author ywl
* @Date 2020-06-15 10:43
* @Version 1.0
**/
public class TcpServer {
public static void main(String[] args){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try{
//设置地址
serverSocket = new ServerSocket(9999);
//连接客户端
accept = serverSocket.accept();
//读取客户端消息
inputStream = accept.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
System.out.println(byteArrayOutputStream.toString());
}
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭资源
if(byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(accept != null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
代码示例:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @ClassName UdpClient
* @Description Udp
* @Author
* @Date 2020-06-15 13:12
* @Version 1.0
**/
public class UdpClient {
public static void main(String[] args) {
DatagramSocket datagramSocket = null;
try {
//创建一个Socket
datagramSocket = new DatagramSocket();
//发送
InetAddress inetAddress = InetAddress.getByName("localhost");
//创建Packet
DatagramPacket datagramPacket = new DatagramPacket("hello word!您好".getBytes(), 0, "hello word!您好".getBytes().length, inetAddress, 9090);
//发送Packet
datagramSocket.send(datagramPacket);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(datagramSocket != null){
datagramSocket.close();
}
}
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @ClassName UdpServer
* @Description TODO
* @Author
* @Date 2020-06-15 13:20
* @Version 1.0
**/
public class UdpServer {
public static void main(String[] args) {
try {
//开放端口
DatagramSocket datagramSocket = new DatagramSocket(9090);
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(datagramPacket);
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
datagramSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
发送工具类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
* @ClassName TalkSendUtils
* @Description UDP发送
* @Author
* @Date 2020-06-15 13:50
* @Version 1.0
**/
public class TalkSendUtils implements Runnable{
private DatagramSocket socket = null;
private BufferedReader reader = null;
private int fromPort;
private String toIp;
private int toPort;
public TalkSendUtils(int fromPort, String toIp, int toPort) {
this.fromPort = fromPort;
this.toIp = toIp;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while(true){
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIp, this.toPort));
socket.send(packet);
if(data.equals("bye")){
break;
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
接受工具类:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* @ClassName TalkReceiveUtils
* @Description 接受工具类
* @Author
* @Date 2020-06-15 14:02
* @Version 1.0
**/
public class TalkReceiveUtils implements Runnable{
private DatagramSocket socket = null;
private int port;
private String fromName;
public TalkReceiveUtils(int port,String fromName) {
this.port = port;
this.fromName = fromName;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true){
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(packet);
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(fromName+":"+receiveData);
if (receiveData.equals("bye")){
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试类:
public static void main(String[] args) {
new Thread(new TalkSendUtils(7777,"localhost",9999)).start();
new Thread(new TalkReceiveUtils(8888,"老师")).start();
}
public static void main(String[] args) {
new Thread(new TalkSendUtils(5555,"localhost",8888)).start();
new Thread(new TalkReceiveUtils(9999,"学生")).start();
}
统一资源定位符。
代码示例如下:
public static void main(String[] args) {
try {
URL url = new URL("https://eu-sycdn.kuwo.cn/0a79505fd846f2b6f1019dd2086d38dd/5ee723f9/resource/n2/22/35/1153965333.mp3");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("E:\\1153965333.mp3");
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1){
fileOutputStream.write(bytes,0,len);
}
fileOutputStream.close();
inputStream.close();
urlConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}