[英]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.