Java – programming alarms via sockets
I made a digital clock in Java through socket programming
When the client sends the same time matching time as the system server and plays the alarm However, when the time sent by the client exceeds 1 minute or 2 minutes, the server does not match the system time and does not play the alarm
My code
Server side:
public class GraphicsServer extends JFrame implements Runnable { String wr; String text; JTextField f1; JTextField f2; JScrollPane js; JTextArea jt; JPanel p; Color color; Calendar cal; String time; String time1; int hrs; int min; int sec; public GraphicsServer() { super("Server Tik Tik Tok"); setSize(400,400); p = new JPanel(); f1 = new JTextField(10); f2 = new JTextField(10); p.add(f1); p.add(f2); add(p); Thread thread = new Thread(new Runnable() { public void run() { while(true) { Calendar Now = Calendar.getInstance(); int hrs = Now.get(Calendar.HOUR_OF_DAY); int min = Now.get(Calendar.MINUTE); int sec = Now.get(Calendar.SECOND); String time = hrs + ":" + min + ":" + sec; f2.setText(time); time1 = hrs + ":" + min; // System.out.println(time1); // System.out.println(time); try { Thread.sleep(1000); } catch(Exception ee) { System.out.println(ee.getMessage()); } } } }); thread.start(); } public void run() { try { ServerSocket server = new ServerSocket(1111); while(true) { Socket ss = server.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(ss.getInputStream())); text = br.readLine(); f1.setText(text); wr = time1; if(text.endsWith(wr)) { color = JColorChooser.showDialog(GraphicsServer.this,"Choose a color",color); p.setBackground(color); } } } catch(Exception e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { GraphicsServer gs = new GraphicsServer(); gs.setVisible(true); Thread th = new Thread(gs); th.start(); } }
client:
public class GrapClient extends JFrame { JButton b1; JTextField f1; Socket client; public GrapClient() { super("Client Tik Tik Tok"); setSize(400,400); JPanel p = new JPanel(); b1 = new JButton("Enter Alarm Time"); f1 = new JTextField(10); p.add(b1); p.add(f1); add(p); ActionListener newListener = new ActionListener() { public void actionPerformed(ActionEvent event) { try { Socket client = new Socket("localhost",1111); PrintStream ps = new PrintStream(client.getOutputStream()); String g = f1.getText(); ps.println(g); f1.setText(""); } catch(Exception e) { System.out.println(e.getMessage()); } } }; b1.addActionListener(newListener); } public static void main(String[] args) { GrapClient GC = new GrapClient(); GC.setVisible(true); GC.setResizable(false); } }
I want to make an alarm clock In the server, I embed the digital clock into the text field When the client sends the alarm time to the server, the server will compare the client alarm time with the digital clock time If the time on the server side is 23:10 and the client sends 23:10 to the server at the same time, the server will display jfile chooser If the server has a time of 23:10 and the client sends an alarm, the time is 23:11 When the server time is equal to the client alarm time after 1 minute, the server will open jfilechoser
Solution
OK, there are some problems with this code
>You can access many fields from different thread instances without any synchronization steps This can lead to strange thread related errors > you cannot access the swing component on the event dispatch thread It may be a good idea to read swing concurrency > start using good variable names, which will make it easier for you to read the code
But none of these points is a real problem The problem is that you may think your (yes, I renamed some variables)
while ( true ) { Socket ss = server.accept(); BufferedReader br = new BufferedReader( new InputStreamReader( ss.getInputStream() ) ); String alarmTimeFromSocket = br.readLine(); alarmTimeTextField.setText( alarmTimeFromSocket ); if ( alarmTimeFromSocket.endsWith( currentTime ) ) { color = JColorChooser.showDialog( GraphicsServer.this,color ); contentPane.setBackground( color ); } }
Often run, but this is not the case br. The readLine () call is a blocking call So thread has been waiting for input Once the new alarm time is received, it immediately executes the next section of code (comparison), and then starts waiting for input again
Therefore, your comparison code is executed only when you set the alarm time, not when the time is updated
Besides, I don't think endswith is correct (or you may not set a time, including the client's seconds, in which case it will work, but you can easily use equals instead of endswith)
One possible solution to the problem is to compare the alarm time with the current suspend time in the loop to update the suspend time on the server side However, if another thread updates the alert time field while comparing with the wall time in the wall time updater thread, you may end up with a strange threading problem