import java.io.*;
import java.applet.Applet;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class ClientApplet extends JApplet implements Runnable {
public static JTextField chat;
public static JTextArea chattxt;
static PrintStream os = null;
static Socket clientSocket = null;
public void init() {
chat = new JTextField(40);
chattxt = new JTextArea(20,50);
chat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println(chat.getText());
}
});
add(chat);
add(chattxt);
chattxt.append("Hey!");
}
public void start() {
new Thread(this).start();
}
public void run() {
// communicate with server here
Socket s = null;
try {
//s = new Socket(getParameter("host"), Integer.valueOf(getParameter("port")));
s = new Socket("localhost", 4444);
InputStream in = s.getInputStream();
os = new PrintStream(clientSocket.getOutputStream());
int buf = -1;
while ((buf = in.read()) != '.') {
chattxt.append("\n" + buf);
}
chattxt.append("\nIn read: " + in.read() + "\nServer sent: " + in);
}catch(Exception e) {
e.printStackTrace();
}
finally {
try {
s.close();
} catch(IOException e)
{ }
}
}
}
|