我有一個客戶端/服務器應用程序來管理某種類型的行。 所有的客戶添加對象到我的線。如何通過java中的服務器啓動客戶端更新
我希望服務器每當線路,線路插入或移除發生變化時,都會向客戶端發送jpanel的屏幕截圖。 我設法捕捉jpanel jpag甚至發送它。 但我的應用程序的流程停止,第一次更新後我得到eofexception,終止我的偵聽服務器套接字。
什麼是更新客戶端的正確方法?我是否應該設置一個serversocket以便始終在客戶端進行監聽?
請幫助,我堅持這個爲2周。
這是我的監聽線程(服務器):
public class ListeningThread implements Runnable {
static boolean listening = true;
public BufferedReader in;
public void run() {
ServerSocket echoServer = null;
String line;
DataInputStream is = null;
PrintStream os = null;
Socket clientSocket = null;
try {
echoServer = new ServerSocket(RequestReciever._communicationPort);
}
catch (IOException e) {
System.out.println(e);
}
// Create a socket object from the ServerSocket to listen and accept
// connections.
// Open input and output streams
try {
// As long as we receive data, send it to be phrased to a request.
while (true) {
clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
// An option for a stop listening button. currently not available !
if(listening==true) {
line = is.readUTF();
os.println(line);
System.out.println(line);
RequestReciever.pharseToRequest(line);
// clientSocket = null;
}
else {
echoServer.close();
is.close();
os.close();
break;
}
}
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Listening Thread Unknown error");
}
}
}
這是我Pharse方法:
public static void pharseToRequest(String input) {
List<String> list = new ArrayList<String>(Arrays.asList(input.split(";;;")));
if (list.get(0).equalsIgnoreCase("Login") && list.get(1).equalsIgnoreCase ("Login") && list.get(2).equalsIgnoreCase("5"))
{
_adminClients.add(list.get(4));
updateScreenCapture();
AdminClientUpdate tmp = new AdminClientUpdate(list.get(4));
Thread aCU = new Thread (tmp);
aCU.start();
}
else
{
ServerRequest newReq = new ServerRequest(list.get(0), list.get(1), Integer.parseInt(list.get(2)),list.get(3),list.get(4));
addRequest(newReq);
}
}
,這是AdminClientUpdate類
public class AdminClientUpdate implements Runnable {
static boolean listening = true;
public BufferedReader in;
public String _ip;
public AdminClientUpdate(String ip)
{
_ip = ip;
}
public void run() {
try {
Socket socket = new Socket(_ip, RequestReciever._communicationPort);
InputStream in = new FileInputStream("Capture/tmp.jpg");
java.io.OutputStream out = socket.getOutputStream();
copy(in, out);
System.out.println("Sent Image !");
socket.close();
out.close();
in.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
System.out.println("Cant find tmp.jpg");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void copy(InputStream in, java.io.OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
我認爲它只關閉此線程內的套接字,而不是偵聽器類,但我會嘗試。 –
是啊試試它或刪除整個塊後嘗試,如果它的工作確實接受答案。 – sam
嗨,我trird刪除整個塊,不做任何事,「其他」塊永遠不會到達。我仍然得到錯誤按摩:java.io.EOFException的 監聽線程未知錯誤 \t在java.io.DataInputStream.readUnsignedShort(來源不明) \t在java.io.DataInputStream.readUTF(來源不明) \t是java。 io.DataInputStream.readUTF(Unknown Source) \t at ListeningThread.run(ListeningThread.java:47) \t at java.lang.Thread.run(Unknown Source) –