Java webapp (ソケット クライアント) からの接続を待機しているソケット サーバー (Java デスクトップ アプリ) があります。通信は問題ないようです。クライアント側でサーバーからのメッセージが表示されますが、メッセージを送信してもサーバー側で何も受信しません。何が問題になる可能性がありますか? telnet でサーバーを確認したところ、すべて正常に動作しています。以下は私のコードです:
サーバ:
public class ThumbnailGenerator {
static Logger log = Logger.getLogger("ThumbnailGenerator");
public static List<ThumbnailTask> taskqueue = new ArrayList<ThumbnailTask>();
public static void main(String[] args) throws IOException {
ThumbnailTask urlobject = new ThumbnailTask();
urlobject.setUrl("http://www.google.com");
urlobject.setOutputfile("newFile");
urlobject.setStatus(0);
taskqueue.add(urlobject);
MyThread mt = new MyThread();
mt.start();
startServer();
}
public static void startServer() {
final int portNumber = 2000;
log.info("Creating server socket on port " + portNumber);
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(portNumber);
log.info("Server waiting for clients!");
while (true) {
Socket socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println("Give URL address!");
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = br.readLine();
log.info("Message received from client is " + str);
String[] ar = str.split(",");
ThumbnailTask urlobject = new ThumbnailTask();
urlobject.setUrl(ar[0]);
urlobject.setOutputfile(ar[1]);
urlobject.setStatus(0);
synchronized (taskqueue) {
taskqueue.add(urlobject);
}
pw.println("Starting making thumbnail of " + ar[0]);
pw.close();
socket.close();
log.info("Thumbnail of: " + ar[0] + " will be created");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class MyThread extends Thread {
static Logger log = Logger.getLogger("MyThread");
boolean running = true;
public void run() {
log.debug("ENTRY THREAD");
while (running) {
if (!ThumbnailGenerator.taskqueue.isEmpty()) {
Iterator<ThumbnailTask> thumbnailiterator = ThumbnailGenerator.taskqueue.iterator();
int count = 0;
while (thumbnailiterator.hasNext()) {
ThumbnailTask task = thumbnailiterator.next();
boolean thumbnailresult = false;
if (task.getStatus() == 0) {
thumbnailresult = ThumbnailGenerator.makeThumbnail(task.getUrl(), task.getOutputfile());
if (thumbnailresult = true) {
task.setStatus(1);
//ThumbnailGenerator.taskqueue.set(count, task);
log.info("RESULT = true");
}
}
count++;
}
} else {
running = false;
}
try {
MyThread.sleep(10000);
} catch (InterruptedException e) {
running = false;
e.printStackTrace();
}
}
log.debug("EXIT");
}
クライアント:
public static void startSender(String url) {
final String host = "localhost";
final int portNumber = 2000;
log.info("Creating socket to '" + host + "' on port " + portNumber);
try {
while (true) {
Socket socket = new Socket(host, portNumber);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
log.info("Server says:" + br.readLine());
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String sendMessage = url;
bw.write(sendMessage);
bw.flush();
log.info("Message sent to the server : " + sendMessage);
log.info("Server says:" + br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
手伝ってくれてありがとう!