Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4101847
Votes 0
Synopsis Continuing UnknownHostException error
Category java:classes_net
Reported Against 1.2beta2
Release Fixed
State 11-Closed, duplicate of 4026938, bug
Priority: 3-Medium
Related Bugs 4026938
Submit Date 05-JAN-1998
Description




In java socket,

String host = "java.sun.com";
int port = 80;

try {
  Socket comm = new Socket(host, port);
} catch (UnknownHostException e) {
  System.out.println("UnknownHostException");
}

if there is some network line problem,
UnknownHostException error will arise.
Once network line problem removed,
expects no UnknownHostException.

But, persists UnknownHostException.
There is only one solution.
-> Restart application.

What's the problem?
Here is my source code.
You can replay it forcefully if you disconnect
LAN cable and get UnknownHostException, then
connect LAN cable and press Enter key in
TextField  customer .

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.util.*;

class GenericClassLoader extends ClassLoader
{
	Hashtable cache= new Hashtable();

	public synchronized Class loadClass(String name, boolean resolve) throws
					ClassNotFoundException {
		try {
			Class c=(Class)cache.get(name);
			if (c != null)
				return c;

			c = findSystemClass(name);
			if ( c != null) {
				cache.put(name, c);
				return c;
			}

			FileInputStream fis = new FileInputStream(name + ".class");
			File f = new File(name + ".class");
			byte data[] = new byte[(int)f.length()];
			fis.read(data);
			c = defineClass(name, data, 0, data.length);
			if (resolve)
				resolveClass(c);
			cache.put(name, c);
			return c;
		} catch (FileNotFoundException e) {
			System.out.println("File Not Found"+e);
		} catch (IOException e) {
		}
		return null;
	}
}

public class Generic extends Frame implements ActionListener
{
	// Constants
	static final int H_SIZE = 750;
	static final int V_SIZE = 560;
	MenuItem exitItem, item;
	TextField tf;

	// The Constructor

	public Generic(String className)
	{
		super("Stargate");

		Toolkit toolkit = Toolkit.getDefaultToolkit();
		setIconImage(toolkit.getImage("Image1.gif"));

		InitializeMenus();
		tf = new TextField("http://java.sun.com", 32);
		tf.addKeyListener(new KeyEventHandler());
		add(tf);
		pack();
		setSize(H_SIZE, V_SIZE);

		setBackground(Color.white);
		setForeground(Color.black);
		setLayout(new BorderLayout());

/*
		try {
			GenericClassLoader cl = new GenericClassLoader();
			Class c = cl.loadClass(className, true);
			Applet o = (Applet)c.newInstance();
			o.init();
			o.start();
			add(o);
		} catch (ClassNotFoundException e) {
			System.out.println("Exception...ClassNotFoundException");	
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		}
*/
		this.addWindowListener(new MyInnerClass());

		show();
	}
	private void InitializeMenus()
	{
		MenuBar mbar = new MenuBar();

		Menu m = new Menu("File");

		m.addSeparator();
		exitItem = new MenuItem("Exit");
		m.add(exitItem);
		exitItem.addActionListener(this);

		mbar.add(m);

		m = new Menu("Edit");
		item = new MenuItem("Preferences");
		m.add(item);
		mbar.add(m);

		m = new Menu("View");
		mbar.add(m);

		m = new Menu("Site");
		mbar.add(m);

		m = new Menu("Help");
		m.addSeparator();
		item = new MenuItem("About Stargate");
		m.add(item);
		mbar.add(m);

		setMenuBar(mbar);
	}
	class MyInnerClass extends WindowAdapter
	{
		public void windowClosing(WindowEvent event)
		{
			System.exit(0);
		}
	}
	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();
		if (src == exitItem) {
			System.exit(0);
		}
	}
	public void paint(Graphics g) {
	}
	public void GetHTML() throws java.io.IOException {
		Socket comm;
		DataOutputStream out;
		BufferedInputStream in;
		URL url;
		String host;
		int port;
		int nread, n;
		byte [] buf = new byte[1024];
		byte [] message = "GET / HTTP/1.1\r\n".getBytes();
		String html;
		StringBuffer strbuf = new StringBuffer("Host: ");

		try {
			url = new URL(tf.getText());
		} catch (MalformedURLException e) {
			System.out.println("MalformedURLException error");
			return;
		}
		host = url.getHost();
		port = url.getPort();

		strbuf.append(host);
		strbuf.append("\r\n");

		if (port == -1)
			port = 80;
		try {
			comm = new Socket(host, port);
		} catch (UnknownHostException e) {
			System.out.println("UnknownHostException error");
			return;
		}
		out = new DataOutputStream(comm.getOutputStream());
		in = new BufferedInputStream(comm.getInputStream());
		out.writeBytes("GET / HTTP/1.1\r\n");
		out.writeBytes(strbuf.toString());
		out.writeBytes("\r\n");
		out.flush();

		System.out.print("Request sent.\r\n");

		n = 0;
               	while (n >= 0) {
      			n = in.read(buf, 0, 1024);
               		if (n == -1)
               			break;
               		html = new String(buf, 0, n);
               		System.out.print(html);
               	}
               	comm.close();
	}
	class KeyEventHandler extends KeyAdapter
	{
		public void keyPressed(KeyEvent ke) {
			switch (ke.getKeyCode()) {
				case ke.VK_ENTER:
					try {
						GetHTML();
					} catch (IOException e) {
						System.out.println("IOException error");
					}
			}
		}
	}
	public static void main(String args[])
	{
		new Generic("OK");
	}
}
(Review ID: 22372)
======================================================================
Work Around
N/A
Evaluation
N/A
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang