1、网络编程中了解的概念
主机 :具有通信能力的设备
IP :主机在网络中的唯一标识
端口 :通信设备在网络中交换数据的口 端口范围:1~65535
通讯协议 :通信的规范 TCP http(s) ftp:文件传输协议 smtp pop3 imap rtmp udp
主机:具有通信能力的设备
IP:主机在网络中的唯一标识
端口:通信设备在网络中交换数据的口,端口范围:1~65535
通讯协议:即通信的规范,有:
tcp(传输控制协议):一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793 [1] 定义。
http(超文本传输协议):是一个简单的请求-响应协议,它通常运行在TCP之上。它指定了客户端可能发送给服务器什么样的消息以及得到什么样的响应。请求和响应消息的头以ASCII形式给出;而 [9] 消息内容则具有一个类似MIME的格式。
smtp(电子邮件传输的协议):SMTP是建立在FTP文件传输服务上的一种邮件服务,主要用于系统之间的邮件信息传递,并提供有关来信的通知。
ftp(文件传输协议):TCP/IP 协议组中的协议之一。FTP协议包括两个组成部分,其一为FTP服务器,其二为FTP客户端。
pop3(邮局协议版本3):TCP/IP协议族中的一员,由RFC1939 定义。本协议主要用于支持使用客户端远程管理在服务器上的电子邮件。提供了SSL加密的POP3协议被称为POP3S。
IMAP(交互邮件访问协议,权威称呼:RFC3501):IMAP协议运行在TCP/IP协议之上,使用的端口是143。它与POP3协议的主要区别是用户可以不用把所有的邮件全部下载,可以通过客户端直接对服务器上的邮件进行操作。
rtmp(实时消息传输协议):该协议基于TCP,是一个协议族,包括RTMP基本协议及RTMPT/RTMPS/RTMPE等多种变种。RTMP是一种设计用来进行实时数据通信的网络协议,主要用来在Flash/AIR平台和支持RTMP协议的流媒体/交互服务器之间进行音视频和数据通信。
udp(用户数据报协议):UDP 为应用程序提供了一种无需建立连接就可以发送封装的 IP 数据包的方法。
补充:Internet 的传输层有两个主要协议,互为补充。无连接的是 UDP,它除了给应用程序发送数据包功能并允许它们在所需的层次上架构自己的协议之外,几乎没有做什么特别的事情。面向连接的是 TCP,该协议几乎做了所有的事情。
2、开发
tcp
ServerSocket
服务套接字Socket
通信套接字
3、窗体:使用IO流,多线程,网络编程
1.Idea中创建窗体的选项:
3.1、同局域网下聊天(使用线程)
public class TestA {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
// 被叫方
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("正在等待别人的连接。。。。。。。。");
// 等待连接 阻塞
Socket accept = serverSocket.accept();//接受
accept.getRemoteSocketAddress(); // 获取连接人的信息
System.out.println(accept.getRemoteSocketAddress());
System.out.println("有人连接了我。。");
InputStream inputStream = accept.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
OutputStream outputStream = accept.getOutputStream();
PrintWriter out = new PrintWriter(outputStream);
new shou(bufferedReader).start();
out.println("hello java aafa///");
while (true){
String msg = scanner.nextLine();
out.println(msg);
out.flush();
System.out.println(bufferedReader.readLine());
}
/*out.flush();
out.close();*/
}
}
public class TestB {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
// 呼叫方
Socket socket = new Socket("192.168.1.6", 8080);
// 输入流
InputStream inputStream = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
System.out.println(in.readLine());
//in.close();
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
new shou(in).start();
while (true){
String line = in.readLine();
System.out.println(line);
String msg = scanner.nextLine();
printWriter.println(msg);
printWriter.flush();
}
}
}
public class shou extends Thread{
BufferedReader in;
public shou(BufferedReader in) {
this.in = in;
}
}
3.2、练习2:使用窗体实现简单的加法
package com.bigdata09.demo1;
// swing 用于窗体开发的包
import javax.swing.*;
// awt : abstract window tool 抽象窗体工具包
import java.awt.*;
/**
* @Created with IDEA 2019
* @package:com.bigdata09.demo1
* @project_name:idea-java
* @author:杨磊
* @Date:2022/8/8
* @Time:8:39
**/
public class TestFrom extends JFrame {
private BorderLayout borderLayout;
private Container container;
private JTextField jTextField;
private JTextField jTextField2;
// button按钮
private JButton jButton;
// 标签
private JLabel jLabel1;
private JLabel jLabel2;
public TestFrom() throws HeadlessException {
//setSize:窗体大小
this.setSize(300,400);
// getDefaultToolkit : 默认操作系统的工具包
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = defaultToolkit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
int fwidth = (int) (width*(3.0/5));
int fheight = (int) (height*(3.0/5));
this.setSize(fwidth,fheight);
// setLocation : 窗体的位置
//this.setLocation(width/4,height/4);
this.setLocation((width-fheight)/2,(height-fheight)/2);
//
borderLayout = new BorderLayout();
// getContentPane : 获得内容面板
container = this.getContentPane();
container.setLayout(borderLayout);
jTextField = new JTextField("第一个加数:");
container.add(jTextField,BorderLayout.WEST);
jTextField2 = new JTextField("第二个加数:");
container.add(jTextField2,BorderLayout.EAST);
jButton = new JButton("=");
// 行为按钮,发生什么事
jButton.addActionListener(e -> {
String num1 = jTextField.getText();
String num2 = jTextField2.getText();
try{
int sum = MyTool.add(num1, num2);
// 改变jLabel1的文本
jLabel2.setText("计算结果:"+sum);
}catch (Exception ex){
JOptionPane.showMessageDialog(this,ex.getMessage(),"计算异常",JOptionPane.WARNING_MESSAGE);
}
});
container.add(jButton,BorderLayout.CENTER);
jLabel1 = new JLabel("计算方式:+");
container.add(jLabel1,BorderLayout.NORTH);
jLabel2 = new JLabel("计算结果:?");
container.add(jLabel2,BorderLayout.SOUTH);
// setVisible 可视 默认值是:false
this.setVisible(true);
// setDefaultCloseOperation : 默认关闭的操作
// 参数WindowConstants.EXIT_ON_CLOSE:关闭窗体时程序退出
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestFrom();
}
}
public class MyTool {
public static int add(String num1,String num2) throws Exception {
try {
return Integer.parseInt(num1) + Integer.parseInt(num2);
}catch (NumberFormatException e){
throw new Exception("请检查两个相加的数是不是数字!");
}
}
}
3.3、在局域网下多人聊天
复制public class Server {
// 多个收发消息的线程共享msg 变量
public static String msg;
public static void main(String[] args) throws IOException {
// 多人聊天时消息的转发
ServerSocket serverSocket = new ServerSocket(8080);
// 通过死循环接受多人连接
while (true) {
// accept : 阻塞
Socket accept = serverSocket.accept();//阻塞 等待连接
System.out.println("有人连接。。。");
// 通过线程实现消息的接受和转发
new Shou(accept).start();
new ZhuanFa(accept).start();
}
}
}
class Shou extends Thread{
private Socket socket;
public Shou(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true){
String msg = in.readLine();
System.out.println(msg);
// 在锁改变消息之前,其他人不能修改消息
// 同步锁 实现任意时刻只有一个线程改变消息
synchronized ("ab") {
System.out.println("同步:"+msg);
Server.msg = msg;
// 唤醒所有人
"ab".notifyAll();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ZhuanFa extends Thread{
private Socket socket;
public ZhuanFa(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true){
// 没有消息的时候等待
synchronized ("ab"){
"ab".wait();
out.println(Server.msg);
System.out.println("发出一条消息:"+Server.msg);
}
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}