- UID
- 44577
- 阅读权限
- 20
- 注册时间
- 2015-10-25
- 最后登录
- 1970-1-1
- 在线时间
- 小时
- 人气
- 点
- MC币
- 个
- 贡献
- 点
该用户从未签到
|
今天放假最后一天=A=闲的无聊的我看见某@Jianghao7172 的PHP教程
身为他(nv)朋友索性也写一个技术性的文章咯
于是脑洞大开想用Java做个伪http服务端= =
所以咯~
就码了几个小时=A=二话不说放我的咸鱼代码(原创)
- package com.wx302x.socket;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Server {
- public static int ServerPort = 20000;
- public static void main(String args[]) {
- ServerSocket s = null;
- Socket socket = null;
- String Head;
- String line;
- String lineall;
- BufferedReader is;
- PrintWriter os;
- try {
- s = new ServerSocket(ServerPort);
- System.out.println("服务端开启在端口:"+s);
- while (true){
- socket = s.accept();
- System.out.println("客户端连接:"+socket);
- is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- Head = is.readLine();
- line = "wx302x";
- lineall = Head + "\r\n";
- while (!line.equals("") )
- {
- line = is.readLine();
- lineall = lineall + line + "\r\n" ;
- }
- os = new PrintWriter(socket.getOutputStream());
- os.println("HTTP/1.1 200 OK");
- os.println("Content-Type:text/html;charset:GBK");
- os.println();
- OnClientConnect (os);
- os.flush();
- os.close();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- private static void OnClientConnect (PrintWriter rs){
- rs.println("Jianghao7172 I Love You <br>- wx302x ");
- }
- }
复制代码
测试了下 Ok~
|
评分
-
查看全部评分
|