教学视频来源于https://www.bilibili.com/video/BV1LJ411z7vY?from=search&seid=6811206386140487034共12集
所有代码均在IDEA上成功运行。
一、入门
Java1.8提供的网络地址api:InetAdress
cmd常用命令:
netstat -ano #查看所有的端口
netstat -ano|findstr "5900" #查看指定的端口
tasklist|findstr "8696" #查看指定端口的进程
Ctrl+Shift+Esc #打开 任务管理器
1.TCP模拟聊天
客户端:
1.连接服务器Socket
2.发送消息
package com.kuang.lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) throws IOException {
//1.要知道服务器的地址
InetAddress severIp = InetAddress.getByName("127.0.0.1");
//2.创建一个socket连接
int port = 10011;
Socket socket = new Socket(severIp, port);
//3.发送消息IO流
OutputStream os = socket.getOutputStream();
os.write("我是中国人".getBytes());
//4.关闭端口
if (os != null){
os.close();
}
if (socket != null){
socket.close();
}
}
}
服务端:
1.建立服务端的端口ServerSocket
2.等待用户的连接 accept
3.接收用户的消息
package com.kuang.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServerDemo01 {
public static void main(String[] args) throws IOException {
//1.我得有一个地址
ServerSocket serverSocket = new ServerSocket(10011);
//2.等待客户端连接过来
Socket socket = serverSocket.accept();//监听
//3.读取客户端的消息
InputStream is = socket.getInputStream();
//4.管道流
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());
//5.关闭端口
if (baos != null){
baos.close();
}
if (is != null){
is.close();
}
if (socket != null){
socket.close();
}
if (serverSocket != null){
serverSocket.close();
}
}
}
2.TCP文件上传
服务端:
package com.kuang.lesson02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServerDemo02 {
public static void main(String[] args) throws IOException {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//3.获取输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));//文件管道流
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
}
//通知客户端我接受完毕了
OutputStream os = socket.getOutputStream();
os.write("我接受完毕了,你可以断开了".getBytes());
//5.关闭资源
fos.close();
is.close();
socket.close();
}
}
客户端:
package com.kuang.lesson02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
//客户端
public class TcpClientDemo02 {
public static void main(String[] args) throws IOException {
//1.创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream(new File("img.jpg"));
//4.写出文件
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){ // 规范写法
os.write(buffer, 0, len);
}
//通知服务器。我已经结束了
socket.shutdownOutput();
//确定服务端接收完毕,才能断开连接
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//字节管道流
byte[] buffer2 = new byte[1024];
int len2;
while((len2 = is.read(buffer2)) != -1){ // 规范写法
baos.write(buffer2, 0, len2);
}
System.out.println(baos.toString());
//5.关闭资源
baos.close();
fis.close();
os.close();
socket.close();
}
}
3.Tomcat
服务端:
客户端:
4.UDP发送消息
发送端:
package com.kuang.lesson03;
import java.io.IOException;
import java.net.*;
//不需要连接服务器
public class UdpClientDemo01 {
public static void main(String[] args) throws IOException {
//1.建立一个数据报Socket
DatagramSocket socket = new DatagramSocket();
//2.建个数据报包
String msg = "你好呀,服务器!" ;
//发送的IP地址,端口号
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//数据,数据的首,尾,IP,端口
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//3.发送数据报包
socket.send(packet);
//4.关闭流
socket.close();
}
}
接收端:
package com.kuang.lesson03;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;
public class UdpServerDemo01 {
public static void main(String[] args) 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(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(), 0, packet.getData().length));
//关闭流
socket.close();
}
}
5.UDP聊天
发送端:
package com.kuang.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpSenderDemo01 {
public static void main(String[] args) throws IOException {
//设置当前端口为8888
DatagramSocket socket = new DatagramSocket(8888);
//准备数据,控制台读取system.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
String data = reader.readLine();
byte[] datas = data.getBytes();
InetAddress ip = InetAddress.getByName("localhost");
int port = 6666;//目标端口号
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, ip, port);
socket.send(packet);
if(data.equals("bye")){
socket.close();
}
}
}
}
接收端:
package com.kuang.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemo01 {
public static void main(String[] args) throws IOException {
//设置当前端口为6666
DatagramSocket socket = new DatagramSocket(6666);
while(true){
//准备接收数据
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);//阻塞式接收包裹
byte[] data = packet.getData();
String s = new String(data, 0, data.length);
System.out.println(s);
if(s.equals("bye")){
break;
}
}
socket.close();
}
}
6.UDP多线程在线咨询(双向聊天)
学生线程:
package com.kuang.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(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 (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
String data = null;
try {
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")){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
老师线程:
package com.kuang.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);//阻塞式接收包裹
byte[] data = packet.getData();
String s = new String(data, 0, data.length);
System.out.println(msgFrom + ":" + s);
if(s.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
学生端启动线程:
package com.kuang.chat;
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777, "localhost", 9999)).start();
new Thread(new TalkReceive(8888, "老师")).start();
}
}
老师端启动线程:
package com.kuang.chat;
public class TalkTeacher {
public static void main(String[] args) {
//开启两个线程,代理模式:使用Thread()代理了Runnerable接口
new Thread(new TalkSend(5555, "localhost", 8888)).start();
new Thread(new TalkReceive(9999, "学生")).start();
}
}
7.URL统一资源定位符
协议://ip地址:端口号/项目名/资源
|