单向通信----一对一聊天

package 一对一聊天;
 
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
public class AServer extends JFrame implements ActionListener,Runnable {
    private int Port = 9999;
    private ServerSocket SS;
    private Socket socket;
    
    private JTextArea area = new JTextArea("聊天内容:"+'\n');
    private JTextField field = new JTextField("");
    
    public AServer(){
        this.setTitle("服务器");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(area,BorderLayout.CENTER);
        this.add(field,BorderLayout.NORTH);
        field.addActionListener(this);
        this.setSize(190, 200);
        this.setVisible(true);
        try{            
            SS = new ServerSocket(Port);
            socket = SS.accept();
            new Thread(this).start();
        }catch(Exception ex){        
        }        
    }
    public void run(){
        try{
            while(true){
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String str = br.readLine();                
                area.append(str + '\n');            
            }            
        }catch(Exception ex){            
        }
    }
    public void actionPerformed(ActionEvent e){
        try{
            OutputStream os = socket.getOutputStream();
            PrintStream ps = new PrintStream(os);
            ps.println("服务器说:" + field.getText());
            field.setText("");
        }catch(Exception ex){    
        }
    }    
    public static void main(String[] args){
        new AServer();
    }
}

客户端

package 一对一聊天;
 
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
public class Aclient extends JFrame implements ActionListener, Runnable {
    private Socket socket1;
    private int Port1 = 9999;
    private InetAddress ip1;
    
    private JTextArea area1 = new JTextArea("聊天内容:\n");
    private JTextField field1 = new JTextField("");
    
    public Aclient(){
        this.setTitle("客户端");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(field1,BorderLayout.NORTH);
        field1.addActionListener(this);
        this.add(area1, BorderLayout.CENTER);        
        this.setSize(190, 200);
        this.setVisible(true);
        try{
            ip1 = InetAddress.getByName("Localhost");
            socket1 = new Socket(ip1,Port1);
            OutputStream os = socket1.getOutputStream();
            PrintStream ps = new PrintStream(os);
            ps.println("客户端连接");
            new Thread(this).start();    
        }catch (Exception ex){    
        }
    }
    public void run1(){
        try{
            while(true){
                InputStream is = socket1.getInputStream();
                BufferedReader bf = new BufferedReader(new InputStreamReader(is));
                String str = bf.readLine();
                area1.append(str + '\n');
            }
        }catch (Exception ex){    
        }
    }
    public void actionPerformed1(ActionEvent e){
        try{
            OutputStream os = socket1.getOutputStream();
            PrintStream ps = new PrintStream(os);
            ps.println("客户端说:" + field1.getText());
            field1.setText("");
        }catch (Exception ex){
        }
    }
    public static void main1(String[] args) {
        new Aclient();
    }
 
    private Socket socket;
    private int Port = 9999;
    private InetAddress ip;
    
    private JTextArea area = new JTextArea("聊天内容:\n");
    private JTextField field = new JTextField("");
    
    public void Aclient1(){
        this.setTitle("客户端");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(field1,BorderLayout.NORTH);
        field1.addActionListener(this);
        this.add(area1, BorderLayout.CENTER);        
        this.setSize(190, 200);
        this.setVisible(true);
        try{
            ip1 = InetAddress.getByName("Localhost");
            socket1 = new Socket(ip1,Port1);
            OutputStream os = socket1.getOutputStream();
            PrintStream ps = new PrintStream(os);
            ps.println("客户端连接");
            new Thread(this).start();    
        }catch (Exception ex){    
        }
    }
    public void run(){
        try{
            while(true){
                InputStream is = socket1.getInputStream();
                BufferedReader bf = new BufferedReader(new InputStreamReader(is));
                String str = bf.readLine();
                area1.append(str + '\n');
            }
        }catch (Exception ex){    
        }
    }
    public void actionPerformed(ActionEvent e){
        try{
            OutputStream os = socket1.getOutputStream();
            PrintStream ps = new PrintStream(os);
            ps.println("客户端说:" + field1.getText());
            field1.setText("");
        }catch (Exception ex){
        }
    }
    public static void main(String[] args) {
        new Aclient();
    }
}

 

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-07 21:20:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-07 21:20:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-07 21:20:03       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-07 21:20:03       20 阅读

热门阅读

  1. 关于 Static 静态关键字的使用,你了解多少?

    2023-12-07 21:20:03       40 阅读
  2. 《计算理论导引(原书第3版)》笔记

    2023-12-07 21:20:03       34 阅读
  3. GaussDB数据库SQL系列-LOCK TABLE

    2023-12-07 21:20:03       30 阅读
  4. 系统部署安装-Centos7-PostgreSQL

    2023-12-07 21:20:03       41 阅读
  5. 编译企业微信会话内容存档PHP版SDK扩展

    2023-12-07 21:20:03       41 阅读
  6. Oracle的JDBC在读取数据时报“流已关闭”异常

    2023-12-07 21:20:03       48 阅读