I’m having a problem connecting to a socket server within Java, using a hostname, and port number. However, I’ve never done any socket’s, nor something like a chat room, server - client - applet relationship using Java.
Here’s the part I’m not sure with:
public void run () {
try {
String host = getParameter ("localhost");
if (host == null)
host = getCodeBase ().getHost ();
String port = getParameter ("port");
if (port == null)
port = "9999";
output.appendText ("Connecting to " + host + ":" + port + "...");
Socket s = new Socket (host, Integer.parseInt (port));
i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));
output.appendText (" connected.\
");
input.setEditable (true);
input.requestFocus ();
execute ();
} catch (IOException ex) {
ByteArrayOutputStream out = new ByteArrayOutputStream ();
ex.printStackTrace (new PrintStream (out));
output.appendText ("\
" + out);
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
// Applet parameters:
// host = host name
// port = host port
public class ChatApplet extends Applet implements Runnable {
protected DataInputStream i;
protected DataOutputStream o;
protected TextArea output;
protected TextField input;
protected Thread listener;
public void init () {
setLayout (new BorderLayout ());
add ("Center", output = new TextArea ());
output.setEditable (false);
add ("South", input = new TextField ());
input.setEditable (false);
}
public void start () {
listener = new Thread (this);
listener.start ();
}
public void stop () {
if (listener != null)
listener.stop ();
listener = null;
}
public void run () {
try {
String host = getParameter ("localhost");
if (host == null)
host = getCodeBase ().getHost ();
String port = getParameter ("1234");
if (port == null)
port = "12938";
output.appendText ("Connecting to " + host + ":" + port + "...");
Socket s = new Socket (host, Integer.parseInt (port));
i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));
output.appendText (" connected.\
");
input.setEditable (true);
input.requestFocus ();
execute ();
} catch (IOException ex) {
ByteArrayOutputStream out = new ByteArrayOutputStream ();
ex.printStackTrace (new PrintStream (out));
output.appendText ("\
" + out);
}
}
public void execute () {
try {
while (true) {
String line = i.readUTF ();
output.appendText (line + "\
");
}
} catch (IOException ex) {
ByteArrayOutputStream out = new ByteArrayOutputStream ();
ex.printStackTrace (new PrintStream (out));
output.appendText (out.toString ());
} finally {
listener = null;
input.hide ();
validate ();
try {
o.close ();
} catch (IOException ex) {
ex.printStackTrace ();
}
}
}
public boolean handleEvent (Event e) {
if ((e.target == input) && (e.id == Event.ACTION_EVENT)) {
try {
o.writeUTF ((String) e.arg);
o.flush ();
} catch (IOException ex) {
ex.printStackTrace();
listener.stop ();
}
input.setText ("");
return true;
} else if ((e.target == this) && (e.id == Event.WINDOW_DESTROY)) {
if (listener != null)
listener.stop ();
hide ();
return true;
}
return super.handleEvent (e);
}
}
Even if I remove the getParameter method, and leave it as a string, it gives me the same error that it could not connect. For example to “localhost”, and port “1234”.
So what are you asking exactly? How to properly set the host and port numbers?
If so, I don’t know. The majic occurs behind that getParameter() method.
You might need to set a command line argument or point to a properties file or something. You’ll just have to see what that method is doing to figure everything out.
Yes, that’s why I’m posting a question on which host, and portname I should use. The error message, or the string of code has nothing to do with it. I just posted it as an example for what’s showing after using some port and localhost.
I didn’t notice that you were using an Applet. There are a lot of security issues associated with Applets, like not being able to connect to any server except the server that the Applet is being hosted from. Can someone else confirm that statement?
Also, how are you hosting your applet? Are you using tomcat or some other web server? Or are you just opening an html page directly by double clicking on it?