欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用TCP实现双向聊天系统

程序员文章站 2022-03-15 20:35:23
...

服务器端代码:

package chat1;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.net.*;
import javax.swing.*;

public class Server extends JFrame implements ActionListener,Runnable {
    private JTextArea taMsg=new JTextArea("以下是聊天记录\n");
    private JTextField tfMsg=new JTextField("请您输入信息\n");
    private JButton btSend= new JButton("发送");
    private Socket s=null;
    public Server(){
        this.setTitle("服务器端");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(taMsg,BorderLayout.CENTER);
        tfMsg.setBackground(Color.yellow);
        this.add(tfMsg,BorderLayout.NORTH);
        this.add(btSend,BorderLayout.SOUTH);
        btSend.addActionListener(this);
        this.setSize(200,300);
        this.setVisible(true);
        try {
            ServerSocket ss=new ServerSocket(9999);
            s=ss.accept();
            new Thread(this).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run(){
        try {
            while(true){
                InputStream is=s.getInputStream();
                BufferedReader br=new BufferedReader(new InputStreamReader(is));
                String str=br.readLine();
                taMsg.append(str+"\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent e){
        try {
            OutputStream os=s.getOutputStream();
            PrintStream ps=new PrintStream(os);
            ps.println("服务器说:"+tfMsg.getText());
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Server server1=new Server();
    }
}

客户端代码:

package chat1;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.sound.midi.Transmitter;
import javax.swing.*;

public class Client extends JFrame implements ActionListener,Runnable{
    private JTextArea taMsg=new JTextArea("以下是聊天记录\n");
    private JTextField tfMsg=new JTextField("请您输入信息");  
    private JButton btSend=new JButton("发送");
    private Socket s=null;
    public Client(){
        this.setTitle("客户端");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(taMsg,BorderLayout.CENTER);
        tfMsg.setBackground(Color.yellow);
        this.add(tfMsg,BorderLayout.NORTH);
        this.add(btSend,BorderLayout.SOUTH);
        btSend.addActionListener(this);
        this.setSize(200,300);
        this.setVisible(true);
        try {
            s=new Socket("127.0.0.1", 9999);
            new Thread(this).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run(){
        try {
            while(true){
                InputStream is=s.getInputStream();
                BufferedReader br=new BufferedReader(new InputStreamReader(is));
                String str=br.readLine();
                taMsg.append(str+"\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent e){
        try {
            OutputStream os=s.getOutputStream();
            PrintStream ps=new PrintStream(os);
            ps.println("客户端说:"+tfMsg.getText());
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Client client1=new Client();
    }
}
相关标签: TCP Java 聊天