利用socket编写聊天程序

import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerProgram extends Frame implements ActionListener{

    Label label=new Label("交谈内容");
    Panel panel=new Panel();
    TextField tf=new TextField(10);
    TextArea ta=new TextArea();
    ServerSocket server;
    Socket client;
    InputStream in;
    OutputStream out;
    public ServerProgram() {
        super("服务器");
        setSize(250, 250);
        panel.add(label);panel.add(tf);
        tf.addActionListener(this);
        add("North", panel);add("Center", ta);
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setVisible(true);
        
        try {
            server=new ServerSocket(4000);
            client=server.accept();
            ta.append("客户机是:"+client.getInetAddress().getHostName()+"\n\n");
            in=client.getInputStream();
            out=client.getOutputStream();
        } catch (IOException e1) {    }    
        while(true) {            
            try {
                byte[] buf=new byte[256];
                in.read(buf);
                String str=new String(buf);
                ta.append("客户机说:"+str+"\n");
            } catch (IOException e1) {    }            
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        try {
            String str=tf.getText();
            byte[] buf=str.getBytes();
            tf.setText(null); 
            out.write(buf);
            ta.append("我说:"+str+"\n");
        } catch (IOException ioe) {    }        
    }    
    public static void main(String[] args) {
        new ServerProgram();
    }
}
 


import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ClientProgram extends Frame implements ActionListener{
    Label label=new Label("交谈内容");
    Panel panel=new Panel();
    TextField tf=new TextField(10);
    TextArea ta=new TextArea();
    Socket client;
    InputStream in;
    OutputStream out;
    public ClientProgram() {
        super("客户机");
        setSize(250, 250);
        panel.add(label);panel.add(tf);
        tf.addActionListener(this);
        add("North", panel);add("Center", ta);
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setVisible(true);
        
        try {
            client=new Socket(InetAddress.getLocalHost(), 4000);
            ta.append("服务器是:"+client.getInetAddress().getHostName()+"\n\n");
            in=client.getInputStream();
            out=client.getOutputStream();
        } catch (IOException e1) {    }    
        while(true) {            
            try {
                byte[] buf=new byte[256];
                in.read(buf);
                String str=new String(buf);
                ta.append("服务器说:"+str+"\n");
            } catch (IOException e1) {    }            
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        try {
            String str=tf.getText();
            byte[] buf=str.getBytes();
            tf.setText(null); 
            out.write(buf);  //将字节数组写入到流
            ta.append("我说:"+str+"\n");
        } catch (IOException ioe) {    }        
    }
    public static void main(String[] args) {
        new ClientProgram();
    }
}
 


运行结果:

猜你喜欢

转载自blog.csdn.net/xxx_1_1/article/details/83213233