LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2004, 04:09 PM   #1
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Java Stopping an Infinite loop


I've got a simple web server that uses an infinite loop to accept connections. This works fine, but now I want to be able to stop the server with a command from System.in to the server. Any ideas how to do this. Everytime I try to read from System.in if there is nothing there it just blocks. Here's the relevant code:

Code:
public static void main(String args[])throws Exception{

	String clientRequest = null;
	/* get the port number as the first argument */
	ServerSocket welcomeSocket = new ServerSocket(Integer.parseInt(args[0]));

	
		Socket connectionSocket;
		BufferedReader inFromClient;
		DataOutputStream outToClient;

		BufferedReader control = new BufferedReader(
			new InputStreamReader(System.in));

		
		
		

	while(true){
		/* loop continuously accepting connections */

		 connectionSocket = welcomeSocket.accept();

		/* create a BufferedReader object to read input */
		inFromClient =
				new BufferedReader(new InputStreamReader(
					connectionSocket.getInputStream()));

		/* create a DatatOutputStream object to write data */
		outToClient =
				new DataOutputStream(
					connectionSocket.getOutputStream());

		/* get the request from the client */
		clientRequest = inFromClient.readLine();
		
		/* create a Webserver object so we can manipulate the request */
		WebServer app = new WebServer();

		/* process the request */
       		app.processRequest(connectionSocket, inFromClient, outToClient, clientRequest);
		
		
		/* close the socket after the response is sent */
		connectionSocket.close();
		clientRequest = null;
		
		
	}

	

	}
}
Thanks for any help.
 
Old 10-18-2004, 07:59 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Why not stopping it with a custom HTTP command instead ?
 
Old 10-18-2004, 08:38 PM   #3
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
You could add a thread that's dedicated to gathering the input so the server thread can go about with it's business
Code:
import java.net.*;
import java.io.*;

public class server {
	//vars are out here so threads other than main thread can access them
	static BufferedReader control = new BufferedReader(
					new InputStreamReader(System.in));
	public static void main(String args[])throws Exception{

		Thread controlThread = new Thread(new Runnable() {
				public void run() {
					while (true) {
						try {
						String input = control.readLine();
						} catch (IOException e){}
						//blah blah, decide whether to exit or not
						//if (timeToExit)
							//cleanup
							System.exit(0);
					}
				}
			});

		controlThread.start();
		String clientRequest = null;
		/* get the port number as the first argument */
		ServerSocketwelcomeSocket = new ServerSocket(Integer.parseInt(args[0]));

		Socket connectionSocket;
		BufferedReader inFromClient;
		DataOutputStream outToClient;

		while(true){
			/* loop continuously accepting connections */

			 connectionSocket = welcomeSocket.accept();

			/* create a BufferedReader object to read input */
			inFromClient =
					new BufferedReader(new InputStreamReader(
						connectionSocket.getInputStream()));

			/* create a DatatOutputStream object to write data */
			outToClient =
					new DataOutputStream(
						connectionSocket.getOutputStream());

			/* get the request from the client */
			clientRequest = inFromClient.readLine();

			/* create a Webserver object so we can manipulate the request */
			WebServer app = new WebServer();

			/* process the request */
			app.processRequest(connectionSocket, inFromClient, outToClient, clientRequest);


			/* close the socket after the response is sent */
			connectionSocket.close();
			clientRequest = null;
		}
	}
}
 
Old 10-18-2004, 10:11 PM   #4
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Original Poster
Rep: Reputation: 30
aaa

That's exactly what I was looking for. Thanks for your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Infinite Loop ewt3y Programming 3 08-16-2005 09:48 AM
Linux Infinite Reboot Loop SNunweiler Linux - Software 2 05-10-2005 12:31 PM
infinite loop beginner_84 Programming 5 08-15-2004 02:32 AM
fetchmail stuck in infinite loop Prommy Linux - Software 0 02-17-2004 08:15 AM
Sendmail build - infinite loop?? kstarks Linux - Software 0 10-02-2003 07:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration