わかりました、私の友人が Java のネットワーキングについて教えてくれました。私たちは成功したプログラムを持っています。パテでつなぐのは簡単なおしゃべりですが、1つ問題があります。一度に接続できるクライアントは 1 つだけです。一度により多くのクライアントを接続する方法と、接続するクライアントの数を制限する方法を教えてください。
public class Base
{
static ServerSocket serverSocket;
public static void main(String[] args) throws IOException
{
final ServerSocket serverSocket = new ServerSocket (1337);
Thread thread = new Thread(new Runnable()
{
public void run()
{
try
{
System.out.println("Waiting for connections...");
//Make Socket on port
Socket client = serverSocket.accept();
System.out.println("Connection from " + client.getInetAddress());
//initialixe no socket with connect server gets
BufferedReader in = new BufferedReader (new InputStreamReader(client.getInputStream()));
//init new beffer to read incom
//while loop to read stuff
final BufferedWriter out = new BufferedWriter(new PrintWriter(client.getOutputStream()));
out.write("Your Connected Mate");
out.newLine();
out.flush();
new Thread(new Runnable()
{
public void run()
{
try
{
Scanner s = new Scanner(System.in);
while(s.hasNext())
{
out.write("CLIENT] " + s.nextLine());
out.newLine();
out.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}).start();
while(true)
{
//make a string form anything thats read from the socket
String tmp = in.readLine();
//if the string isnt null (which it is if we disconnect) print it out
if(tmp != null)
{
System.out.println("[CLIENT -> SERVER] " + tmp);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
thread.run();
}
}
わからないので、何も試していません:)