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?
java server connection
add a comment |
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?
java server connection
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
add a comment |
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?
java server connection
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
java server connection
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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