A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冷月 高级黑马   /  2013-8-30 08:59  /  1935 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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());
  }
}
}

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

4 个回复

倒序浏览
抢个楼先   lz辛苦了
自己写的  还是在哪复制的????
回复 使用道具 举报
楼主给个运行示例的截图呗。。
回复 使用道具 举报
代码拿出去运行不就看到了吗
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马