聊天窗口

服务端

package talkingthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {
    ServerSocket server;
    Socket socket;
    public BufferedReader reader;
    private void getserver(int n){
        try{
            server = new ServerSocket(n);
            while(true){
                socket = server.accept();
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public BufferedReader sendMessage(){
        try {
            while(true){
                return reader;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try{
                if(reader != null){
                    reader.close();
                }
                if(socket != null){
                    socket.close();
                }
            }catch(IOException e1){
                e1.printStackTrace();
            }
            return null;
        }
    }
    public static void main(String[] args) {
        ChatServer cserver = new ChatServer();
        cserver.getserver(8998);
    }
}

客户端

package talkingthread;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import talkingthread.ChatServer;

public class ChatClient extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private PrintWriter writer;
    private Socket socket;
    private JTextArea ta = new JTextArea();
    private JTextField tf = new JTextField();
    private Container con;
    public ChatClient(){
        con = this.getContentPane();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBorder(new BevelBorder(BevelBorder.RAISED));
        getContentPane().add(scrollPane);
        scrollPane.setViewportView(ta);
        con.add(tf, "South");
        tf.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                writer.println(tf.getText());
                tf.setText("");
            }
        });
    }
    private void connect(){
        ta.append("尝试连接\n");
        try{
            socket = new Socket("127.0.0.1",8998);
            ta.append("完成连接\n");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private void getMessage(){
        while(true){
            ChatServer chat = new ChatServer();
            BufferedReader mes = chat.sendMessage();
            try {
                if(mes != null){
                    ta.append(mes.toString()+"\n");
                }else{
                    continue;
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args){
        ChatClient client = new ChatClient();
        client.setSize(500,300);
        client.setVisible(true);
        client.connect();
        client.getMessage();
    }
}

似乎是jtextfield部分写的不对。
线程的影响又是什么呢?
反正运行不起来...

你可能感兴趣的:(聊天窗口)