java run application all the time and send data to client whenever he connected?









up vote
1
down vote

favorite












I'm trying to do the follow :



calculate the running time of my device all the time and and whenever a client connect he will show it to him , until he disconnet.



how do to this ?



import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

public class MyServer
public static void main(String args)
connectToServer();


static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void connectToServer()
try(ServerSocket serverSocket = new ServerSocket(9991))
Socket connectionSocket = serverSocket.accept();
InputStream inputToServer = connectionSocket.getInputStream();
OutputStream outputFromServer = connectionSocket.getOutputStream();
Scanner scanner = new Scanner( inputToServer, "UTF-8" );
PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "UTF-8" ), true );
serverPrintOut.println("Welcome to time server ");
while(true)
long EndTime = System.currentTimeMillis();
long Total = EndTime - StartTime;
serverPrintOut.println(Total);
System.out.println(Total);
try
Thread.sleep(1000);

catch(InterruptedException ex)
Thread.currentThread().interrupt();


catch (IOException e)
e.printStackTrace();





but he only start show me the time when a user is connected



why?










share|improve this question























  • Your forever loop prevent any other client to connect : after an accept() you have to treat the newly client into a dedicated thread.
    – Aubin
    Nov 11 at 15:11










  • can you show me how?
    – David12123
    Nov 11 at 15:27














up vote
1
down vote

favorite












I'm trying to do the follow :



calculate the running time of my device all the time and and whenever a client connect he will show it to him , until he disconnet.



how do to this ?



import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

public class MyServer
public static void main(String args)
connectToServer();


static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void connectToServer()
try(ServerSocket serverSocket = new ServerSocket(9991))
Socket connectionSocket = serverSocket.accept();
InputStream inputToServer = connectionSocket.getInputStream();
OutputStream outputFromServer = connectionSocket.getOutputStream();
Scanner scanner = new Scanner( inputToServer, "UTF-8" );
PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "UTF-8" ), true );
serverPrintOut.println("Welcome to time server ");
while(true)
long EndTime = System.currentTimeMillis();
long Total = EndTime - StartTime;
serverPrintOut.println(Total);
System.out.println(Total);
try
Thread.sleep(1000);

catch(InterruptedException ex)
Thread.currentThread().interrupt();


catch (IOException e)
e.printStackTrace();





but he only start show me the time when a user is connected



why?










share|improve this question























  • Your forever loop prevent any other client to connect : after an accept() you have to treat the newly client into a dedicated thread.
    – Aubin
    Nov 11 at 15:11










  • can you show me how?
    – David12123
    Nov 11 at 15:27












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to do the follow :



calculate the running time of my device all the time and and whenever a client connect he will show it to him , until he disconnet.



how do to this ?



import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

public class MyServer
public static void main(String args)
connectToServer();


static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void connectToServer()
try(ServerSocket serverSocket = new ServerSocket(9991))
Socket connectionSocket = serverSocket.accept();
InputStream inputToServer = connectionSocket.getInputStream();
OutputStream outputFromServer = connectionSocket.getOutputStream();
Scanner scanner = new Scanner( inputToServer, "UTF-8" );
PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "UTF-8" ), true );
serverPrintOut.println("Welcome to time server ");
while(true)
long EndTime = System.currentTimeMillis();
long Total = EndTime - StartTime;
serverPrintOut.println(Total);
System.out.println(Total);
try
Thread.sleep(1000);

catch(InterruptedException ex)
Thread.currentThread().interrupt();


catch (IOException e)
e.printStackTrace();





but he only start show me the time when a user is connected



why?










share|improve this question















I'm trying to do the follow :



calculate the running time of my device all the time and and whenever a client connect he will show it to him , until he disconnet.



how do to this ?



import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

public class MyServer
public static void main(String args)
connectToServer();


static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void connectToServer()
try(ServerSocket serverSocket = new ServerSocket(9991))
Socket connectionSocket = serverSocket.accept();
InputStream inputToServer = connectionSocket.getInputStream();
OutputStream outputFromServer = connectionSocket.getOutputStream();
Scanner scanner = new Scanner( inputToServer, "UTF-8" );
PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "UTF-8" ), true );
serverPrintOut.println("Welcome to time server ");
while(true)
long EndTime = System.currentTimeMillis();
long Total = EndTime - StartTime;
serverPrintOut.println(Total);
System.out.println(Total);
try
Thread.sleep(1000);

