一、网络编程的基本要素
IP地址+端口号+协议
- IP地址就是指服务器的IP 也就是定位此服务器的标识(本机的IP地址是127.0.0.1)
- 端口号值的是该IP地址的服务器的哪一个端口与客户端或者浏览器进行连接,通过IP地址+端口号就可以确定这台服务器的对外接口
- 协议 通俗来说就是规定了与该服务器连接时的标准,因为全世界的计算机数量是很庞大的数目,所有计算机一起组成了互联网,如果没有一套标准的话,不同计算机连接的效率会很差。
二、通信协议
目前的通信协议分为两种,分别是TCP协议和UDP协议
TCP协议(传输控制协议)
TCP协议最重要的知识点是三次握手:
- 第一次握手时 客户端向服务器发送连接请求 等待服务器确认
- 第二次握手时 服务器接收到客户端的请求后,向客户端发送一个响应,通知客户端已接收到请求
- 第三次握手时 客户端再次向服务器发送消息,确认连接
UDP协议(用户数据报协议)
是一个面向无连接的协议,也就是说不管对方是否能不能收到,客户端都会单纯的发送
64kb以内
网络编程的分类
- B/S程序:浏览器与服务器 程序
- C/S程序:客户端与服务器 程序
实例操作:
package com.jiansheng.TCP_Thread;
import java.io.Serializable;
import java.util.Objects;
/**
* @author Jiansheng
*/
public class AccountBean implements Serializable {
private String account;
private String password;
public AccountBean(String account, String password) {
this.account = account;
this.password = password;
}
public AccountBean() {
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "AccountBean{" +
"account='" + account + '\'' +
", password='" + password + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountBean that = (AccountBean) o;
return Objects.equals(account, that.account) &&
Objects.equals(password, that.password);
}
}
package com.jiansheng.TCP_Thread;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author Jiansheng
*/
public class Servlet {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//搭建服务器
ServerSocket ss = new ServerSocket(55566);
//等待客户端的连接
System.out.println("服务器已经启动,正在等待客户端的连接:");
while(true){
Socket sc = ss.accept();
new Thread(new MyRunnalbe(sc)).start();
}
}
}
class MyRunnalbe implements Runnable{
Socket sc = null;
public MyRunnalbe(Socket sc){
super();
this.sc = sc;
}
@Override
public void run() {
//获取用户端发送的消息并进行序列化操作
InputStream ips = null;
try {
ips = sc.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
ObjectInputStream is = null;
try {
is = new ObjectInputStream(ips);
} catch (IOException e) {
e.printStackTrace();
}
//创建对象接收输入
AccountBean ab = null;
try {
ab = (AccountBean) is.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if ("admin".equals(ab.getAccount()) && "abc".equals(ab.getPassword())){
OutputStream os = null;
try {
os = sc.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
PrintStream ps = new PrintStream(os);
ps.println("登陆成功,管理员您好。");
}else if ("user".equals(ab.getAccount()) && "123".equals(ab.getPassword())){
OutputStream os = null;
try {
os = sc.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
PrintStream ps = new PrintStream(os);
ps.println("登陆成功,用户您好。");
}else {
OutputStream os = null;
try {
os = sc.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
PrintStream ps = new PrintStream(os);
ps.println("登陆失败,请检查您的用户名和密码。");
}
}
}
package com.jiansheng.TCP_Thread;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
/**
* @author Jiansheng
*/
public class Client {
public static void main(String[] args) throws IOException {
while (true) {
//定义用户输入
Scanner input = new Scanner(System.in);
System.out.println("请输入您的账号:");
String account = input.nextLine();
System.out.println("请输入您的密码:");
String password = input.nextLine();
//将用户输入存入AccountBean对象中
AccountBean ab = new AccountBean(account, password);
//创建Socket对象进行连接
Socket sc = new Socket("localhost", 55566);
//创建连接流
OutputStream ops = sc.getOutputStream();
//将AccountBean对象放到连接流里
ObjectOutputStream oos = new ObjectOutputStream(ops);
oos.writeObject(ab);
//客户端接收服务器返回的消息
InputStream ips = sc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ips));
String s = br.readLine();
System.out.println(s);
}
}
}