我正在使用带有Java的套接字来做一种时钟但是我需要我的客户端可以与服务器交互来改变时间。我的主要问题是:如果我添加任何代码来监听客户端,服务器停止。我想我可以使用一个线程或一个异步函数,但我是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真的很新。
答案 0 :(得分:1)
您可以创建一个类来存储您的计时器。
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();