catch(InterruptedException ex)
Thread.currentThread().interrupt();


catch (IOException e)
e.printStackTrace();





but he only start show me the time when a user is connected



why?







java server connection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 15:11









Aubin

11.1k63964




11.1k63964










asked Nov 11 at 15:03









David12123

599




599











  • Your forever loop prevent any other client to connect : after an accept() you have to treat the newly client into a dedicated thread.
    – Aubin
    Nov 11 at 15:11










  • can you show me how?
    – David12123
    Nov 11 at 15:27
















  • Your forever loop prevent any other client to connect : after an accept() you have to treat the newly client into a dedicated thread.
    – Aubin
    Nov 11 at 15:11










  • can you show me how?
    – David12123
    Nov 11 at 15:27















Your forever loop prevent any other client to connect : after an accept() you have to treat the newly client into a dedicated thread.
– Aubin
Nov 11 at 15:11




Your forever loop prevent any other client to connect : after an accept() you have to treat the newly client into a dedicated thread.
– Aubin
Nov 11 at 15:11












can you show me how?
– David12123
Nov 11 at 15:27




can you show me how?
– David12123
Nov 11 at 15:27












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Server Code:



import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;

class ClientThread extends Thread

private final Socket _socket;

public ClientThread( Socket socket )
System.out.println( "New client" );
_socket = socket;
setDaemon( true );
start();


@Override
public void run()
try(
final OutputStream outputFromServer = _socket.getOutputStream();
final PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "utf-8" ), true ))

serverPrintOut.println( "Welcome to time server" );
for(;;)
final long elapsed = System.currentTimeMillis() - MyServer.StartTime;
serverPrintOut.println( elapsed );
Thread.sleep( 1000L );


catch( final InterruptedException ex) /**/
catch( final IOException e )
e.printStackTrace();




public class MyServer

static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void main( String args ) throws IOException
try( ServerSocket serverSocket = new ServerSocket( 9991 ))
for(;;)
new ClientThread( serverSocket.accept());






Client code:



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class MyClient

public static void main( String args ) throws IOException
try(
Socket socket = new Socket( args[0], 9991 );
BufferedReader br = new BufferedReader(
new InputStreamReader( socket.getInputStream(), "utf-8" )))

String line;
while(( line = br.readLine()) != null )
System.out.println( line );






Output, server side:



$ java -cp bin so.MyServer 
New client
New client


Output, client side:



$ java -cp bin so.MyClient localhost
Welcome to time server
3274
4274
5275
6275
7275
8276
9276
10276
11276
^C <---- End of first client

$ java -cp bin so.MyClient localhost
Welcome to time server
19838
20838
21838
^C <---- End of second client





share|improve this answer




















  • Thanks , I do have a follow up question : I need to embedded the server code you show me in a working code that read data from sensors. now all the data is save local as string and once every 10 seconds I save it to a file and upload it. what would be the best way to do this?
    – David12123
    Nov 12 at 8:33










  • Send me your credit card number, please....
    – Aubin
    Nov 12 at 19:05










  • :) , just wanted to know in general how to do this..... :-)
    – David12123
    Nov 13 at 8:13










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250008%2fjava-run-application-all-the-time-and-send-data-to-client-whenever-he-connected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Server Code:



import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;

class ClientThread extends Thread

private final Socket _socket;

public ClientThread( Socket socket )
System.out.println( "New client" );
_socket = socket;
setDaemon( true );
start();


@Override
public void run()
try(
final OutputStream outputFromServer = _socket.getOutputStream();
final PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "utf-8" ), true ))

serverPrintOut.println( "Welcome to time server" );
for(;;)
final long elapsed = System.currentTimeMillis() - MyServer.StartTime;
serverPrintOut.println( elapsed );
Thread.sleep( 1000L );


catch( final InterruptedException ex) /**/
catch( final IOException e )
e.printStackTrace();




