packagetomcatdemo;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;publicclassMyServer{privateServerSocketserverSocket;privateintport;privatebo...
package tomcatdemo;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
private ServerSocket serverSocket;
private int port;
private boolean shutdown; //default false;
public void startup() {
try {
serverSocket = new ServerSocket(port);
while(!shutdown) {
Socket socket = serverSocket.accept();
System.out.println("一个客户端请求来了!");
new Thread(new DispatchRequest(socket)).start(); //启动一个线程,为请求提供服务,以后可以增加线程池,对线程提供管理
}
} catch (IOException e) {
e.printStackTrace();
}
}
public MyServer(int port) {
super();
this.port = port;
}
public static void main(String[] args) {
new MyServer(6666).startup();
}
}
package tomcatdemo;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class DispatchRequest implements Runnable {
private Socket socket;
。。。字数限制就省略了
地址栏输这个localhost:6666,浏览器是网页无法访问,控制台没反应
展开