package syson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
new Server().start();
}
public void start() throws IOException {
//给服务端设定一个占用的端口
ServerSocket serverSocket = new ServerSocket(8204);
Socket socket = null;
System.out.println("启动web服务");
//无限循环时刻监听
while (true) {
socket = serverSocket.accept();
Thread thread = new Thread(new ServerThread(socket));
thread.start();
}
}
private class ServerThread implements Runnable {
Socket socket = null;
ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
InputStream inputStream = null;
OutputStream os = null;
BufferedReader br = null;
try {
inputStream = this.socket.getInputStream();
os = this.socket.getOutputStream();
// 页面的请求
br = new BufferedReader(new InputStreamReader(inputStream));
String line = br.readLine();
int i = 0;
//ready 是查看流是否已经准备好被读,是一个非阻塞的方法
while (null != line && br.ready()) {
//readLine是一个阻塞的方法,只要没有断开连接,就会一直等待,直到有东西返回只有读到数据流 的末尾才返回null
line = br.readLine();
System.out.println("第" + i + "行信息:" + line);
i++;
}
// 页面的响应
String reply = "HTTP/1.1\n"; //指定使用那种协议
reply += "Content-type:text/html;charset=utf-8\n\n";
reply += "<html><head></head><body><form action=\"\" method=\"post\">\r\n" +
" 用户名:<input name=\"username\" type=\"text\" value=\"" + "123" + "\" />\r\n" +
" 密码:<input name=\"password\" type=\"text\" value=\"" + "475" + "\" />\r\n" +
" <input type=\"submit\" value=\"登录\" />\r\n" +
" </form></body></html>"; //这里面写的是你的内容
os.write(reply.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
inputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
在页面 输入自己的ip然后加上端口号就可以看到结果啦!