public class MyServer

static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void main( String args ) throws IOException
try( ServerSocket serverSocket = new ServerSocket( 9991 ))
for(;;)
new ClientThread( serverSocket.accept());






Client code:



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class MyClient

public static void main( String args ) throws IOException
try(
Socket socket = new Socket( args[0], 9991 );
BufferedReader br = new BufferedReader(
new InputStreamReader( socket.getInputStream(), "utf-8" )))

String line;
while(( line = br.readLine()) != null )
System.out.println( line );






Output, server side:



$ java -cp bin so.MyServer 
New client
New client


Output, client side:



$ java -cp bin so.MyClient localhost
Welcome to time server
3274
4274
5275
6275
7275
8276
9276
10276
11276
^C <---- End of first client

$ java -cp bin so.MyClient localhost
Welcome to time server
19838
20838
21838
^C <---- End of second client





share|improve this answer




















  • Thanks , I do have a follow up question : I need to embedded the server code you show me in a working code that read data from sensors. now all the data is save local as string and once every 10 seconds I save it to a file and upload it. what would be the best way to do this?
    – David12123
    Nov 12 at 8:33










  • Send me your credit card number, please....
    – Aubin
    Nov 12 at 19:05










  • :) , just wanted to know in general how to do this..... :-)
    – David12123
    Nov 13 at 8:13














up vote
1
down vote



accepted










Server Code:



import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;

class ClientThread extends Thread

private final Socket _socket;

public ClientThread( Socket socket )
System.out.println( "New client" );
_socket = socket;
setDaemon( true );
start();


@Override
public void run()
try(
final OutputStream outputFromServer = _socket.getOutputStream();
final PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "utf-8" ), true ))

serverPrintOut.println( "Welcome to time server" );
for(;;)
final long elapsed = System.currentTimeMillis() - MyServer.StartTime;
serverPrintOut.println( elapsed );
Thread.sleep( 1000L );


catch( final InterruptedException ex) /**/
catch( final IOException e )
e.printStackTrace();




public class MyServer

static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void main( String args ) throws IOException
try( ServerSocket serverSocket = new ServerSocket( 9991 ))
for(;;)
new ClientThread( serverSocket.accept());






Client code:



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class MyClient

public static void main( String args ) throws IOException
try(
Socket socket = new Socket( args[0], 9991 );
BufferedReader br = new BufferedReader(
new InputStreamReader( socket.getInputStream(), "utf-8" )))

String line;
while(( line = br.readLine()) != null )
System.out.println( line );






Output, server side:



$ java -cp bin so.MyServer 
New client
New client


Output, client side:



$ java -cp bin so.MyClient localhost
Welcome to time server
3274
4274
5275
6275
7275
8276
9276
10276
11276
^C <---- End of first client

$ java -cp bin so.MyClient localhost
Welcome to time server
19838
20838
21838
^C <---- End of second client





share|improve this answer




















  • Thanks , I do have a follow up question : I need to embedded the server code you show me in a working code that read data from sensors. now all the data is save local as string and once every 10 seconds I save it to a file and upload it. what would be the best way to do this?
    – David12123
    Nov 12 at 8:33










  • Send me your credit card number, please....
    – Aubin
    Nov 12 at 19:05










  • :) , just wanted to know in general how to do this..... :-)
    – David12123
    Nov 13 at 8:13












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Server Code:



import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;

class ClientThread extends Thread

private final Socket _socket;

public ClientThread( Socket socket )
System.out.println( "New client" );
_socket = socket;
setDaemon( true );
start();


@Override
public void run()
try(
final OutputStream outputFromServer = _socket.getOutputStream();
final PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "utf-8" ), true ))

serverPrintOut.println( "Welcome to time server" );
for(;;)
final long elapsed = System.currentTimeMillis() - MyServer.StartTime;
serverPrintOut.println( elapsed );
Thread.sleep( 1000L );


catch( final InterruptedException ex) /**/
catch( final IOException e )
e.printStackTrace();




public class MyServer

static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void main( String args ) throws IOException
try( ServerSocket serverSocket = new ServerSocket( 9991 ))
for(;;)
new ClientThread( serverSocket.accept());






