一个线程方面的问题
第一个文件
import java.net.*;
import java.io.*;
public class Server{
private static final int SERVER_PORT = 30000;
public static CrazyItMap<String,PrintStream> clients = new CrazyItMap<>();
public void init(){
try{
ServerSocket ss = new ServerSocket(SERVER_PORT);
while(true){
Socket socket = ss.accept();
new ServerThread(socket).start();
}
}
catch(Exception e){
System.out.println("服务器启动失败,是否端口 " + SERVER_PORT + "已被占用?");
}
}
public static void main(String[] args){
Server server = new Server();
server.init();
}
}
第二个文件
import java.net.*;
import java.io.*;
public class ServerThread extends Thread{
private Socket socket ;
BufferedReader br = null;
PrintStream ps = null;
public ServerThread(Socket socket){
this.socket = socket;
}
public void run(){
try{
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ps = new PrintStream(socket.getOutputStream());
String line = null;
while((line = br.readLine()) != null){
if(line.startsWith(CrazyItProtocol.USER_ROUND) && line.endsWith(CrazyItProtocol.USER_ROUND)){
String userName = getRealMsg(line);
if(Server.clients.containsKey(userName)){
System.out.println("用户重复");
ps.println(CrazyItProtocol.NAME_REP);
}
else{
System.out.println("成功");
ps.println(CrazyItProtocol.LOGIN_SUCCESS);
Server.clients.put(userName,ps);
}
}
else if(line.startsWith(CrazyItProtocol.PRIVATE_ROUND) && line.endsWith(CrazyItProtocol.PRIVATE_ROUND)){
String userAndMsg = getRealMsg(line);
String user = userAndMsg.split(CrazyItProtocol.SPLIT_SIGN)[0];
String msg = userAndMsg.split(CrazyItProtocol.SPLIT_SIGN)[1];
Server.clients.get(user).println(Server.clients.getKeyByValue(ps) + "悄悄对你说:" + msg);
}
else{
String msg = getRealMsg(line);
for(PrintStream clientsPs : Server.clients.valueSet()){
clientsPs.println(Server.clients.getKeyByValue(ps) + "说" + msg);
}
}
}
}
catch(IOException ioe){
System.out.println("用户" + Server.clients.getKeyByValue(ps) + "下线了!");
Server.clients.removeByValue(ps);
System.out.println("当前用户数量" + Server.clients.size());
try{
if(br != null){
br.close();
}
if(ps != null){
ps.close();
}
if(socket != null){
socket.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
private String getRealMsg(String line){
return line.substring(CrazyItProtocol.PROTOCOL_LEN,line.length() - CrazyItProtocol.PROTOCOL_LEN);
}
}
为什么我编译的时候会提示:Server类中的new ServerThread(socket).start();找不到.符号的?