package socket;
import java.io.*;
import java.net.*;
import java.util.*;
public class ServerDemo {
public static void main(String[] args) throws IOException{
System.out.println("服务器已经启动......\n");
ServerSocket server=new ServerSocket(8000);
while(true){
Socket s=server.accept();
System.out.println("等待用户连接......\n");
new ServerThread(s).start();//这个地方出的错:Severity and Description Path Resource Location Creation Time Id
No enclosing instance of type ServerDemo is accessible. Must qualify the allocation with an enclosing instance of type ServerDemo (e.g. x.new A() where x is an instance of ServerDemo). MyApps/socket ServerDemo.java line 12 1154857248907 74
}
}
class ServerThread extends Thread{
private Socket s;
ServerThread(Socket s){
this.s=s;
}
public void run(){
BufferedReader br=null;
PrintWriter pw=null;
try{
InputStreamReader isr;
isr=new InputStreamReader(s.getInputStream());
br=new BufferedReader(isr);
pw=new PrintWriter(s.getOutputStream(),true);
Calendar c=Calendar.getInstance();
do{
String cmd=br.readLine();
cmd=cmd.toUpperCase();
if(cmd.startsWith("TIME")||cmd.startsWith("DATE"))pw.println(c.getTime().toString());
else if(cmd.startsWith("BYE")) break;
else{
System.out.println(cmd);
}
}while(true);
}catch(IOException e){
System.out.println(e.toString());
}finally{
System.out.println("关闭连接......\n");
try{
if(br !=null) br.close();
if(pw !=null) pw.close();
if(s !=null) s.close();
}catch(IOException e){

}
}
}
}}