import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class SocketTalk extends Frame implements ActionListener,WindowListener {
TextField field,id,message;
Button button,bid,btmsg,close;
byte[] c=new byte[1000];
Socket socket;
private final int MAXUSER=4;
private Daemon daemons[];
public ServerSocket svsocket;
public Vector clients;
TextField port;
Button btstart;
TextArea tamsg;
Panel panel1,panel2;
public static void main(String args[]) {
SocketTalk test=new SocketTalk();
test.addWindowListener(test);
test.setSize(800,400);
test.setVisible(true);
}
public SocketTalk(){
setTitle("聊天室简易版");
panel1=new Panel();
panel1.setLayout(new FlowLayout());
TextField tf=new TextField("输入连接端口(如:8888),开启Socket");
tf.setBackground(Color.yellow);
tf.setEditable(false);
panel1.add(tf);
port=new TextField("3388");
panel1.add(port);
btstart=new Button("start");
btstart.addActionListener(this);
panel1.add(btstart);
add(panel1,BorderLayout.NORTH);
panel2=new Panel();
panel2.setLayout(new FlowLayout());
panel2.add(new Label("address: "));
field=new TextField(20);
panel2.add(field);
button=new Button("link");
panel2.add(button);
button.addActionListener(this);
id=new TextField(10);
panel2.add(id);
bid=new Button("id_ok");
panel2.add(bid);
bid.addActionListener(this);
bid.enable(false);
message=new TextField(20);
panel2.add(message);
btmsg=new Button("send");
btmsg.enable(false);
panel2.add(btmsg);
btmsg.addActionListener(this);
close=new Button("close");
panel2.add(close);
close.addActionListener(this);
add(panel2,BorderLayout.SOUTH);
tamsg=new TextArea();
tamsg.setBackground(Color.PINK);
tamsg.append("输入你要链接的地址,然后按(link)按钮\n");
add(tamsg,BorderLayout.CENTER);
setBackground(Color.GREEN);
}
public void actionPerformed(ActionEvent e) {
int i;
String strmsg;
String label=((Button)e.getSource()).getLabel();
if(label.compareTo("start")==0) {
try {
int po=Integer.parseInt(port.getText());
svsocket=new ServerSocket(po);
daemons=new Daemon[MAXUSER];
clients=new Vector();
for (int j=0;j<MAXUSER;j++) {
daemons[j]=new Daemon(this);
daemons[j].start();
}
tamsg.append("Listening to port 3388......\n");
}
catch (IOException ioe) {
tamsg.append("IOException:"+ioe.getMessage());
}
}
else if(label.compareTo("link")==0)
{
try {
socket=new Socket(field.getText(),3388);
button.enable(false);
bid.enable(true);
DataInputStream is=new DataInputStream(socket.getInputStream());
i=is.read(c);
tamsg.append(new String(c,0,i));
}
catch (Exception exc) {
tamsg.append("error happended link\n");
tamsg.append(exc.toString());
}
}
else if(label.compareTo("id_ok")==0)
{
try {
DataInputStream is=new DataInputStream(socket.getInputStream());
bid.enable(false);
btmsg.enable(true);
strmsg=id.getText()+"\n";
DataOutputStream os=new DataOutputStream(socket.getOutputStream());
os.write(strmsg.getBytes());
tamsg.append(strmsg+" 进入聊天!\n");
i=is.read(c);
tamsg.append(new String(c,0,i));
}
catch (Exception exc) {
tamsg.append("error happended id\n");
tamsg.append(exc.toString());
}
}
else if(label.compareTo("send")==0) {
try {
strmsg=message.getText()+"\n";
DataOutputStream os=new DataOutputStream(socket.getOutputStream());
os.write(strmsg.getBytes());
tamsg.append(id.getText()+"说: "+strmsg);
message.setText("");
}
catch (Exception exc) {
tamsg.append("error happended id\n");
tamsg.append(exc.toString());
}
}
else if(label.compareTo("close")==0) {
try {
DataInputStream is=new DataInputStream(socket.getInputStream());
button.enable(true);
btmsg.enable(false);
tamsg.append("closing socket!\n");
i=is.read(c);
tamsg.append(new String(c,0,i));
DataOutputStream os=new DataOutputStream(socket.getOutputStream());
os.writeBytes("\n");
tamsg.append("close socket\n");
socket.close();
}
catch (Exception exc) {
tamsg.append("error happended close\n");
tamsg.append(exc.toString());
}
}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}
class Daemon extends Thread {
private SocketTalk server;
private DataInputStream dis;
private DataOutputStream dos;
public Daemon(SocketTalk s) {
server=s;
}
public void run() {
String name="";
String message="";
try {
do {
Socket sck=server.svsocket.accept();
server.tamsg.append("Accept connection from "+sck.getInetAddress().getHostAddress());
dis=new DataInputStream(sck.getInputStream());
dos=new DataOutputStream(sck.getOutputStream());
postMessage("请输入您的姓名(ID): ");
name=getInput();
server.clients.addElement(name);
postMessage("欢迎 "+name+" 来到!\r\n");
server.tamsg.append(name+" 进入.\n");
do{
message=getInput();
if(message.compareTo("exit")==0)
break;
server.tamsg.append(name+"说: "+message+"\n");
}while(true);
postMessage(name+"请按(close)离开\r\n");
getInput();
server.clients.removeElement(name);
server.tamsg.append(name+" 离开");
dis.close();
dos.close();
sck.close();
} while(true);
}
catch (IOException ioe) {
server.tamsg.append("IOException: "+ioe.getMessage());
}
}
public String getInput() throws IOException {
byte c;
byte input[]=new byte[255];
int index=0;
do {
c=dis.readByte();
if(c==8||c==127) {
if(index>0) {
postMessage("\b\b");
index--;
}
}
else if (index<255) {
dos.writeByte(c);
input[index++]=c;
}
} while (c!=10);
return (new String(input,0,index-1));
}
public void postMessage(String msg) {
try {
dos.write(msg.getBytes());
}
catch (IOException ioe) {
server.tamsg.append("IOException: "+ioe.getMessage());
}
}
}
|