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;
public static PrintStream os = null;
static Socket clientSocket = null;
static BufferedReader is = null;
public void init() {
System.out.println("****** Welcome to the game ******");
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() {
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(s.getOutputStream());
int buf = -1;
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Client > " + responseLine);
}
while ((buf = in.read()) != -1) {
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)
{ }
}
}
}
|