目录
1.概念
用编程控制网络叫网络编程,也就是Socket
要有服务端(Server)和客户端(Client),客户端发送请求,服务端接收到之后给客户端响应。
2.客户端和服务端进行简单的交互案例
服务端代码:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class MyServer {
public static void main(String args[]) throws IOException {
ServerSocket server = new ServerSocket(9999);
Socket socket = server.accept();
System.out.println("hhhh....");
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
os.write("Hello".getBytes());
for (int i =0;i<5;i++){
char c = (char) is.read();
System.out.println(c);
}
os.close();
is.close();
socket.close();
}
}
客户端代码:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class MyClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost",9999);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
for (int i=0;i<5;i++){
char c = (char)is.read();
System.out.println(c);
}
os.write("Hello".getBytes());
}
}
3.要点:
Socket 相当于缓冲区,服务端和客户端都往Socket里面写内容,然后进行交互。
先启动服务端,再启动客户端,顺序不能反。
网址格式 ( url )
协议:// ip : port /目录名/.../.../资源名
例:http://localhost:9999 (互联网上网页端口默认为80,可以不用写)
HTTP是应用层的协议,是字符型协议
4.请求和响应
客户端发送请求,服务端接收到之后给客户端响应
1.请求
请求分为两部分:
1.请求头:(请求方式 / 请求资源/协议/版本)
GET / HTTP/1.1
2.请求主体:key:value
Host: localhost:9900 Connection: keep-alive sec-ch-ua: "Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "Windows" Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9 Sec-Fetch-Site: none Sec-Fetch-Mode: navigate Sec-Fetch-User: ?1 Sec-Fetch-Dest: document Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9 Cookie: Idea-b81c8d9b=18812050-e902-4281-9ee1-dfc796c7204e
2.响应
响应分为三部分:
1.响应头:(协议/版本 状态码 状态描述)
pw.write("HTTP/1.1 200 OK\r\n");
2.响应主体:key:value
pw.write("Server: Apache\r\n");
pw.write("Date: Fri, 24 Sep 2021 11:58:01 GMT\r\n");
pw.write("Connection: keep-alive\r\n");
pw.write("Content-Type: text/html;charset=utf-8\r\n");
响应主体和响应内容之间有一行隔开
3.响应内容:让用户看到的东西
服务端和浏览器进行交互
打开服务端,在浏览器中的地址栏输入地址”http://localhost:9999“,按回车
代码(服务端只输入)
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer2 {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9999);
Socket socket = server.accept();
InputStream is = socket.getInputStream();
while (true){
System.out.print((char) is.read());
}
}
}
进阶代码(服务端有输入有输出)
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer1 {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9900);
Socket socket = server.accept();
System.out.println("hhh...");
//接受客户端传递的数据,并进行解析
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//获取客户端传递的第一条信息
String firstLine = br.readLine();
String [] str_re = firstLine.split(" ");
//根据客户端的请求 给客户端进行相应的响应
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.write("HTTP/1.1 200 OK\r\n");
pw.write("Server: Apache\r\n");
pw.write("Date: Fri, 24 Sep 2021 11:58:01 GMT\r\n");
pw.write("Connection: keep-alive\r\n");
pw.write("Content-Type: text/html;charset=utf-8\r\n");
String msg = " ";
pw.write("Content-Length: "+msg.length()+"\r\n\r\n");
//不同的请求进行不同的响应
if (str_re[1].equals("/")){
msg="default";
}else if (str_re[1].equals("/a.html")){
msg="aaaaa";
}else if (str_re[1].equals("/b.html")){
msg="bbbb";
}
pw.write(msg);
pw.flush();
pw.close();
os.close();
socket.close();
}
}