好吧我正在努力做的是一个测验程序,它允许多个用户连接并查看其他团队分数,以便能够与他们进行比较。但是我遇到的问题是,我无法弄清楚如何让多个用户连接到同一台服务器,以便能够同时使用它。目前,单个用户可以一次连接。
有什么建议吗?
(线程,一个是为每个连接的用户/团队创建的)
package QuizProgram.ServerComponents;
import QuizProgram.Frames.Team;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by Daniel on 16/06/2015.
*/
public class PointsServer
{
private static int lastArrayPosition=0;
protected static ArrayList<TeamThread>threadsList;
protected static ArrayList<Team>teamsList;
protected static PrintWriter toClient;
protected static Scanner fromClient;
protected static ServerSocket serverSocket;
protected static Socket socket;
public static synchronized void setupList()
{
teamsList=new ArrayList<Team>();
threadsList=new ArrayList<TeamThread>();
}
public static synchronized ArrayList<Team> getTeams()
{
return teamsList;
}
public static synchronized void addTeam(String teamName)
{
teamsList.add(new Team(teamName));
}
public static synchronized void addTeam(String teamName, int initialScore)
{
teamsList.add(new Team(teamName,initialScore));
}
public static synchronized void addTeamScore(String teamName, int addScore)
{
boolean found=false;
int count=0;
String currentTeamName="";
Team currentTeam;
while(!found && count<teamsList.size())
{
currentTeam=teamsList.get(count);
currentTeamName=currentTeam.getTeamName();
if(currentTeamName.equals(teamName))
{
currentTeam.setScore(currentTeam.getScore()+addScore);
found=true;
}
}
}
public static void setupServer()
{
try
{
System.out.println("setting up server");
serverSocket=new ServerSocket(2004);
System.out.println("waiting for connection");
socket=serverSocket.accept();
System.out.println("connection established");
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void setupThreads()
{
threadsList.add(new TeamThread(socket));
//TeamThread thread=new TeamThread(teamName,toClient,fromClient,socket);
//thread.start();
threadsList.get(lastArrayPosition).start();
lastArrayPosition++;
try
{
socket.close();
}
catch(Exception ex){}
}
}//class
(启动服务器时如何调用服务器)
package QuizProgram;
import QuizProgram.ServerComponents.PointsServer;
/**
* Created by Daniel on 17/06/2015.
*/
public class ServerTest
{
public static void main(String[] args)
{
PointsServer.setupList();
while(true)
{
PointsServer.setupServer();
PointsServer.setupThreads();
}
// PointsServer.setupServer();
// PointsServer.setupThreads();
}//main
}//class
(点服务器,其中包含服务器使用和存储信息的方法的msot)
package QuizProgram.ServerComponents;
import QuizProgram.Frames.Team;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by Daniel on 16/06/2015.
*/
public class PointsServer
{
private static int lastArrayPosition=0;
protected static ArrayList<TeamThread>threadsList;
protected static ArrayList<Team>teamsList;
protected static PrintWriter toClient;
protected static Scanner fromClient;
protected static ServerSocket serverSocket;
protected static Socket socket;
public static synchronized void setupList()
{
teamsList=new ArrayList<Team>();
threadsList=new ArrayList<TeamThread>();
}
public static synchronized ArrayList<Team> getTeams()
{
return teamsList;
}
public static synchronized void addTeam(String teamName)
{
teamsList.add(new Team(teamName));
}
public static synchronized void addTeam(String teamName, int initialScore)
{
teamsList.add(new Team(teamName,initialScore));
}
public static synchronized void addTeamScore(String teamName, int addScore)
{
boolean found=false;
int count=0;
String currentTeamName="";
Team currentTeam;
while(!found && count<teamsList.size())
{
currentTeam=teamsList.get(count);
currentTeamName=currentTeam.getTeamName();
if(currentTeamName.equals(teamName))
{
currentTeam.setScore(currentTeam.getScore()+addScore);
found=true;
}
}
}
public static void setupServer()
{
try
{
System.out.println("setting up server");
serverSocket=new ServerSocket(2004);
System.out.println("waiting for connection");
socket=serverSocket.accept();
System.out.println("connection established");
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void setupThreads()
{
threadsList.add(new TeamThread(socket));
//TeamThread thread=new TeamThread(teamName,toClient,fromClient,socket);
//thread.start();
threadsList.get(lastArrayPosition).start();
lastArrayPosition++;
try
{
socket.close();
}
catch(Exception ex){}
}
}//class
如果需要,我也可以显示客户端代码,但我很确定服务器端的某些内容我做错了。 感谢