Kan ikke lave socket connect via applet
HejsaJeg har fået lavet mig en Chat som køre fint, hvis jeg logger på med en telnet client. Men nu ville jeg jo gerne havde den til at virker også med at køre som en applet.
Men det er lige som om at den ikke vil connect og bare springer min main function over. Kan nogle hjælpe mig??
import java.io.*;
import java.net.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
public class PrintThread extends Applet {
TextArea display = new java.awt.TextArea(7, 90);
TextField textField = new TextField("", 90);
int paintCount = 0;
public void init() {
display.setEditable(false);
textField.setEditable(true);
GridBagLayout gridBag = new GridBagLayout();
setLayout(gridBag);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(display, c);
add(display);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 0.0;
gridBag.setConstraints(textField, c);
add(textField);
validate();
addItem("Velkommen... ");
}
public boolean action(Event evt, Object arg) {
String text;
text = textField.getText();
display.appendText(text + " \n");
textField.setText("");
// textField.selectAll();
// Connect();
return true;
}
void addItem(String newWord) {
System.out.println(newWord);
display.appendText(newWord + "\n");
display.repaint();
if (++paintCount % 4 == 0) {
repaint();
}
}
public void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("*.*.*.*", 1997);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
// System.err.println("Don't know about host: *.*.*.*.");
addItem("Don't know about host: *.*.*.*.");
System.exit(1);
} catch (IOException e) {
// System.err.println("Couldn't get I/O for the connection to: *.*.*.*.");
addItem("Couldn't get I/O for the connection to: *.*.*.*.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));
String userInput;
out.println("ibrene");
out.println("password");
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
// System.out.println("echo: " + in.readLine());
}
addItem("Fake Don't know about host: *.*.*.*.");
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}