Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:42:25+00:00 2026-05-26T01:42:25+00:00

I have written a java socket server program which listens to a port continuously.

  • 0

I have written a java socket server program which listens to a port continuously. It creates a new text file for the incoming data but I want to create a new text file every 30 mins.

Can someone help me with scheduling this to run every 30 mins?

Thank you.

@paul: i have the following code:

import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.util.Timer;
import java.util.TimerTask;

public class DateServer extends Thread {

    static public String str;   

   public static void main(String args[]) {

       String pattern = "yyyyMMdd-hhmm"; 
        SimpleDateFormat format = new SimpleDateFormat (pattern); 
        str = format.format(new Date());
        int delay = 0;
        int period = 180000;
        Timer timer = new Timer();

        ServerSocket echoServer = null;
        String line = null;
        DataInputStream is;
        PrintStream os;
        Socket clientSocket = null;

        try {
           echoServer = new ServerSocket(3000);
        }
        catch (IOException e) {
           System.out.println(e);
        }   

        try {
           clientSocket = echoServer.accept();
           is = new DataInputStream(clientSocket.getInputStream());
           os = new PrintStream(clientSocket.getOutputStream());

           while (true) {
             line = is.readLine();
             os.println("From server: "+ line); 
             System.out.println(line);

             timer.scheduleAtFixedRate(new TimerTask() {
                 public void run(){
                     try{

        FileWriter fstream = new FileWriter("C://" +str+".txt",true);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(line);
        out.newLine();
        out.flush(); 
        out.close();
          }catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
                    }

                 }
             }}, delay, period);           
        }   
        catch (IOException e) {
           System.out.println(e);
    }        
    }
}
  1. At timer.scheduleAtFixedRate(new TimerTask() this line it is giving me the following error:

    [no suitable method found for scheduleAtFixedRate()
    method java.util.Timer.scheduleAtFixedRate(java.util.TimerTask,java.util.Date,long) is not applicable
    (actual and formal argument lists differ in length)
    method java.util.Timer.scheduleAtFixedRate(java.util.TimerTask,long,long) is not applicable
    (actual and formal argument lists differ in length)]

  2. at line = is.readLine(); it is giving me the following error:

    [cannot assign a value to final variable line].

I am new to java. i am sorry for the terrible indentation. please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:42:26+00:00Added an answer on May 26, 2026 at 1:42 am

    Your server simply needs to create a timed interval that fires off every thirty minutes and creates the file. See here for an example, the Java docs and another example.

    Here’s the code snippet with a few mods for your situation:

    int delay = 0;   // delay for - no delay
    int period = 1800000;  // repeat every 1.8 mil milliseconds = 30 minutes
    Timer timer = new Timer();
    
    timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                // Create file here
            }
        }, delay, period);
    

    And fixed up code:

    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.text.*;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class DateServer extends Thread {
        public static void main(String args[]) {
            new Runner().go();
        }
    }
    
    class Runner {
        public static  LinkedList<String> data = new LinkedList<String>();
    
        public void go() {
            ServerSocket echoServer = null;
    
            MyTimerTask timerTask = new MyTimerTask();
            new Timer().scheduleAtFixedRate(timerTask, 0, 2000);
    
            try {
                echoServer = new ServerSocket(3000);
            }
            catch (IOException e) {
                System.out.println(e);
            }
    
            try {
                Socket clientSocket = echoServer.accept();
                DataInputStream is = new DataInputStream(clientSocket.getInputStream());
                PrintStream os = new PrintStream(clientSocket.getOutputStream());
    
                while (true) {
                    String line = is.readLine();
                    data.add(line);
                    os.println("From server: "+ line);
                    System.out.println(line);
                }
            }
            catch (IOException e) {
                System.out.println(e);
            }
        }
    }
    
    class MyTimerTask extends TimerTask {
        public void run() {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-hhmm");
            String line = null;
            System.out.print(".");
            try {
                String str = format.format(new Date());
                FileWriter fstream = new FileWriter("C://" +str+".txt",true);
                BufferedWriter out = new BufferedWriter(fstream);
                while (Runner.data.size() > 0) out.write(Runner.data.getLast());
                out.newLine();
                out.flush();
                out.close();
            } catch (Exception e) {//Catch exception if any
                System.err.println("Error: " + e.getMessage() + e.getStackTrace()[0].toString());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program in Java which creates a socket connection for a
I have a very simple client server code written java(server listens on some port
Hey all. I have a server written in java using the ServerSocket and Socket
I have written a java program named Automate.java, in which the another java program
I have written the java code below, which executes another java program named Newsworthy_RB.
I have a single-threaded non-blocking socket IO server written in Java using nio. When
i have written the following code to download file. java.io.BufferedInputStream in = new java.io.BufferedInputStream(new
I have written a small java application for which I need to obtain performance
I have a server written in Java that runs as a Windows service (thanks
As a Christmas gift I have written a small program in Java to calculate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.