贴给你吧,不过好像有错误!
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
public class QQ extends Frame implements ActionListener
{
Label l1=new Label("请输入您要发送的信息(限英文):");
Label l2=new Label("以下是你收到的消息记录:");
Label l3=new Label("把以上消息发给如下IP地址:");
TextArea input=new TextArea("",7,14,TextArea.SCROLLBARS_BOTH);
TextArea output=new TextArea("",8,14,TextArea.SCROLLBARS_BOTH);
TextField IPAdd=new TextField("127.0.0.1");
Button send=new Button("发送消息");
Button about=new Button("关于");
Button clear=new Button("清空消息纪录");
GregorianCalendar time=new GregorianCalendar();
QQ()
{
super("仿QQ聊天工具");
setLayout(null);
setLocation(250, 250);
this.setSize(518, 218);
this.setResizable(false);
this.setBackground(new Color(220, 220, 220));
Toolkit kit=Toolkit.getDefaultToolkit();
Image myImage=kit.getImage("QQ.bmp");
this.setIconImage(myImage); l1.setFont(new Font("宋体",Font.PLAIN,12));
l1.setForeground(new Color(0, 0, 192));
l1.setBounds(8,28,216, 16); input.setBackground(new Color(255, 255, 128));
input.setFont(new Font("Times New Roman",Font.BOLD,15));
input.setForeground(Color.magenta);
input.setBounds(8,44,248, 120); output.setBackground(new Color(128, 255, 255));
output.setFont(new Font("Times New Roman",Font.PLAIN,12));
output.setForeground(new Color(192, 64, 0));
output.setBounds(264, 44,248, 136);
output.setEditable(false); send.setFont(new Font("新宋体",Font.PLAIN,12));
send.setLocation(136, 188);
send.setSize(120, 22); clear.setFont(new Font("新宋体",Font.PLAIN,12));
clear.setLocation(392, 188);
clear.setSize(120, 22); l2.setFont(new Font("宋体",Font.PLAIN, 12));
l2.setForeground(new Color(0, 0, 192));
l2.setBounds(264,28,216, 16); about.setFont(new Font("新宋体",Font.PLAIN,12));
about.setLocation(264, 188);
about.setSize(120, 22); l3.setFont(new Font("宋体",Font.PLAIN,12));
l3.setForeground(new Color(0, 0, 192));
l3.setBounds(8,172,160,16); IPAdd.setFont(new Font("新宋体",Font.PLAIN,12));
IPAdd.setLocation(8, 190);
IPAdd.setSize(120, 19); add(l1);add(input);add(l3);add(l2);add(output);
add(IPAdd);add(send);add(about);add(clear);
addWindowListener(new closeWin());
send.addActionListener(this);
about.addActionListener(this);
clear.addActionListener(this); show();
waitForData();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==send)
sendData();
else
if(e.getSource()==clear)
output.setText("");
else
if(e.getSource()==about)
{
AboutQQ test=new AboutQQ();
test.showDialog();
}
}
public static void main(String args[])
{
new QQ();
}
void sendData()
{
try{
String msg=input.getText();
if(msg.equals(""))
return;
input.setText("");
String ad=IPAdd.getText();
InetAddress tea=InetAddress.getLocalHost();
String asd=tea.getHostAddress();//发送方的IP地址
output.append("["+asd+"]:("+time.get(GregorianCalendar.YEAR)+"-"+time.get(GregorianCalendar.MONTH)+"-"+time.get(GregorianCalendar.DATE)+" "+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND)+") "+"\n"+msg+"\n");
msg="From ["+asd+"]:("+time.get(GregorianCalendar.YEAR)+"-"+time.get(GregorianCalendar.MONTH)+"-"+time.get(GregorianCalendar.DATE)+" "+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND)+") \n"+msg;
InetAddress address=InetAddress.getByName(ad);
int len=msg.length();
byte[] message=new byte[len];
msg.getBytes(0,len,message,0);
DatagramPacket packet=new DatagramPacket(message,len,address,9999);
DatagramSocket socket=new DatagramSocket();
socket.send(packet);
}
catch(Exception e){}
}
void waitForData()
{
try{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
DatagramSocket socket=new DatagramSocket(9999);
while(true)
{
socket.receive(packet);
String s=new String(buffer,0,0,packet.getLength());
output.append(s+"\n");
packet=new DatagramPacket(buffer,buffer.length);
}
}
catch(Exception e){}
}
}
class closeWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Frame fr=(Frame)(e.getSource());
fr.dispose();
System.exit(0);
}
}
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
public class QQ extends Frame implements ActionListener
{
Label l1=new Label("请输入您要发送的信息(限英文):");
Label l2=new Label("以下是你收到的消息记录:");
Label l3=new Label("把以上消息发给如下IP地址:");
TextArea input=new TextArea("",7,14,TextArea.SCROLLBARS_BOTH);
TextArea output=new TextArea("",8,14,TextArea.SCROLLBARS_BOTH);
TextField IPAdd=new TextField("127.0.0.1");
Button send=new Button("发送消息");
Button about=new Button("关于");
Button clear=new Button("清空消息纪录");
GregorianCalendar time=new GregorianCalendar();
QQ()
{
super("仿QQ聊天工具");
setLayout(null);
setLocation(250, 250);
this.setSize(518, 218);
this.setResizable(false);
this.setBackground(new Color(220, 220, 220));
Toolkit kit=Toolkit.getDefaultToolkit();
Image myImage=kit.getImage("QQ.bmp");
this.setIconImage(myImage); l1.setFont(new Font("宋体",Font.PLAIN,12));
l1.setForeground(new Color(0, 0, 192));
l1.setBounds(8,28,216, 16); input.setBackground(new Color(255, 255, 128));
input.setFont(new Font("Times New Roman",Font.BOLD,15));
input.setForeground(Color.magenta);
input.setBounds(8,44,248, 120); output.setBackground(new Color(128, 255, 255));
output.setFont(new Font("Times New Roman",Font.PLAIN,12));
output.setForeground(new Color(192, 64, 0));
output.setBounds(264, 44,248, 136);
output.setEditable(false); send.setFont(new Font("新宋体",Font.PLAIN,12));
send.setLocation(136, 188);
send.setSize(120, 22); clear.setFont(new Font("新宋体",Font.PLAIN,12));
clear.setLocation(392, 188);
clear.setSize(120, 22); l2.setFont(new Font("宋体",Font.PLAIN, 12));
l2.setForeground(new Color(0, 0, 192));
l2.setBounds(264,28,216, 16); about.setFont(new Font("新宋体",Font.PLAIN,12));
about.setLocation(264, 188);
about.setSize(120, 22); l3.setFont(new Font("宋体",Font.PLAIN,12));
l3.setForeground(new Color(0, 0, 192));
l3.setBounds(8,172,160,16); IPAdd.setFont(new Font("新宋体",Font.PLAIN,12));
IPAdd.setLocation(8, 190);
IPAdd.setSize(120, 19); add(l1);add(input);add(l3);add(l2);add(output);
add(IPAdd);add(send);add(about);add(clear);
addWindowListener(new closeWin());
send.addActionListener(this);
about.addActionListener(this);
clear.addActionListener(this); show();
waitForData();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==send)
sendData();
else
if(e.getSource()==clear)
output.setText("");
else
if(e.getSource()==about)
{
AboutQQ test=new AboutQQ();
test.showDialog();
}
}
public static void main(String args[])
{
new QQ();
}
void sendData()
{
try{
String msg=input.getText();
if(msg.equals(""))
return;
input.setText("");
String ad=IPAdd.getText();
InetAddress tea=InetAddress.getLocalHost();
String asd=tea.getHostAddress();//发送方的IP地址
output.append("["+asd+"]:("+time.get(GregorianCalendar.YEAR)+"-"+time.get(GregorianCalendar.MONTH)+"-"+time.get(GregorianCalendar.DATE)+" "+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND)+") "+"\n"+msg+"\n");
msg="From ["+asd+"]:("+time.get(GregorianCalendar.YEAR)+"-"+time.get(GregorianCalendar.MONTH)+"-"+time.get(GregorianCalendar.DATE)+" "+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND)+") \n"+msg;
InetAddress address=InetAddress.getByName(ad);
int len=msg.length();
byte[] message=new byte[len];
msg.getBytes(0,len,message,0);
DatagramPacket packet=new DatagramPacket(message,len,address,9999);
DatagramSocket socket=new DatagramSocket();
socket.send(packet);
}
catch(Exception e){}
}
void waitForData()
{
try{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
DatagramSocket socket=new DatagramSocket(9999);
while(true)
{
socket.receive(packet);
String s=new String(buffer,0,0,packet.getLength());
output.append(s+"\n");
packet=new DatagramPacket(buffer,buffer.length);
}
}
catch(Exception e){}
}
}
class closeWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Frame fr=(Frame)(e.getSource());
fr.dispose();
System.exit(0);
}
}
文档注释还比较详细,自己看看吧
** Constants and Class Methods for Java Chat Clients and Server.
*
* The protocol:
* --> Lusername
* --> Btext_to_broadcast
* --> Musername\Message
* --> Q
* <-- any text to be displayed.
*
* @author Ian Darwin
* @version $Id: Chat.java,v 1.2 2000/10/10 22:35:41 ian Exp $
*/
public class Chat { // These are the first character of messages from client to server public static final int PORTNUM = 9999;
public static final int MAX_LOGIN_LENGTH = 20;
public static final char SEPARATOR = '\\';
public static final char COMMAND = '\\';
public static final char CMD_LOGIN = 'L';
public static final char CMD_QUIT = 'Q';
public static final char CMD_MESG = 'M';
public static final char CMD_BCAST = 'B'; // These are the first character of messages from server to client public static final char RESP_PUBLIC = 'P';
public static final char RESP_PRIVATE = 'M';
public static final char RESP_SYSTEM = 'S'; // TODO in main loop:
// if (text.charAt(0) == '/')
// send(text);
// else send("B"+text); public static boolean isValidLoginName(String login) {
// check length
if (login.length() > MAX_LOGIN_LENGTH)
return false; // check for bad chars
// if (contains bad chars)
// return false // Passed above tests, is OK
return true;
}
}
ChatRoom.java//
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;/** Simple Chat Room Applet.
* Writing a Chat Room seems to be one of many obligatory rites (or wrongs)
* of passage for Java experts these days.
* <P>
* This one is a toy because it doesn't much of a command protocol, which
* means we can't query the server as to * who's logged in,
* or anything fancy like that. However, it works OK for small groups.
* <P>
* Uses client socket w/ two Threads (main and one constructed),
* one for reading and one for writing.
* <P>
* Server multiplexes messages back to all clients.
* <P>
* TODO in V2: use Java's MultiCastSocket, if it works OK on '95.
*
* @author Ian Darwin, [email protected]
* @version $Id: ChatRoom.java,v 1.5 2001/12/26 01:04:09 ian Exp $
*/
public class ChatRoom extends Applet {
/** Whether we are being run as an Applet or an Application */
protected boolean inAnApplet = true;
/** The state of logged-in-ness */
protected boolean loggedIn;
/* The Frame, for a pop-up, durable Chat Room. */
protected Frame cp;
/** The default port number */
protected static int PORTNUM = Chat.PORTNUM;
/** The actual port number */
protected int port;
/** The network socket */
protected Socket sock;
/** BufferedReader for reading from socket */
protected BufferedReader is;
/** PrintWriter for sending lines on socket */
protected PrintWriter pw;
/** TextField for input */
protected TextField tf;
/** TextArea to display conversations */
protected TextArea ta;
/** The Login button */
protected Button lib;
/** The LogOUT button */
protected Button lob;
/** The TitleBar title */
final static String TITLE = "Chat: Ian Darwin's Toy Chat Room Client";
/** The message that we paint */
protected String paintMessage; /** Init, inherited from Applet */
public void init() {
paintMessage = "Creating Window for Chat";
repaint();
cp = new Frame(TITLE);
cp.setLayout(new BorderLayout());
String portNum = null;
if (inAnApplet)
portNum = getParameter("port");
port = PORTNUM;
if (portNum != null)
port = Integer.parseInt(portNum); // The GUI
ta = new TextArea(14, 80);
ta.setEditable(false); // readonly
ta.setFont(new Font("Monospaced", Font.PLAIN, 11));
cp.add(BorderLayout.NORTH, ta); Panel p = new Panel();
Button b; // The login button
p.add(lib = new Button("Login"));
lib.setEnabled(true);
lib.requestFocus();
lib.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
login();
lib.setEnabled(false);
lob.setEnabled(true);
tf.requestFocus(); // set keyboard focus in right place!
}
}); // The logout button
p.add(lob = new Button("Logout"));
lob.setEnabled(false);
lob.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logout();
lib.setEnabled(true);
lob.setEnabled(false);
lib.requestFocus();
}
}); p.add(new Label("Message here:"));
tf = new TextField(40);
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (loggedIn) {
pw.println(Chat.CMD_BCAST+tf.getText());
tf.setText("");
}
}
});
p.add(tf); cp.add(BorderLayout.SOUTH, p); cp.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// If we do setVisible and dispose, then the Close completes
ChatRoom.this.cp.setVisible(false);
ChatRoom.this.cp.dispose();
logout();
}
});
cp.pack();
// After packing the Frame, centre it on the screen.
Dimension us = cp.getSize(),
them = Toolkit.getDefaultToolkit().getScreenSize();
int newX = (them.width - us.width) / 2;
int newY = (them.height- us.height)/ 2;
cp.setLocation(newX, newY);
cp.setVisible(true);
paintMessage = "Window should now be visible";
repaint();
} protected String serverHost = "localhost"; /** LOG ME IN TO THE CHAT */
public void login() {
showStatus("In login!");
if (loggedIn)
return;
if (inAnApplet)
serverHost = getCodeBase().getHost();
try {
sock = new Socket(serverHost, port);
is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
pw = new PrintWriter(sock.getOutputStream(), true);
} catch(IOException e) {
showStatus("Can't get socket to " +
serverHost + "/" + port + ": " + e);
cp.add(new Label("Can't get socket: " + e));
return;
}
showStatus("Got socket"); // Construct and start the reader: from server to textarea.
// Make a Thread to avoid lockups.
new Thread(new Runnable() {
public void run() {
String line;
try {
while (loggedIn && ((line = is.readLine()) != null))
ta.append(line + "\n");
} catch(IOException e) {
showStatus("GAA! LOST THE LINK!!");
return;
}
}
}).start(); // FAKE LOGIN FOR NOW
pw.println(Chat.CMD_LOGIN + "AppletUser");
loggedIn = true;
} /** Log me out, Scotty, there's no intelligent life here! */
public void logout() {
if (!loggedIn)
return;
loggedIn = false;
try {
if (sock != null)
sock.close();
} catch (IOException ign) {
// so what?
}
} // It is deliberate that there is no STOP method - we want to keep
// going even if the user moves the browser to another page.
// Anti-social? Maybe, but you can use the CLOSE button to kill
// the Frame, or you can exit the Browser. /** Paint paints the small window that appears in the HTML,
* telling the user to look elsewhere!
*/
public void paint(Graphics g) {
Dimension d = getSize();
int h = d.height;
int w = d.width;
g.fillRect(0, 0, w, 0);
g.setColor(Color.black);
g.drawString(paintMessage, 10, (h/2)-5);
}
/** a showStatus that works for Applets or non-Applets alike */
public void showStatus(String mesg) {
if (inAnApplet)
super.showStatus(mesg);
System.out.println(mesg);
} /** A main method to allow the client to be run as an Application */
public static void main(String[] args) {
ChatRoom room101 = new ChatRoom();
room101.inAnApplet = false;
room101.init();
room101.start();
}
}
import java.io.*;
import java.net.*;
import java.util.*;/** Trivial Chat Server to go with our Trivial Chat Client.
*
* Does not implement any form of "anonymous nicknames" - this is
* a good thing, given how a few people have abused anonymous
* chat rooms in the past.
*
* WARNING -- this code is believed thread-safe but has NOT been 100% vetted
* by a team of world-class experts for Thread-safeness.
* DO NOT BUILD ANYTHING CRITICAL BASED ON THIS until you have done so.
* See the various books on Threaded Java for design issues.
* YOU HAVE BEEN WARNED!!
*
* @author Ian F. Darwin, [email protected]
* @version $Id: ChatServer.java,v 1.6 2001/12/26 01:04:09 ian Exp $
*/
public class ChatServer {
/** What I call myself in system messages */
protected final static String CHATMASTER_ID = "ChatMaster";
/** What goes between any handle and the message */
protected final static String SEP = ": ";
/** The Server Socket */
protected ServerSocket servSock;
/** The list of my current clients */
protected ArrayList clients;
/** Debugging state */
private static boolean DEBUG = false; /** Main just constructs a ChatServer, which should never return */
public static void main(String[] argv) {
System.out.println("DarwinSys Chat Server 0.1 starting...");
if (argv.length == 1 && argv[0].equals("-debug"))
DEBUG = true;
ChatServer w = new ChatServer();
w.runServer(); // should never return.
System.out.println("**ERROR* Chat Server 0.1 quitting");
} /** Construct (and run!) a Chat Service */
ChatServer() {
clients = new ArrayList();
try {
servSock = new ServerSocket(Chat.PORTNUM);
System.out.println("DarwinSys Chat Server Listening on port " +
Chat.PORTNUM);
} catch(IOException e) {
log("IO Exception in ChatServer.<init>");
System.exit(0);
}
} public void runServer() {
try {
while (true) {
Socket us = servSock.accept();
String hostName = us.getInetAddress().getHostName();
System.out.println("Accepted from " + hostName);
ChatHandler cl = new ChatHandler(us, hostName);
synchronized (clients) {
clients.add(cl);
cl.start();
if (clients.size() == 1)
cl.send(CHATMASTER_ID, "Welcome! you're the first one here");
else {
cl.send(CHATMASTER_ID, "Welcome! you're the latest of " +
clients.size() + " users.");
}
}
}
} catch(IOException e) {
log("IO Exception in runServer: " + e);
System.exit(0);
}
} protected void log(String s) {
System.out.println(s);
} /** Inner class to handle one conversation */
protected class ChatHandler extends Thread {
/** The client socket */
protected Socket clientSock;
/** BufferedReader for reading from socket */
protected BufferedReader is;
/** PrintWriter for sending lines on socket */
protected PrintWriter pw;
/** The client's host */
protected String clientIP;
/** String handle */
protected String login; /* Construct a Chat Handler */
public ChatHandler(Socket sock, String clnt) throws IOException {
clientSock = sock;
clientIP = clnt;
is = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
pw = new PrintWriter(sock.getOutputStream(), true);
} /** Each ChatHandler is a Thread, so here's the run() method,
* which handles this conversation.
*/
public void run() {
String line;
try {
while ((line = is.readLine()) != null) {
char c = line.charAt(0);
line = line.substring(1);
switch (c) {
case Chat.CMD_LOGIN:
if (!Chat.isValidLoginName(line)) {
send(CHATMASTER_ID, "LOGIN " + line + " invalid");
log("LOGIN INVALID from " + clientIP);
continue;
}
login = line;
broadcast(CHATMASTER_ID, login +
" joins us, for a total of " +
clients.size() + " users");
break;
case Chat.CMD_MESG:
if (login == null) {
send(CHATMASTER_ID, "please login first");
continue;
}
int where = line.indexOf(Chat.SEPARATOR);
String recip = line.substring(0, where);
String mesg = line.substring(where+1);
log("MESG: " + login + "-->" + recip + ": "+ mesg);
ChatHandler cl = lookup(recip);
if (cl == null)
psend(CHATMASTER_ID, recip + " not logged in.");
else
cl.psend(login, mesg);
break;
case Chat.CMD_QUIT:
broadcast(CHATMASTER_ID, "Goodbye to " + login + "@" + clientIP);
close();
return; // END OF THIS CHATHANDLER
case Chat.CMD_BCAST:
if (login != null)
broadcast(login, line);
else
log("B<L FROM " + clientIP);
break;
default:
log("Unknown cmd " + c + " from " + login + "@" + clientIP);
}
}
} catch (IOException e) {
log("IO Exception: " + e);
} finally {
// the sock ended, so we're done, bye now
// Can NOT send a good-bye message, until we have
// a simple command-based protocol in place.
System.out.println(login + SEP + "All Done");
synchronized(clients) {
clients.remove(this);
if (clients.size() == 0) {
System.out.println(CHATMASTER_ID + SEP +
"Im so lonely I could cry...");
} else if (clients.size() == 1) {
ChatHandler last = (ChatHandler)clients.get(0);
last.send(CHATMASTER_ID,
"Hey, you're talking to yourself again");
} else {
broadcast(CHATMASTER_ID,
"There are now " + clients.size() + " users");
}
}
}
} protected void close() {
if (clientSock == null) {
log("close when not open");
return;
}
try {
clientSock.close();
clientSock = null;
} catch (IOException e) {
log("Failure during close to " + clientIP);
}
} /** Send one message to this user */
public void send(String sender, String mesg) {
pw.println(sender + SEP + mesg);
} /** Send a private message */
protected void psend(String sender, String msg) {
send("<*" + sender + "*>", msg);
}
/** Send one message to all users */
public void broadcast(String sender, String mesg) {
System.out.println("Broadcasting " + sender + SEP + mesg);
for (int i=0; i<clients.size(); i++) {
ChatHandler sib = (ChatHandler)clients.get(i);
if (DEBUG)
System.out.println("Sending to " + sib);
sib.send(sender, mesg);
}
if (DEBUG) System.out.println("Done broadcast");
} protected ChatHandler lookup(String nick) {
synchronized(clients) {
for (int i=0; i<clients.size(); i++) {
ChatHandler cl = (ChatHandler)clients.get(i);
if (cl.login.equals(nick))
return cl;
}
}
return null;
} /** Present this ChatHandler as a String */
public String toString() {
return "ChatHandler[" + login + "]";
}
}
}
import java.io.*;
import java.net.*;/** Simple console-mode (command-line) chat client.
* @author Ian Darwin, [email protected]
* @version $Id: ConsChat.java,v 1.4 2001/12/26 01:04:09 ian Exp $
*/
public class ConsChat {
public static void main(String[] args) throws IOException {
new ConsChat().chat();
} protected Socket sock;
protected BufferedReader is;
protected PrintWriter pw;
protected BufferedReader cons; protected ConsChat() throws IOException {
sock = new Socket("localhost", Chat.PORTNUM);
is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
pw = new PrintWriter(sock.getOutputStream(), true);
cons = new BufferedReader(new InputStreamReader(System.in)); // Construct and start the reader: from server to stdout.
// Make a Thread to avoid lockups.
new Thread() {
public void run() {
setName("socket reader thread");
System.out.println("Starting " + getName());
System.out.flush();
String line;
try {
// reader thread blocks here
while ((line = is.readLine()) != null) {
System.out.println(line);
System.out.flush();
}
} catch (IOException ex) {
System.err.println("Read error on socket: " + ex);
return;
}
}
}.start();
} protected void chat() throws IOException {
String text; System.out.print("Login name: "); System.out.flush();
text = cons.readLine();
send(Chat.CMD_LOGIN + text); // Main thread blocks here
while ((text = cons.readLine()) != null) {
if (text.length() == 0 || text.charAt(0) == '#')
continue; // ignore null lines and comments
if (text.charAt(0) == '/')
send(text.substring(1));
else send("B"+text);
}
} protected void send(String s) {
pw.println(s);
pw.flush();
}}