一个线程方面的问题

hugonnyy 2014-02-18 08:53:31
第一个文件
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();找不到.符号的?
...全文
272 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
代码间的舞者 2014-03-04
  • 打赏
  • 举报
回复
引用 9 楼 hugonnyy 的回复:
[quote=引用 8 楼 dring321 的回复:] [quote=引用 7 楼 hugonnyy 的回复:] [quote=引用 5 楼 dring321 的回复:] 你代码没有问题。 这个错误的意思是,找不到ServerThread.class文件。 解决思路: 1、查看ServerThread是否已经编译,即生成class文件。 2、编译的ServerThread.class文件是否和Server.java在同一个路径下。
是编译的时候,不是运行,即生成.class文件前[/quote] 啊,楼主我说的就是编译的时候的出的问题啊。你要先变编译ServerThread.java文件,使其生成ServerThread.class文件,并且放在和Server.java相同目录下[/quote] 连class文件都生成不了 [/quote] 请把编译ServerThread.java文件的过程截图过来。 PS:楼主似乎都没明白我的答案的意思,就急于否认...请考虑下回答问题的人的感受,好么!!!
Azzurro 2014-03-04
  • 打赏
  • 举报
回复
感谢楼上的回答。
hugonnyy 2014-03-04
  • 打赏
  • 举报
回复
引用 8 楼 dring321 的回复:
[quote=引用 7 楼 hugonnyy 的回复:] [quote=引用 5 楼 dring321 的回复:] 你代码没有问题。 这个错误的意思是,找不到ServerThread.class文件。 解决思路: 1、查看ServerThread是否已经编译,即生成class文件。 2、编译的ServerThread.class文件是否和Server.java在同一个路径下。
是编译的时候,不是运行,即生成.class文件前[/quote] 啊,楼主我说的就是编译的时候的出的问题啊。你要先变异ServerThread.java文件,使其生成ServerThread.class文件,并且放在和Server.java相同目录下[/quote] 连class文件都生成不了
代码间的舞者 2014-02-19
  • 打赏
  • 举报
回复
引用 7 楼 hugonnyy 的回复:
[quote=引用 5 楼 dring321 的回复:] 你代码没有问题。 这个错误的意思是,找不到ServerThread.class文件。 解决思路: 1、查看ServerThread是否已经编译,即生成class文件。 2、编译的ServerThread.class文件是否和Server.java在同一个路径下。
是编译的时候,不是运行,即生成.class文件前[/quote] 啊,楼主我说的就是编译的时候的出的问题啊。你要先变异ServerThread.java文件,使其生成ServerThread.class文件,并且放在和Server.java相同目录下
hugonnyy 2014-02-19
  • 打赏
  • 举报
回复
引用 5 楼 dring321 的回复:
你代码没有问题。 这个错误的意思是,找不到ServerThread.class文件。 解决思路: 1、查看ServerThread是否已经编译,即生成class文件。 2、编译的ServerThread.class文件是否和Server.java在同一个路径下。
是编译的时候,不是运行,即生成.class文件前
william_yao 2014-02-19
  • 打赏
  • 举报
回复
首先,我在eclipse中编译代码,编译错误不是因为Server.java:12: 错误: 找不到符号 new ServerThread(socket).start(); 是因为找不到你的CrazyItMap等一系列的自定义的类。 另外,请下载一个eclipse,将代码直接粘贴进去,没有叉就代表编译通过,如果有叉,鼠标悬停在小叉上,看提示!
代码间的舞者 2014-02-18
  • 打赏
  • 举报
回复
你代码没有问题。 这个错误的意思是,找不到ServerThread.class文件。 解决思路: 1、查看ServerThread是否已经编译,即生成class文件。 2、编译的ServerThread.class文件是否和Server.java在同一个路径下。
hugonnyy 2014-02-18
  • 打赏
  • 举报
回复
引用 1 楼 yzw19932010 的回复:
问题描述不够清晰。
编译时出现: Server.java:12: 错误: 找不到符号 new ServerThread(socket).start(); ^ 符号: 方法 start() 位置: 类 ServerThread 1 个错误
引用 2 楼 jzq114 的回复:
把错误代码贴出来看看吧
Server.java:12: 错误: 找不到符号 new ServerThread(socket).start(); ^ 符号: 方法 start() 位置: 类 ServerThread 1 个错误
jzq114 2014-02-18
  • 打赏
  • 举报
回复
把错误代码贴出来看看吧
william_yao 2014-02-18
  • 打赏
  • 举报
回复
问题描述不够清晰。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