serverSocketはlocalhostのポートでリッスンしていないので、いくつかのポートを試しました。タイムアウトラインがなくても動作しません。このコードに変更を提案してください。serverSocket not listening
public class server1 extends JApplet implements Serializable{
static JApplet japplet = new JApplet();
private static ServerSocket serverSocket = null;
private static Socket clientSocket = null;
private static final int maxClientsCount = 5;
private static final clientThread[] threads = new clientThread[maxClientsCount];
public void init() {
tool = Toolkit.getDefaultToolkit();
setup_applet();
setup_layout();
run();
}
public void run() {
try {
serverSocket = new ServerSocket(6789);
serverSocket.setSoTimeout(60000);
while (true) {
screen.init_Screen();
clientSocket = serverSocket.accept();
int i = 0;
for (i = 0; i < maxClientsCount; i++) {
if (threads[i] == null) {
(threads[i] = new clientThread(clientSocket, threads))
.start();
break;
}
}
if (i == maxClientsCount) {
clientSocket.close();
}
}
} catch (IOException e) {
System.out.println(e);
}
}
}
class clientThread extends Thread implements Serializable {
private String clientName = null;
private PrintStream os = null;
private Socket clientSocket = null;
private final clientThread[] threads;
private int maxClientsCount;
public clientThread(Socket clientSocket, clientThread[] threads) {
this.clientSocket = clientSocket;
this.threads = threads;
maxClientsCount = threads.length;
}
public void run() {
int maxClientsCount = this.maxClientsCount;
clientThread[] threads = this.threads;
try {
os = new PrintStream(clientSocket.getOutputStream());
while (true) {
String msg = "server:apl";
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] == this) {
os.println(msg);
break;
}
}
}
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this
&& threads[i].clientName != null) {
BufferedImage image = ImageIO.read(clientSocket.getInputStream());
if(image != null) {
soms1.screen.paint(image.getGraphics());
} else {
System.out.println("failed to get");
}
}
}
}
os.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
ファイアウォールをチェックしても、ポートが別のプロセスで使用されていないかどうかチェックしました。 助けをいただければ幸いです。
なぜこれが機能しないのかわかりません。 'serverSocket.accept();'は新しい接続を待ちます。サーバーソケットに実際に接続しない限り、コードは決して新しいクライアントスレッドを開始する行に到達しません。私はあなたもクライアントソケットのコードを追加することをお勧めします –