刚学了一个月的JAVASE..老师让做个带swing界面的socket多线程小项目.找了些代码...但还是不知道从哪里下手改动.....很多知识点不是很清晰...希望各位大神..有简单的实现的代码 贴给我...让我参考参考...现在貌似通信还好说...我好象卡在界面那里了...界面的设置和事件处理的很不好...明天就要交题啦...苦恼...拜托了!!!

解决方案 »

  1.   

    我的资源里有一个用SOCKET实现的广播式的聊天程序,是用SWING做的界面,,希望对你有用。
      

  2.   

    我只做过控制台直接通信的 没有界面的 如果你要界面的话 我这里有一个聊天室界面的 
    但是是已经打包的 那么里面就只有class文件 如果你想看源码的话 你自己必须用工具将class文件反编译出来 如果你要的话 加我QQ 我发给你 396801753 
      

  3.   

    swing的话,CoreJAVA第一卷 中讲的很详细,有很多例子
      

  4.   

    网上搜,几乎都有socket多线程小项目(java聊天程序)
      

  5.   

    的确!CSDN下载频道也有很多,可以下载的,你搜下
      

  6.   

    给你个简单例子,希望帮到你服务器类:
    import java.io.*;
    import java.net.*;
    import java.util.*;public class ChatServer {
    boolean started = false;
    ServerSocket ss = null;

    List clients = new ArrayList();

    public static void main(String[] args) {
    new ChatServer().start();
    }

    public void start() {
    try {
    ss = new ServerSocket(8888);
    started = true;
    } catch (BindException e) {
    System.out.println("端口使用中....");
    System.out.println("请关掉相关程序并重新运行服务器!");
    System.exit(0);
    } catch (IOException e) {
    e.printStackTrace();
    }

    try {

    while(started) {
    Socket s = ss.accept();
    Client c = new Client(s);
    System.out.println("a client connected!");
    new Thread(c).start();
    clients.add(c);

    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    ss.close();
    } catch (IOException e) {

    e.printStackTrace();
    }
    }
    }

    class Client implements Runnable {
    private Socket s;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    private boolean bConnected = false;

    public Client(Socket s) {
    this.s = s;
    try {
    dis = new DataInputStream(s.getInputStream());
    dos = new DataOutputStream(s.getOutputStream());
    bConnected = true;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void send(String str) {
    try {
    dos.writeUTF(str);
    } catch (IOException e) {
    clients.remove(this);
    System.out.println("对方退出了!我从List里面去掉了!");

    }
    }

    public void run() {
    try {
    while(bConnected) {
    String str = dis.readUTF();
    System.out.println(str);
    for(int i=0; i<clients.size(); i++) {
    Client c = (Client) clients.get(i);
    c.send(str); }


    }
    } catch (EOFException e) {
    System.out.println("Client closed!");
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if(dis != null) dis.close();
    if(dos != null) dos.close();
    if(s != null)  {
    s.close();

    }

    } catch (IOException e1) {
    e1.printStackTrace();
    }


    }
    }

    }
    }
    用户类:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;public class ChatClient extends Frame {
    Socket s = null;
    DataOutputStream dos = null;
    DataInputStream dis = null;
    private boolean bConnected = false; TextField tfTxt = new TextField(); TextArea taContent = new TextArea();

    Thread tRecv = new Thread(new RecvThread());  public static void main(String[] args) {
    new ChatClient().launchFrame(); 
    } public void launchFrame() {
    setLocation(400, 300);
    this.setSize(300, 300);
    add(tfTxt, BorderLayout.SOUTH);
    add(taContent, BorderLayout.NORTH);
    pack();
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent arg0) {
    disconnect();
    System.exit(0);
    }

    });
    tfTxt.addActionListener(new TFListener());
    setVisible(true);
    connect();

    tRecv.start();
    }

    public void connect() {
    try {
    s = new Socket("127.0.0.1", 8888);
    dos = new DataOutputStream(s.getOutputStream());
    dis = new DataInputStream(s.getInputStream());
    System.out.println("connected!");
    bConnected = true;
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }

    public void disconnect() {
    try {
    dos.close();
    dis.close();
    s.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private class TFListener implements ActionListener { public void actionPerformed(ActionEvent e) {
    String str = tfTxt.getText().trim();

    tfTxt.setText("");

    try { dos.writeUTF(str);
    dos.flush();

    } catch (IOException e1) {
    e1.printStackTrace();
    }

    }

    }

    private class RecvThread implements Runnable { public void run() {
    try {
    while(bConnected) {
    String str = dis.readUTF();

    taContent.setText(taContent.getText() + str + '\n');
    }
    } catch (SocketException e) {
    System.out.println("退出了,bye!");
    } catch (EOFException e) {
    System.out.println("退出了,bye - bye!");
    } catch (IOException e) {
    e.printStackTrace();


    }

    }
    }
    先起服务器,再起用户,用户间可以聊天
      

  7.   

    谢谢 各位大神的回复...但好象对我的帮助不是很大..这样吧..我把我的代码写出来..大家帮我分析一下...我该需要加些什么东西好..总体来说我的client和9楼的类似//JavaSE 知识总结 Socket+Swing+awt 编程项目. 08.20 -王深//import java.util.Date;
    import java.net.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class ChatServer extends JFrame 
    {
    JTextArea ta = new JTextArea();
    JButton bu = new JButton("刷新");

    //Date date = new Date(); public void launchFrame() //布局方法
    {
    add(ta, BorderLayout.CENTER);
    add(bu, BorderLayout.SOUTH);
    add( new JScrollPane( ta ),BorderLayout.CENTER );
    setBounds(0,0,200,300);
    this.addWindowListener(
    new WindowAdapter() 
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );
    setResizable(false);
    setVisible(true);
    }

    ServerSocket server = null;
    Collection cClient = new ArrayList(); //new 一个ArrayList用于接受客户端机器信息

    public ChatServer(int port) throws Exception
    {
    super("Server信息显示");
    server = new ServerSocket(port);
    launchFrame();
    }

    public void startServer() throws Exception
    {
    while(true)
    {
    Socket s = server.accept(); //阻塞监听
    cClient.add( new ClientConn(s) );
    ta.append("用户IP  :" + s.getInetAddress() + "   端口:" + s.getPort());  //返还客户端的 IP 和端口号
    // ta.appent("现在时间: " + date.toString() );
    ta.append("\n" + "登陆顺序: " + cClient.size() +"\n");
    ta.append("==========================" +"\n");
    }
    }

    class ClientConn implements Runnable
    {
    Socket s = null;
    public ClientConn(Socket s)
    {
    this.s = s;
    (new Thread(this)).start(); //开启新线程
    }

    public void send(String str) throws IOException
    {
    DataOutputStream dos = new DataOutputStream(s.getOutputStream()); //接受字节包装成DATA流
    dos.writeUTF(str);
    }

    public void dispose()
    {
    try {
    if (s != null) s.close();
    cClient.remove(this);
    ta.append("有用户退出 \n");
    ta.append("当前用户数 " + cClient.size() + "\n");
    ta.append("===========================" +"\n");
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    public void run()
    {
    try {

    DataInputStream dis = new DataInputStream(s.getInputStream());  //接受客户端的字符
    String str = dis.readUTF();
    while(str != null && str.length() !=0)
    {
    System.out.println(str); //输出当前CMD
    for(Iterator it = cClient.iterator(); it.hasNext(); )
    {
    ClientConn cc = (ClientConn)it.next(); // ??????????????
    if(this != cc)
    {
    cc.send(str); //写出去
    }
    }
    str = dis.readUTF(); //????????????????
    //send(str);
    }
    this.dispose(); // ???

    catch (Exception e) 
    {
    System.out.println("退出");
    this.dispose(); // ???
    }

    }
    }

    public static void main(String[] args) throws Exception
    {
    ChatServer cs = new ChatServer(8888);
    cs.startServer();
    }
    }//JavaSE 知识总结 Socket+Swing+awt 编程项目. 08.20 -王深
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class ChatClient extends JFrame
    {
    JTextArea ta = new JTextArea();
    JTextField tf = new JTextField(19);
    JLabel lab = new JLabel("按Enter输入");
    JPanel jj = new JPanel();
    public void launchFrame() throws Exception
    {
    jj.add(tf);
    jj.add(lab);
    //setLayout(new GridLayout(2,1));
    //this.add(lab, BorderLayout.NORTH);
    //this.add(ta, new JTextArea());
    this.add(ta, BorderLayout.CENTER);
    this.add(jj, BorderLayout.SOUTH);
    this.add( new JScrollPane( ta ),BorderLayout.CENTER );
    tf.addActionListener( //添加指定的操作侦听器
    new ActionListener() 
    {
    public void actionPerformed(ActionEvent ae)
    {
    try {
    String sSend = tf.getText();
    if(sSend.trim().length() == 0) return; //trim()返回字符串的副本,忽略前导空白和尾部空白
    ChatClient.this.send(sSend);
    tf.setText(""); //重置JTextField当前
    ta.append("我说: "+sSend + "\n");
    }
    catch (Exception e) { e.printStackTrace(); }
    }
    }
    );

    this.addWindowListener( //退出java虚拟机
    new WindowAdapter() 
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );
    setBounds(300,300,300,200);
    setResizable(false); //不可改变窗口大小
    setVisible(true);
    tf.requestFocus();
    }

    Socket s = null;

    public ChatClient() throws Exception
    {
    super("Client交互界面");
    s = new Socket("127.0.0.1", 8888);
    launchFrame();
    (new Thread(new ReceiveThread())).start();
    System.out.println("与服务器连接成功!");
    }

    public void send(String str) throws Exception
    {
    DataOutputStream dos = new DataOutputStream(s.getOutputStream()); //包装输出流
    dos.writeUTF(str); //writeUTF(str);发送出去
    }

    public void disconnect() throws Exception
    {
    s.close(); //关闭Socket流
    }

    public static void main(String[] args) throws Exception
    {
    BufferedReader br = new BufferedReader (
    new InputStreamReader(System.in)); // 输出流
    ChatClient cc = new ChatClient();
    String str = br.readLine();
    while(str != null && str.length() != 0)
    {
    cc.send(str);
    str = br.readLine();
    }
    cc.disconnect(); //调用disconnect方法
    }

    class ReceiveThread implements Runnable
    {
    public void run()
    {
    if(s == null) return;
    try {
    DataInputStream dis = new DataInputStream(s.getInputStream()); //接受传输过来的字符
    String str = dis.readUTF();
    while (str != null && str.length() != 0)
    {
    //System.out.println(str);
    ChatClient.this.ta.append("对方说:" + str + "\n");
    str = dis.readUTF(); //判断str是否为空
    }

    catch (Exception e)
    {
    e.printStackTrace();
    }

    }
    }
    }