Client code:



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class MyClient

public static void main( String args ) throws IOException
try(
Socket socket = new Socket( args[0], 9991 );
BufferedReader br = new BufferedReader(
new InputStreamReader( socket.getInputStream(), "utf-8" )))

String line;
while(( line = br.readLine()) != null )
System.out.println( line );






Output, server side:



$ java -cp bin so.MyServer 
New client
New client


Output, client side:



$ java -cp bin so.MyClient localhost
Welcome to time server
3274
4274
5275
6275
7275
8276
9276
10276
11276
^C <---- End of first client

$ java -cp bin so.MyClient localhost
Welcome to time server
19838
20838
21838
^C <---- End of second client





share|improve this answer












Server Code:



import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;

class ClientThread extends Thread

private final Socket _socket;

public ClientThread( Socket socket )
System.out.println( "New client" );
_socket = socket;
setDaemon( true );
start();


@Override
public void run()
try(
final OutputStream outputFromServer = _socket.getOutputStream();
final PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "utf-8" ), true ))

serverPrintOut.println( "Welcome to time server" );
for(;;)
final long elapsed = System.currentTimeMillis() - MyServer.StartTime;
serverPrintOut.println( elapsed );
Thread.sleep( 1000L );


catch( final InterruptedException ex) /**/
catch( final IOException e )
e.printStackTrace();




public class MyServer

static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();

public static void main( String args ) throws IOException
try( ServerSocket serverSocket = new ServerSocket( 9991 ))
for(;;)
new ClientThread( serverSocket.accept());






Client code:



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class MyClient

public static void main( String args ) throws IOException
try(
Socket socket = new Socket( args[0], 9991 );
BufferedReader br = new BufferedReader(
new InputStreamReader( socket.getInputStream(), "utf-8" )))

String line;
while(( line = br.readLine()) != null )
System.out.println( line );






Output, server side:



$ java -cp bin so.MyServer 
New client
New client


Output, client side:



$ java -cp bin so.MyClient localhost
Welcome to time server
3274
4274
5275
6275
7275
8276
9276
10276
11276
^C <---- End of first client

$ java -cp bin so.MyClient localhost
Welcome to time server
19838
20838
21838
^C <---- End of second client






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 16:38









Aubin

11.1k63964




11.1k63964











  • Thanks , I do have a follow up question : I need to embedded the server code you show me in a working code that read data from sensors. now all the data is save local as string and once every 10 seconds I save it to a file and upload it. what would be the best way to do this?
    – David12123
    Nov 12 at 8:33










  • Send me your credit card number, please....
    – Aubin
    Nov 12 at 19:05










  • :) , just wanted to know in general how to do this..... :-)
    – David12123
    Nov 13 at 8:13
















  • Thanks , I do have a follow up question : I need to embedded the server code you show me in a working code that read data from sensors. now all the data is save local as string and once every 10 seconds I save it to a file and upload it. what would be the best way to do this?
    – David12123
    Nov 12 at 8:33










  • Send me your credit card number, please....
    – Aubin
    Nov 12 at 19:05










  • :) , just wanted to know in general how to do this..... :-)
    – David12123
    Nov 13 at 8:13















Thanks , I do have a follow up question : I need to embedded the server code you show me in a working code that read data from sensors. now all the data is save local as string and once every 10 seconds I save it to a file and upload it. what would be the best way to do this?
– David12123
Nov 12 at 8:33




Thanks , I do have a follow up question : I need to embedded the server code you show me in a working code that read data from sensors. now all the data is save local as string and once every 10 seconds I save it to a file and upload it. what would be the best way to do this?
– David12123
Nov 12 at 8:33












Send me your credit card number, please....
– Aubin
Nov 12 at 19:05




Send me your credit card number, please....
– Aubin
Nov 12 at 19:05












:) , just wanted to know in general how to do this..... :-)
– David12123
Nov 13 at 8:13




:) , just wanted to know in general how to do this..... :-)
– David12123
Nov 13 at 8:13

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250008%2fjava-run-application-all-the-time-and-send-data-to-client-whenever-he-connected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3