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 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文件 如果你想看源码的话 你自己必须用工具将class文件反编译出来 如果你要的话 加我QQ 我发给你 396801753
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();
}
}
}
}
先起服务器,再起用户,用户间可以聊天
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();
}
}
}
}