写一个服务器的前期准备
Socket编程
C/S结构 – 客户端/服务器
可以参考之前的文章:
java网络编程以及通信的项目研究(一)
网址:https://blog.youkuaiyun.com/qq_43545801/article/details/109552092
或者参考以下的代码:
客户端:
package net.test;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
System.out.println("客户端启动");
//1.创建Socket对象
Socket client = null;
//2.获取输出流-请求
DataOutputStream dos = null;
DataInputStream dis = null;
try {
client = new Socket("localhost",9999);
//2.获取输出流-请求
dos = new DataOutputStream(client.getOutputStream());
dos.writeUTF("我是客户端,服务器端,你好");
//3.获取输入流-响应
dis = new DataInputStream(client.getInputStream());
System.out.println(dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭流--调用关闭的工具类
IOClose.closeAll(dis,dos,client);
}
}
}
关闭流的类
package net.test;
import java.io.Closeable;
import java.io.IOException;
public class IOClose {
//关闭全部的工具类
public static void closeAll(Closeable...c){
for (Closeable closeable:c){
if (closeable!=null){
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器端:
package server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
System.out.println("这是服务器端");
//1. 创建ServerSocket对象
ServerSocket server = null;
DataInputStream dis = null;
DataOutputStream dos= null;
Socket socket = null;
try {
server = new ServerSocket(9999);
//2.监视客户端是否有客户端连接
socket = server.accept();
//3.获取输入流接入数据,得到客户端的请求
dis = new DataInputStream(socket.getInputStream());
System.out.println(dis.readUTF());
//4.获取输出流,给客户端一个响应的结果
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("客户端你好,我是服务器,收到您的消息");
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭流--调用关闭的工具类
IOClose.closeAll(dis,dos,socket,server);
}
}
}
关闭流的类和客户端的关闭流的类一样
运行结果如下:
B/S结构 – 浏览器/服务器
在上面的代码基础上,进行测试
我们将服务器端变为浏览器端,进行一些改造
package server;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
System.out.println("这是服务器端");
//1. 创建ServerSocket对象
ServerSocket server = null;
Socket socket = null;
BufferedReader br = null;
try {
server = new ServerSocket(9999);
socket = server.accept();
br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
String str = null;
while ((str=br.readLine()).length()>0) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭流--调用关闭的工具类
IOClose.closeAll(br,socket,server);
}
}
}
我们打开浏览器,输入 http://localhost:9999/index.html
运行结果如下:
这样我们初步完成了B/S的改造
HTML和基础服务器的交互
先对HTML页面进行一个简单的了解
<html>
<head>
<title>一个可以交互的html</title>
</head>
<body>
<h1>登录</h1>
<form action="" method="post">
<p>用户名:<input type="text" id="uname" name="username"></p>
<p>密码: <input type="password" id="pwd" name="password"></p>
<p><input type="submit" value="登录"></p>
</form>
</body>
</html>
我们输入一个用户名和密码,可以在浏览器控制台的网络一栏的表头下看到
我们在form的action内添加上跳转地址:
http://localhost:9999/index.html
开始对上面的C/S服务器代码进行简单的修改
package server;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
System.out.println("这是服务器端");
//1. 创建ServerSocket对象
ServerSocket server = null;
Socket client = null;
InputStream is = null;
try {
server = new ServerSocket(9999);
client = server.accept();
//获取来自浏览器的请求信息
is = client.getInputStream();
byte [] buf = new byte[20480];
int len = is.read(buf);
System.out.println(new String(buf,0,len));
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭流--调用关闭的工具类
IOClose.closeAll(is,client,server);
}
}
}
我们先启动自建的服务器,在浏览器运行桌面的index.html登录网页,输入用户名和密码,运行结果如下:
我们可以看到,在浏览器输入的用户名和密码,可以被自建的服务器接收到
利用HTTP协议实现点击登录按钮显示“登录成功”提示
package server;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
String CRLF="\r\n";//换行
String BLANK = " ";//空格
System.out.println("这是服务器端");
//1. 创建ServerSocket对象
ServerSocket server = null;
Socket client = null;
InputStream is = null;
try {
server = new ServerSocket(9999);
client = server.accept();
//获取来自浏览器的请求信息
is = client.getInputStream();
byte [] buf = new byte[20480];
int len = is.read(buf);
System.out.println(new String(buf,0,len));
/**
* 对web浏览器的请求作出响应
*/
StringBuffer sb = new StringBuffer();
StringBuffer sbContent = new StringBuffer();//响应的文本
sbContent.append("<html><head><title>响应结果</title></head>");
sbContent.append("<body>登陆成功</body></html>");
//1. 拼接响应头
sb.append("HTTP/1.1").append(BLANK).append(200).append(BLANK).append("OK");
sb.append(CRLF);
//2.构造响应头
sb.append("Content-Type:text/html;charset=utf-8");
sb.append(CRLF);
sb.append("Content-Length:").append(sbContent.toString().getBytes().length).append(CRLF);
sb.append(CRLF);//换行,代表响应头和响应的正文部门之间的空行
sb.append(sbContent);
//通过流输出
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));
bw.write(sb.toString());
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭流--调用关闭的工具类
IOClose.closeAll(is,client,server);
}
}
}