java 网络聊天程序

//Server
package test7;

import java.awt.EventQueue;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.Collections;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Server {

	private JFrame frame;
	static JTextArea input;
	static JTextArea view ;
	static DataInputStream dis;
	static DataOutputStream dos;
	static Socket so;
	static ServerSocket ss;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Server window = new Server();
					
					window.frame.setTitle("Server");
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		try {
			
			ss = new ServerSocket(1001);
			so = ss.accept();
			System.out.println("Connect successfully");
			dis = new DataInputStream(so.getInputStream());
			dos = new DataOutputStream(so.getOutputStream());
			while(true)
			{
				String s = dis.readUTF();
				view.setText(s);
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			try {
				so.close();
				dis.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
//				e1.printStackTrace();
			}
//			e.printStackTrace();
		}
	}

	/**
	 * Create the application.
	 */
	public Server() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		input = new JTextArea();
		input.setBounds(38, 167, 201, 30);
		frame.getContentPane().add(input);
		
		view = new JTextArea();
		view.setEditable(false);
		view.setBounds(38, 13, 201, 115);
		frame.getContentPane().add(view);
		
		JLabel lblView = new JLabel("view");
		lblView.setBounds(291, 65, 72, 18);
		frame.getContentPane().add(lblView);
		
		JButton btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					dos.writeUTF(input.getText());
					dos.flush();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnSend.setBounds(291, 170, 113, 27);
		frame.getContentPane().add(btnSend);
	}
}

//Client

package test7;

import java.awt.EventQueue;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Client {
	public Client(Socket so) {
		this.so = so;
	}
	private JFrame frame;
	static JTextArea view;
	static JTextArea input;
	static DataInputStream dis;
	static DataOutputStream dos;
	static Socket so;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Client window = new Client();
					window.frame.setTitle("Client");
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		try {
			so = new Socket("127.0.0.1", 1004);
			dis = new DataInputStream(so.getInputStream());
			dos = new DataOutputStream(so.getOutputStream());
			while(true)
			{
				String s = dis.readUTF();
				view.setText(s);
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			try {
				dis.close();
				so.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
//				e1.printStackTrace();
			}
//			e.printStackTrace();
		}
		
	}

	/**
	 * Create the application.
	 */
	public Client() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		view = new JTextArea();
		view.setEditable(false);
		view.setBounds(35, 25, 202, 96);
		frame.getContentPane().add(view);
		
		input = new JTextArea();
		input.setBounds(35, 164, 202, 38);
		frame.getContentPane().add(input);
		
		JLabel lblView = new JLabel("view");
		lblView.setBounds(302, 63, 72, 18);
		frame.getContentPane().add(lblView);
		
		JButton btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					dos.writeUTF(input.getText());
					dos.flush();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnSend.setBounds(285, 175, 113, 27);
		frame.getContentPane().add(btnSend);
	}

}

猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/80662163
今日推荐