java一直在运行应用程序,并且只要他连接就向客户端发送数据?

时间:2018-11-11 15:03:40

标签: java server connection

我正在尝试执行以下操作:

一直计算我的设备的运行时间,每当客户端连接时,他都会向他展示设备,直到他断开网络为止。

该怎么做?

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();
        }
    }
}

但是他只是开始向我显示用户连接的时间

为什么?

1 个答案:

答案 0 :(得分:1)

服务器代码:

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());
         }
      }
   }
}

客户代码:

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 );
         }
      }
   }
}

输出,服务器端:

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

输出,客户端:

$ 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