java swing版的聊天系统

1.ChatClient
import java.awt.* ;
import java.awt.event.*;
import java.io.*;
import java.net.*;


public class ChatClient extends Frame {

private static final long serialVersionUID = 1L;
private static final int WIDTH = 350 ;
private static final int HEIGHT = 450 ;

TextArea ta = new TextArea() ;

TextField tf = new TextField() ;

Socket s = null ;

DataOutputStream ops = null ;
DataInputStream ips = null ;

private boolean isconnected = false ;



public void launchFrame()
{
setBounds(200, 200, WIDTH, HEIGHT) ;
setTitle("My ChatClient");
this.add(ta,BorderLayout.NORTH) ;
this.add(tf,BorderLayout.SOUTH) ;
pack() ;

this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnect() ;
System.exit(0);
}
});

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

public void connect()
{
try {
if(s == null )
s = new Socket("127.0.0.1", 8888) ;
System.out.println("connected ! ");

isconnected = true ;

ops = new DataOutputStream(s.getOutputStream()) ;
ips = new DataInputStream(s.getInputStream()) ;

ReadMessages rm = new ReadMessages() ;
new Thread(rm).start();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


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


private class TFListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String tfString = tf.getText().trim() ;
//ta.setText(tfString);

try {
ops.writeUTF(tfString);
System.out.println(tfString);
ops.flush();
//ops.close();


} catch (IOException e1) {
e1.printStackTrace();
}
tf.setText(null);

}
}



public static void main(String args) {
new ChatClient().launchFrame() ;

}


class ReadMessages implements Runnable
{

public void run() {
try {
while (isconnected) {
String str = ips.readUTF();
ta.setText(ta.getText()+str+"\n");
}
} catch (IOException e) {
//e.printStackTrace();
System.out.println(" over ! ");
}finally{
try{
if(ips != null ) ips.close();
if(s != null ) s.close();
}catch(Exception e){
e.printStackTrace();
}
}

}

}



}
2.ChatServer

import java.io.*;
import java.util.* ;
import java.net.*;
 
public class ChatServer {
 
    ServerSocket ss = null;
    boolean started = false;
    int NO = 0;
    List<Client> clients = new ArrayList<Client>() ;
     
    public static void main(String args)
    {
        new ChatServer().start() ;     
    }
 
    public void start()
    {
        try {
            ss = new ServerSocket(8888);
            started = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
         
        try {
            while (started) {
                Socket s = ss.accept();
                NO++;
                Client c = new Client(s,NO) ;
                clients.add(c) ;
                 
                new Thread(c).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
         
    class Client implements Runnable {
 
        Socket s = null;
        DataInputStream ips = null;
        DataOutputStream ops = null ;
        private int num;
        boolean isConnected = false;
 
        public Client(Socket s, int num) {     
            try {
                this.s = s;
                ips = new DataInputStream(s.getInputStream());
                ops = new DataOutputStream(s.getOutputStream());
                this.num = num;
                isConnected = true;
            } catch (IOException e) {
                e.printStackTrace();
            }          
        }
         
        private void send(String str)
        {
            try {
                ops.writeUTF(str);
            } catch (IOException e) {
                clients.remove(this) ;
                System.out.println("over");
                //e.printStackTrace();
            }
        }
 
 
        public void run() {
             
            try {
                System.out.println("NO" + num + " client connected ! ");
                while (isConnected) {
                    String message = ips.readUTF();
System.out.println("NO" + num + " : " + message);
                    for(int i =0 ; i < clients.size() ; i++ )
                    {
                        Client c = clients.get(i) ;
                        c.send("NO" + num + " : " + message);
                    }
                }
 
            } catch (IOException e) {
                 
                System.out.println("NO" + num + " client over ! ");
                 
            } finally {
                 
                    try {
                        if (ips != null) ips.close();
                        if(ops != null) ops.close();
                        if (s != null)  {
                            s.close();
                            s = null ;  //±ãÓÚÀ¬»ø»ØÊÕ
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
 
            }
        }
 
    }
 

--来自网页客户端
二维码.jpg
已邀请:

lool - 梦里有时不是有,梦里无时莫强求

赞同来自:

好长啊!

--来自移动客户端

laoshu

赞同来自:

我也写过一个!网络通讯还是很有意思的!

--来自网页客户端

走着 - 。。。。。。。。

赞同来自:

......Σ( ° △ °|||)︴

--来自移动客户端

要回复问题请先登录注册

Template error, template file not found