【小程序】模拟web服务器

/*

模拟web服务器

*/

package com.lin.michael;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.io.OutputStream;

public class Demo09 extends Thread{

Socket socket;

public Demo09(Socket socket){

this.socket = socket;

}

@Override

public void run() {

try {

//1.获取sokcet的输出对象

OutputStream outputStream = socket.getOutputStream();

//2.把数据返回给给浏览器

outputStream.write("你好,浏览器先生".getBytes());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(String[] args) throws IOException{

//1.建立tcp服务器

ServerSocket serversocket = new ServerSocket(9090);

//2.不断接收客户端的请求

while(true){

Socket socket = serversocket.accept();

new Demo08(socket).start();

}

}

}

你可能感兴趣的:(【小程序】模拟web服务器)