473,662 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An Internet server application which returns the time to Internet clients

7 New Member
Hi Everyone,

I have written the code which gives the system time in a window. But I need also to have the Internet address and the port number of the client in the window. the TimeserverRF considers incoming DatagramPackets as a request for the current time, if the client wishes to receive the current time from TimeserverRF, it has only has to send a DatagramPacket to TimeserverRF.

I am not sure how to do this writing DatagramPackets .

I have the TimeClient class application. Timeclient should send the Internet address and the port on which it is running.

Do You have any ideas.

// the code for system time.

import javax.swing.*;

public class Window1 {

public static void main(String[] args) {

JFrame win = new JFrame("TimeSer ver running on port 9999");

JTextArea tA = new JTextArea(1, 30);

win.getContentP ane().add(tA);
win.pack();
win.setVisible( true);


String s1; // initialise variables
TimeData t1; // TimeData object initalise to t1 variable


t1 = new TimeData(System .currentTimeMil lis()); // start of loop body

s1 = t1.toTimeString (); // system time held sring s1


tA.append("Sent "); tA.append(s1); // Prints statement and time in Frame





}

}
Apr 24 '08 #1
2 1228
MarkoKlacar
296 Recognized Expert Contributor
Hi,

If I understood you correctly, you want to push out the current time to the clients connected?

Cheers
May 6 '08 #2
Nepomuk
3,112 Recognized Expert Specialist
Hi!
Is what you posted everything you have so far? If it is, do you know about Socket-Programming? You should, because you'll need it.

Here's a very simple example of how Sockets work: An Echo Server and Client. (So, the Echo Client sends the Server a String and the Server sends it back.)
Expand|Select|Wrap|Line Numbers
  1. // EchoServer.java
  2. // Run: java EchoServer
  3.  
  4. import java.net.*;
  5. import java.io.*;
  6. public class EchoServer 
  7. {
  8.     public static void main (String args[]) throws Exception
  9.     {
  10.         // Create a Server on Port 4242
  11.         ServerSocket server = new ServerSocket(4242);
  12.         Socket socket = null;
  13.  
  14.         // The Server should run for ever - cancel it with CRTL+C
  15.         while(true)
  16.         {
  17.             // Wait, until a client tries to connect
  18.             socket = server.accept();
  19.  
  20.             // Prepare to read whatever the Client sends
  21.             BufferedReader inStream = new BufferedReader(
  22.                     new InputStreamReader(
  23.                         socket.getInputStream()));
  24.             // Prepare to send back something to the Client
  25.             PrintStream outStream = new PrintStream(
  26.                     socket.getOutputStream());
  27.             // What does the Client say?
  28.             String input = inStream.readLine();
  29.             System.out.println("Echoing " + input);
  30.             // Send it back
  31.             outStream.println(input);
  32.             // Close the socket, as we are finished and you want to start again.
  33.             socket.close();
  34.         }
  35.     }
  36. }
  37.  
Expand|Select|Wrap|Line Numbers
  1. // EchoClient.java
  2. // Run: java EchoClient [hostname] message
  3.  
  4. import java.net .*;
  5. import java.io. *;
  6. public class EchoClient {
  7.     public static void main (String args[]) throws Exception
  8.     {
  9.         // Create new Connection to the IP given or the local computer on port 4242
  10.         Socket socket = new Socket ((args.length > 1 ? args[0] : "localhost"), 4242); 
  11.         // Prepare to read, what the Server sends back
  12.         BufferedReader inStream = new BufferedReader(
  13.                 new InputStreamReader(
  14.                     socket.getInputStream()));
  15.         // Prepare to send something to the server
  16.         PrintStream outStream = new PrintStream(
  17.             socket.getOutputStream());
  18.         // Send the given message (One word!) to the Server
  19.         outStream.println(args.length() > 1 ? args[1] : args[0]);
  20.         // Print, what the server sends back to you
  21.         System.out.println("Received: " + inStream.readLine());
  22.         // Close the socket, as we are finished
  23.         socket.close();
  24.     }
  25. }
  26.  
This is a very basic example, but I think, it's a good start.

If your Server should do more, like feature a website or do some wonderful things at some point in time, you may want to check out Tomcat instead. But if it's just this simple task, use Sockets.

Greetings,
Nepomuk

PS.: Please use code tags, when posting code.
May 6 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
2132
by: Daven Thrice | last post by:
I have a database that needs to be replicated over the Internet. I was hoping it would be a simple task, but apparently it is not. I've started browsing the msdn article on the topic. In the meantime, I am wondering who might be able to host a server on the Internet that meets such requirements. If anybody has had any experience with this, and/or if you can recommend a host who meets said requirements, I'd appreciate it if you'd post. ...
4
2342
by: Nicolás Castagnet | last post by:
Hi, I write this post because I notice a strange behavior related with "Temporary Internet Files" and maybe some of you can help me to understand it. I am working in a web application with ASP.NET. Recently, I group of user have problems with it because the values of the sessions were not stored correctly (the application save the username in a login page, then other page try to get it and the result was always ""). We restart the web...
12
3065
by: Paul Tillotson | last post by:
At my company we are looking at deploying clients for our client/server app outside our firewall, which will then require our postgres box to be internet-accessible. Does anyone out there have experience with this or recommended best practices? We have been looking at either (a) tunnelling everything over ssh, or (b) just making sure that users have "strong" passwords and requiring "md5" authentication in pg_hba.conf. Our client app...
1
3502
by: Apu Nahasapeemapetilon | last post by:
Hello and thank you in advance for your help. Can anyone think of a reason why this code would work properly on one PC, but not another? I've got a System.Windows.Forms.UserControl that products events which I want to consume (sink) within Internet Explorer. I'm following the instructions at: ms-http://support.microsoft.com/default.aspx?kbid=313891.
5
1468
by: Lyle Fairfield | last post by:
From time to time I have posted about the convenience of such hosts. Typically, they make available an MS-Sql Server database on their internet-enabled servers (See note below). I (and my clients) have been using these for a long time, since Access 2000 and ADPs came out which I think is six or seven years. I have pointed out as well that the great drawback to these hosts is that local backups are not available. While the host management...
13
1884
by: José Joye | last post by:
Hello, What is the best way to stop a remoting server (Singleton SAO) that has been started with RemotingConfiguration.Configure(). For sure, I want to do this without quitting the application :-) In fact, at a given time based on an internal event, I would like to stop the remoting functionality within my process. Many thanks,
0
1870
by: doron.grinstein | last post by:
A lot of architects tackle the issue of exposing internal web services and web applications to the Internet. How many times do you see a requirement such as "the application should be accessible to users behind the firewall (on the intranet) but also to users on the Internet"? Traditionally, architects had two choices - beg the network engineers and managers to "poke holes" in the firewall (unlikely), buy an expensive specialized...
24
5794
by: Asja | last post by:
I have written an application that runs in a kind of peer-to-peer framework. Servers connect to a masterserver, which a client then queries to receive a list of servers. The client selects a server and connects to that server directly and both the server and client sever their connections with the masterserver. Problem: I have the masterserver set up at a location and it is not behind a router or firewall or anything, just directly connected...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.