[英]How to create a socket that constantly listen from the Server in Java?
我正在使用Java的Sockets做時鍾,但是我需要我的客戶端可以與Server交互以更改時間。 我的主要問題是:如果添加任何代碼以監聽客戶端,則服務器停止。 我想我可以使用線程或異步函數,但是我在Java中是一個全新的人,我也不知道如何操作它。
這些是我當前的代碼:
server.java
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class server {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
ServerSocket ss = new ServerSocket(2000);
Timer data = new Timer();
while (true) {
Socket s = ss.accept();
ArrayList<Integer> time = new ArrayList<Integer>();
data.updateTime();
time.add(data.getHour());
time.add(data.getMinute());
time.add(data.getSecond());
System.out.println(time.get(0) + ":" + time.get(1) + ":"
+ time.get(2));
try {
ObjectOutputStream oos = new ObjectOutputStream(
s.getOutputStream());
oos.writeObject(time);
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000); // 1000 milliseconds is one second.
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
Timer.java
import java.util.Random;
public class Timer {
private int hour;
private int minute;
private int second;
public Timer() {
this.hour = randInt(0, 23);
this.minute = randInt(0, 59);
this.second = randInt(0, 59);
}
public void changeTime(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public static int randInt(int min, int max) {
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
private void updateHour() {
if (this.hour == 23)
this.hour = 0;
else
this.hour++;
}
private void updateMinute() {
if (this.minute == 59)
this.minute = 0;
else
this.minute++;
}
private void updateSecond() {
if (this.second == 59)
this.second = 0;
else
this.second++;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
public void updateTime() {
this.updateSecond();
if (this.second == 0) {
this.updateMinute();
if (this.minute == 0)
this.updateHour();
}
}
}
這是我要發送數據的事件單擊:
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == startButton) {
Socket s;
try {
s = new Socket("localhost", 2000);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
in, textField1.getText()));
out = new BufferedOutputStream(out);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out,
textField1.getText()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// we try to connect
}
}
我想知道你們是否知道我應該在服務器中添加哪種代碼,以繼續發送隨機創建的時間,並等待是否有來自客戶端的消息以及是否有消息更改時間新的那一個。 在此先感謝您的幫助,如果這是一個基本問題,則非常抱歉,但是我真的是Sockets和Java的新手。
您可以創建一個類來存儲您的計時器。
public class MyTimer(){
private Timer t;
public MyTimer(Timer t){
this.t = t;
}
public synchronized Timer getTimer(){
return t;
}
public synchronized void setTimer(Timer t){
this.t = t;
}
}
在服務器中,您需要使用此類中的一個對象。
final MyTimer myTimer = new MyTimer(new Timer());
在while循環的第一行中,您可以將myTimer中的計時器獲取到數據變量。
Timer data = myTimer.getTimer();
在while循環之前,啟動一個新的線程來偵聽客戶端,以便您可以更新myTimer對象中的計時器。
new Thread(new Runnable(){
public void run(){
while(true){
//Listen to the client if there is new time available
//get new timer to a variable (here, newData)
myTimer.setTimer(newData);
}
}
}).start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.