一个线程方面的问题
第一个文件
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();找不到.符号的?
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();找不到.符号的?
...全文
相关推荐
实现多线程同步问题--一个线程自加一个线程显示
面试的时候碰到的一个问题,实现多线程对一个全局数的自加,另外一个线程当全局数自加1以后立马显示这个数的现值。
项目开发中碰到的一个线程问题 (二)
NULL 博文链接:https://alanlg.iteye.com/blog/1697445
一个多线程示例程序及多线程常见问题介绍
一个多线程示例程序及多线程常见问题介绍。实例程序,带有源码,直接可用,c++builder的精彩编程!!
c++ 多线程向同一个文本写入信息 解决同步与互斥问题
c++ 多线程向同一个文本写入信息 解决同步与互斥问题
一个高频问题:异步操作会创建线程吗?.doc
一个高频问题:异步操作会创建线程吗?.doc


Java SE
