Avatar of Billy Ma
Billy MaFlag for Hong Kong asked on

Error Message in my Java Program

I don't know why I get the following error message when I run my Java program. In the Code Snippet, this is the function that I modified yesterday.

Cannot open Connection
The driver was unable to create a connection due to an inability to establish th
e client portion of a socket.

This is usually caused by a limit on the number of sockets imposed by the operat
ing system. This limit is usually configurable.

For Unix-based platforms, see the manual page for the 'ulimit' command. Kernel o
r system reconfiguration may also be required.

For Windows-based platforms, see Microsoft Knowledge Base Article 196271 (Q19627
1).
public void fetch_device_data(String device_name){
 
        String s = null;
		String line = "";
 
        try {
			
			try{
				FileOutputStream output_file = new FileOutputStream("output.txt");
				FileOutputStream error_file = new FileOutputStream("error.txt");
 
	            Process p = Runtime.getRuntime().exec("wget -O source_data/" + device_name + ".html http://****:****@61.10.0.168/cgi-bin/chkdocoper_chid.cgi?"+device_name);
				
				StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT", output_file);
				StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR", error_file);	
				
				outputGobbler.start();
				errorGobbler.start();
 
				int exitVal = p.waitFor();
	            System.out.println("ExitValue: " + exitVal);
				
				output_file.flush();
				output_file.close();
				
	            error_file.flush();
	            error_file.close();
				
			}catch(Throwable t){
				t.printStackTrace();
			}
 
			BufferedReader stdInput = new BufferedReader(new FileReader("source_data/" + device_name + ".html"));
			 
			boolean inside_table_tag = false;
			
            while ((s = stdInput.readLine()) != null) {
			
				/* If encounter a table tag */
				if(s.matches("<[/]{0,1}table[^>]*>")){
					if(inside_table_tag == true){
						inside_table_tag = false;
						
						line = line.replaceAll("<(tr|td)[^>]*>", "<$1>");		//Remove all the attributes in tr and td tags
						line = line.replaceAll("<([/]{0,1}[a-z]*)>[ ]*", "<$1>").trim();
						line = line.replaceAll("</(td|tr)>", "");				//Remove all ending tr and td tags
						line = line.replaceFirst("<td>", "");
						
						String data[] = line.split("<td>");
						
						insert_data(data);
						write_to_excel(data);
						
					}else if(inside_table_tag == false){
						inside_table_tag = true;
						line = "";
					}
				}
				
				/* If on a td line */
				if(inside_table_tag && s.contains("<td")){
					line += s + " ";
				}
 
            }
			
        }
        catch (IOException e) {
            write_log("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }
}
 
**** Code Edited for content 5-23-8 MM ****

Open in new window

Java

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon
ASKER
Billy Ma

Is it because the database connection did not close?
Mick Barry

for one you're not closing the file.
but why are you writing it to a file and then reading the file back?
And why use wget?

ASKER
Billy Ma

wget is a program to get the HTML source from a web page and write it into a file.

then I use my program to read the .html file and then process it.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Billy Ma

hey........because the webpage have .htaccess, is URLConnection able to pass through the .htaccess?
ASKER
Billy Ma

Please DELETE this question for me please because the source code contains the password...
Mick Barry

> hey........because the webpage have .htaccess, is URLConnection able to pass through the .htaccess?

yes, though I prefere httpclient as its more powerful.

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Billy Ma

is httpclient able to run under both Linux and Windows?
Mick Barry

yes, its a java library

http://hc.apache.org/httpclient-3.x/