私はあなたの助けが必要です :D だから、私は単純な Java マルチソケットサーバーを構築しようとしています。ここでこのコードを使用して検索しました: http://www.kieser.net/linux/java_server.htmlそしてそれを少し修正しました。うまくいきますが、送信したクライアントだけでなく、すべてのクライアントにメッセージを送り返す必要があります。変更したコードは次のとおりです。
import java.io.*;
import java.net.*;
import java.util.Date;
public class Main {
private static int port=4444, maxConnections=0;
private static Date date = new Date();
// Listen for incoming connections and handle them
public static void main(String[] args) {
int i=0;
System.out.println("[Notice][" + date.toString() + "] - Server started!");
try{
ServerSocket listener = new ServerSocket(port);
Socket server;
while((i++ < maxConnections) || (maxConnections == 0)){
server = listener.accept();
doComms conn_c= new doComms(server);
Thread t = new Thread(conn_c);
System.out.println("[Notice][" + date.toString() + "] - Client connected! ID: " + i);
t.start();
}
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
class doComms implements Runnable {
private Socket server;
private String line;
private static Date date = new Date();
doComms(Socket server) {
this.server=server;
}
public void run () {
try {
// Get input from the client
DataInputStream in = new DataInputStream (server.getInputStream());
PrintStream out = new PrintStream(server.getOutputStream());
while((line = in.readLine()) != null && !line.equals("exit")) {
out.println("Somebody says: " + line);
log("Message: " + line, 0);
}
log("Server closed", 0);
server.close();
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
void log(String msg, int lvl)
{
if(lvl == 0)
{
System.out.println("[Notice][" + date.toString() + "] - " + msg);
}else if(lvl == 1)
{
System.out.println("[Warn][" + date.toString() + "] - " + msg);
}else if(lvl == 2)
{
System.out.println("[Error][" + date.toString() + "] - " + msg);
}
}
}
たいしたことじゃないと思いますが、よくわかりません :D 助けてくれてありがとう!
//編集
今のところ動作するコードは次のとおりです。よくわかりません: マルチキャスト ソケットを使用する必要がありますか? 彼らはより良いパフォーマンスを持っていますか?
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
public class Main {
private static int port=4444, maxConnections=0;
private static Date date = new Date();
// Listen for incoming connections and handle them
public static void main(String[] args) {
int i=0;
System.out.println("[Notice][" + date.toString() + "] - Server started!");
try{
ServerSocket listener = new ServerSocket(port);
Socket server;
while((i++ < maxConnections) || (maxConnections == 0)){
server = listener.accept();
doComms conn_c= new doComms(server, i -1);
Thread t = new Thread(conn_c);
System.out.println("[Notice][" + date.toString() + "] - Client connected! ID: " + i);
t.start();
}
} catch (IOException ioe) {
System.out.println("[Error][" + date.toString() + "] - " + ioe);
ioe.printStackTrace();
}
}
}
class doComms implements Runnable {
private Socket server;
private String line;
private int client_id;
private boolean cmd = false;
private DataInputStream in;
private PrintStream out;
private static ArrayList<doComms> clients = new ArrayList<doComms>();
private static Date date = new Date();
doComms(Socket server, int id) {
this.server = server;
this.client_id = id;
clients.add(this);
}
public void run () {
try {
// Get input from the client
in = new DataInputStream (server.getInputStream());
out = new PrintStream(server.getOutputStream());
while((line = in.readLine()) != null && !line.equals("shutdown") && !line.equals("exit")) {
send_toAll(line, this.client_id);
log("Message: " + line, 0);
}
if(line.equals("shutdown"))
{
log("Got shutdown, going down now!", 0);
log("Calling to exit...", 0);
send_toAll("Going down in 5sec! See you!", this.client_id);
Thread.sleep(5000);
log("Exiting! See you!", 0);
System.exit(0);
}
exit();
} catch (IOException | InterruptedException ioe) {
log("IOException on socket listen: " + ioe, 2);
ioe.printStackTrace();
}
}
void exit()
{
log("Client with ID: " + this.client_id + " disconnected.", 0);
clients.remove(this.client_id);
try {
this.in.close();
this.out.close();
this.server.close();
} catch (IOException ioe) {
log("IOException on socket listen: " + ioe, 2);
ioe.printStackTrace();
}
}
void send_toAll(String msg, int id)
{
for (int i =0; i < clients.size(); i++)
{
clients.get(i).send(msg, id);
}
}
void send(String msg, int id)
{
out.println("Somebody(ID: " + id + ") says: " + msg);
}
void log(String msg, int lvl)
{
if(lvl == 0)
{
System.out.println("[Notice][" + date.toString() + "] - " + msg);
}else if(lvl == 1)
{
System.out.println("[Warn][" + date.toString() + "] - " + msg);
}else if(lvl == 2)
{
System.out.println("[Error][" + date.toString() + "] - " + msg);
}
}
}
ご協力ありがとう御座います!