今天学习写了简单的聊天通信,先看人家写的,毕竟还不能随手撸代码,得努力。还有些瑕疵,但是还能玩,挺有意思的,在这块玩了半天!哈哈哈哈!在改进吧,然后,要复习一下里面的知识点,复习一下IO,在复习一下集合类,不复习都要忘完了。赶紧去复习了
客户端
import java.awt.Container;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Client extends JFrame{
DataOutputStream dos = null;
DataInputStream dis = null;
JButton send = new JButton("发送");
JButton content = new JButton("连接");
JButton clear = new JButton("清除");
TextArea toContent = new TextArea();
JTextField tfTxt = new JTextField(10);
JPanel p1 =new JPanel();
Thread t = new Thread(new RecToServer());
Socket s = null;
boolean bConnected = false;
public void LanuchFrame(){
toContent.setEditable(false); // 使taContent不能被编辑
Container c = getContentPane();
p1.add(send);
p1.add(content);
p1.add(clear);
c.add(toContent, "North");
c.add(tfTxt, "Center");
c.add(p1, "South");
this.setSize(300, 350);
this.setLocation(400, 200);
this.setTitle("Chat Client");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.addActionListener(new Content());
send.addActionListener(new SendMsg());
clear.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
toContent.setText("");
// TODO Auto-generated method stub
}
});
}
public void contentSever() throws IOException, IOException{
s=new Socket("192.168.1.74",11112);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
bConnected=true;
}
public void disConnect() {
try {
if (s != null) {
s.close();
}
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
Client tc = new Client();
tc.LanuchFrame();
// TODO Auto-generated method stub
}
private class Content implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="连接"){
try {
contentSever();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
t.start();
content.setText("断开连接");
}else if(e.getActionCommand()=="断开连接"){
disConnect();
content.setText("连接");
}
// TODO Auto-generated method stub
}
}
private class SendMsg implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="发送"){
String str = tfTxt.getText();
tfTxt.setText("");
try {
dos.writeUTF(str);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
// TODO Auto-generated method stub
}
}
private class RecToServer implements Runnable {
@Override
public void run() {
while (bConnected) {
try {
String str = dis.readUTF();
toContent.append(str + "\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// TODO Auto-generated method stub
}
}
服务器
import java.util.List;
import java.awt.Color;
import java.awt.Container;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import chat111.TCPServer.Client;
public class Server extends JFrame{
private ServerSocket ss= null;
boolean bstart=false;
private JTextArea toContent = new JTextArea();
private int index = 0;
List<Client> clients = new ArrayList<Client>(); // 用于存客户端
public void launchFrame() throws IOException{
Container c = this.getContentPane();
toContent.setEditable(false);
toContent.setBackground(Color.DARK_GRAY);
toContent.setForeground(Color.GREEN);
this.add(toContent);
this.setSize(300, 350);
this.setLocation(400, 200);
this.setTitle("TCP Server");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tcpMonitor();
}
public void tcpMonitor() throws IOException{
ss = new ServerSocket(11112);
bstart = true;
while(bstart){ //这时候有客户端进来了 开始接收
index++;
Socket s=ss.accept();
Client c = new Client(s);
clients.add(c);
toContent.append(s.getInetAddress().getHostAddress() //写出哪个客户端进来了
+ " connected " + index + " clients\n");
new Thread(c).start(); //执行线程
}
}
public static void main(String[] args) throws IOException {
Server s1 = new Server();
s1.launchFrame();
}
private class Client implements Runnable{
DataOutputStream dos = null;
DataInputStream dis = null;
Socket s = null;
boolean bstart = false;
public Client(Socket s) throws IOException{
this.s = s;
dos = new DataOutputStream(s.getOutputStream());//包装通道内的流
dis = new DataInputStream(s.getInputStream());
bstart = true;
}
@Override
public void run() {
while(bstart){
try {
String str =dis.readUTF();//读出来 字符
System.out.println(s);
for(int i=0;i<clients.size();i++){
Client c = clients.get(i);
c.sendToEveryClient(str); //调用sendToEveryClient(s)方法给客户端发东西
}
} catch (IOException e) {
e.printStackTrace();
}
// TODO Auto-generated method stub
}
}
public void sendToEveryClient(String str) {
try{
dos.writeUTF(str);
dos.flush();
}catch(IOException e){ //客户端掉线了
index--;
clients.remove(this);
toContent.append(s.getInetAddress().getHostAddress()
+ " exited " + index + " clients\n");
}
}
}
}