This discussion is archived
3 Replies Latest reply: Mar 23, 2011 12:25 AM by 796440 RSS

Error while creating Multithreaded DNS logger

849545 Newbie
Currently Being Moderated
I am new to Java programming I am getting this error called

Exception in thread "Thread-0" java.util.NoSuchElementException
     at java.util.LinkedList.remove(LinkedList.java:805)
     at java.util.LinkedList.removeFirst(LinkedList.java:151)
     at java.util.LinkedList.remove(LinkedList.java:498)
     at getIPAddress.appendTextFile(prog8.java:135)
     at getIPAddress.run(prog8.java:66)
     at java.lang.Thread.run(Thread.java:636)

Below is the program.


//You should have a very neat dnsServer.txt file.
// Format for file should follow below
// SERVER START putdate
// CLIENT DATE putdate CONNECT PORT num IP num
// CLIENT DATE putdate REQUEST url_requested
// CLIENT DATE putdate CLOSE
// You should have one thread that is the writer to the file
// that is spawned first by the main.
// HINT: If clientNo ==1, run method should run logger code
// else it is a client connecting
// You have one queue of strings. Clients put onto the queue,
// the logger writes to the file. No thread can access the
// queue while another is accessing the queue
//

import java.net.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.text.SimpleDateFormat;

//Runnable provides the means for a class to be active while not subclassing thread.
class getIPAddress implements Runnable {
private InetAddress hostIPAddress;
private Socket createSocket;
private String IPAddress;
private int clNum;


//Implementing queue
private static LinkedList<String> fileQueue = new LinkedList<String>();

public getIPAddress( Socket sockNum, int num ) {

if (num == 0) {
clNum = num;

}
else {
try {
createSocket = sockNum;
clNum = num;
}
catch (Exception output) {
System.out.println("Failed to get socket connection");
}
clNum = num;
}//end of else
}//end of getIPAddress

public void run() {
// the run method executes independently in each thread
// It also creates input buffer and output writer
SimpleDateFormat time = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
String timenDate = time.format(date);
String clientEntry;
String hostIPAddressStringed;
String storeQueue;


if (clNum == 0) {
appendTextFile();
}

else if(clNum == 1) {
storeQueue = " CLIENT " + clNum + ": DATE: " + timenDate +
" CONNECT PORT " + createSocket.getPort() +
" IP " + createSocket.getInetAddress() + "\n";
//storing whatever's in storeQueue into fileQueue
//synchronized makes sure that only one thread is writing to the
//queue at a time.
synchronized (fileQueue) {
fileQueue.add(storeQueue);
}
}//end of else if (clNum == 1)
else {
String choice ="y";
while (choice.equals("y") || choice.equals("Y")) {
try{
BufferedReader clientSendKbd = new BufferedReader( new InputStreamReader(
createSocket.getInputStream())); // Getting input from keyboard clt
DataOutputStream clientReceiveScreen = new DataOutputStream(
createSocket.getOutputStream()); // Sending output to the screen clt
clientReceiveScreen.writeBytes("Enter address: ");// Get web address
hostIPAddress = InetAddress.getByName(clientSendKbd.readLine());
IPAddress = hostIPAddress.getHostAddress();//get IP address
clientReceiveScreen.writeBytes("IP Address: " + IPAddress);
storeQueue =" CLIENT " + clNum + ": DATE: " + timenDate +
" REQUEST: " + hostIPAddress.getHostName() + "\n";
synchronized (fileQueue) {
fileQueue.add(storeQueue);
}

clientReceiveScreen.writeBytes("\nWould you like to repeat?\n(y or Y to repeat): ");
choice = (clientSendKbd.readLine());
} catch (Exception ioe) {
System.out.println("Wrong choice " + ioe);
}
}//end of while
}//end of else
storeQueue = " CLIENT " + clNum + ": DATE: " + timenDate +
" CLOSED" + "\n";
synchronized (fileQueue) {
fileQueue.add(storeQueue);
}
try {
createSocket.close();
}
catch(Exception err) {
System.out.println("The error " + err);
}
}//end of run()

public void appendTextFile( ) {
SimpleDateFormat time = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
String timenDate = time.format(date);
String removedFromQueue = null;
try {
FileWriter fstream = new FileWriter("dnsServer.txt", true);
BufferedWriter buffer = new BufferedWriter(fstream);
buffer.write(" SERVER START " + timenDate + "\n");
do {
synchronized (fileQueue) {removedFromQueue = fileQueue.remove(); }
buffer.write(removedFromQueue);
buffer.flush();
}
while(!(fileQueue.isEmpty()));
fstream.close();
}//end of try
catch(IOException ioe) {
System.out.println(" Fail: " + ioe);
}
}//end of appendTextFile
}//end of getIPAddress


public class prog8{
public static void main(String[] args) {
int port = -1;
int num = 0;
if(args.length > 0) {
try {
port = Integer.parseInt(args[0].trim());
}
catch (Exception out) {
System.out.println("Wrong value: "+ out);
port = -1;
}
}
while (port < 1024 || port > 65536) {// check the port number
BufferedReader keyboardInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number between 1024 and 65536 ");
try {
port = Integer.parseInt(keyboardInput.readLine());
}
catch (Exception outkb) {
System.out.println("Wrong entry from KB: " + outkb);
port = -1;
}
}//end of while
try {
ServerSocket sockNum = new ServerSocket(port);
getIPAddress loggerThread = new getIPAddress(null, num);//Thread for logger
new Thread(loggerThread).start();
while (true) {
Socket clientNum = null;
num++;
try {
clientNum = sockNum.accept();
}
catch (Exception outsock){
System.out.println("Wrong : " + outsock);
}

getIPAddress IP = new getIPAddress(clientNum, num);//Thread for socket IP
new Thread(IP).start();
}//end of while
}//try block
catch (IOException ioutput) {
System.out.println("There is error" + ioutput);
}
}//end of main
}//end of prog5

Edited by: 846542 on Mar 22, 2011 5:19 PM
  • 1. Re: Error while creating Multithreaded DNS logger
    tschodt Pro
    Currently Being Moderated
    846542 wrote:
    I am new to Java programming I am getting this error called
    Exception in thread "Thread-0" java.util.NoSuchElementException
         at java.util.LinkedList.remove(LinkedList.java:805)
         at java.util.LinkedList.removeFirst(LinkedList.java:151)
         at java.util.LinkedList.remove(LinkedList.java:498)
         at getIPAddress.appendTextFile(prog8.java:135)
         at getIPAddress.run(prog8.java:66)
         at java.lang.Thread.run(Thread.java:636)
    What do you think happens if fileQueue is empty when you call appendTextFile() ?
    Below is the program.
    //You should have a very neat dnsServer.txt file.
    //         Format for file should follow below
    //         SERVER START putdate
    //         CLIENT DATE putdate CONNECT PORT num IP num
    //         CLIENT DATE putdate REQUEST url_requested
    //         CLIENT DATE putdate CLOSE
    //         You should have one thread that is the writer to the file
    //         that is spawned first by the main.  
    //       HINT: If clientNo ==1, run method should run logger code
    //      else it is a client connecting
    //      You have one queue of strings.  Clients put onto the queue,
    //      the logger writes to the file.  No thread can access the 
    //      queue while another is accessing the queue
    //
    
    import java.net.*;
    import java.lang.*;
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.text.SimpleDateFormat;
    
    //Runnable provides the means for a class to be active while not subclassing thread.
    class getIPAddress implements Runnable {
    private InetAddress hostIPAddress;
    private Socket createSocket;
    private String IPAddress;
    private int clNum;
    
    
    //Implementing queue 
    private static LinkedList<String> fileQueue = new LinkedList<String>();
    
    public getIPAddress( Socket sockNum, int num ) {
    
    if (num == 0) {
    clNum = num;
    
    }
    else {
    try {
    createSocket = sockNum;
    clNum = num;
    }
    catch (Exception output) {
    System.out.println("Failed to get socket connection");
    }
    clNum = num;
    }//end of else
    }//end of getIPAddress
    
    public void run() {
    // the run method executes independently in each thread
    // It also creates input buffer and output writer
    SimpleDateFormat time = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    Date date = new Date();
    String timenDate = time.format(date);
    String clientEntry;
    String hostIPAddressStringed;
    String storeQueue;
    
    
    if (clNum == 0) {
    appendTextFile();
    }
    
    else if(clNum == 1) {
    storeQueue = " CLIENT " + clNum + ": DATE: " + timenDate +
    " CONNECT PORT " + createSocket.getPort() +
    " IP " + createSocket.getInetAddress() + "\n";
    //storing whatever's in storeQueue into fileQueue
    //synchronized makes sure that only one thread is writing to the
    //queue at a time.
    synchronized (fileQueue) {
    fileQueue.add(storeQueue);
    }
    }//end of else if (clNum == 1)
    else {
    String choice ="y";
    while (choice.equals("y") || choice.equals("Y")) {
    try{
    BufferedReader clientSendKbd = new BufferedReader( new InputStreamReader(
    createSocket.getInputStream())); // Getting input from keyboard clt
    DataOutputStream clientReceiveScreen = new DataOutputStream(
    createSocket.getOutputStream()); // Sending output to the screen clt
    clientReceiveScreen.writeBytes("Enter address: ");// Get web address
    hostIPAddress = InetAddress.getByName(clientSendKbd.readLine());
    IPAddress = hostIPAddress.getHostAddress();//get IP address
    clientReceiveScreen.writeBytes("IP Address: " + IPAddress);
    storeQueue =" CLIENT " + clNum + ": DATE: " + timenDate +
    " REQUEST: " +  hostIPAddress.getHostName() + "\n";
    synchronized (fileQueue) {
    fileQueue.add(storeQueue);
    }
    
    clientReceiveScreen.writeBytes("\nWould you like to repeat?\n(y or Y to repeat): ");
    choice = (clientSendKbd.readLine());
    } catch (Exception ioe) {
    System.out.println("Wrong choice " + ioe);
    }
    }//end of while
    }//end of else
    storeQueue = " CLIENT " + clNum + ": DATE: " + timenDate +
    " CLOSED" +  "\n";
    synchronized (fileQueue) {
    fileQueue.add(storeQueue);
    }
    try {
    createSocket.close();
    }
    catch(Exception err) {
    System.out.println("The error " + err);
    }
    }//end of run()
    
    public void appendTextFile( ) {
    SimpleDateFormat time = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    Date date = new Date();
    String timenDate = time.format(date);
    String removedFromQueue = null;
    try {
    FileWriter fstream = new FileWriter("dnsServer.txt", true);
    BufferedWriter buffer = new BufferedWriter(fstream);
    buffer.write(" SERVER START " + timenDate + "\n");
    do {
    synchronized (fileQueue) {removedFromQueue = fileQueue.remove(); }
    buffer.write(removedFromQueue);
    buffer.flush();
    }
    while(!(fileQueue.isEmpty()));
    fstream.close();
    }//end of try
    catch(IOException ioe) {
    System.out.println(" Fail: " + ioe);
    }
    }//end of appendTextFile
    }//end of getIPAddress
    
    
    public class  prog8{
    public static void main(String[] args) {
    int port = -1;
    int num = 0;
    if(args.length > 0) {
    try {
    port = Integer.parseInt(args[0].trim());
    }
    catch (Exception out) {
    System.out.println("Wrong value: "+ out);
    port = -1;
    }
    }
    while (port < 1024 || port > 65536) {// check the port number
    BufferedReader keyboardInput = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter number between 1024 and 65536 ");
    try {
    port = Integer.parseInt(keyboardInput.readLine());
    }
    catch (Exception outkb) {
    System.out.println("Wrong entry from KB: " + outkb);
    port = -1;
    }
    }//end of while
    try {
    ServerSocket sockNum = new ServerSocket(port);
    getIPAddress loggerThread = new getIPAddress(null, num);//Thread for logger
    new Thread(loggerThread).start();
    while (true) {
    Socket clientNum = null;
    num++;
    try {
    clientNum = sockNum.accept();
    }
    catch (Exception outsock){
    System.out.println("Wrong : " + outsock);
    }
    
    getIPAddress IP = new getIPAddress(clientNum, num);//Thread for socket IP
    new Thread(IP).start();
    }//end of while
    }//try block
    catch (IOException ioutput) {
    System.out.println("There is error" + ioutput);
    }
    }//end of main
    }//end of prog5
    Edited by: 846542 on Mar 22, 2011 5:19 PM
  • 2. Re: Error while creating Multithreaded DNS logger
    849545 Newbie
    Currently Being Moderated
    What do you think happens if fileQueue is empty when you call appendTextFile() ?

    ------
    So that's due to empty fileQueue?? I haven't been able to figure out much.
  • 3. Re: Error while creating Multithreaded DNS logger
    796440 Guru
    Currently Being Moderated
    846542 wrote:
    What do you think happens if fileQueue is empty when you call appendTextFile() ?

    ------
    So that's due to empty fileQueue?? I haven't been able to figure out much.
    Well...

    If you call removeFirst and the queue is empty--that is, there is no first element, no element at all--what would you expect to happen?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points