基于Java Socket编程的一对一聊天软件

效果如图:

import java.awt.Color;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.*;



public class Client extends JFrame{
	
	private ImageIcon qq = null;
	private JLabel label1 = null;
	private JLabel label2 = null;
	private JTextField tf1 = null;
	private JTextField tf2 = null;
	private JButton button1 = null;		
	private TextArea ta = null;
	private JButton button2 = null;
	private JTextField tf3 = null;
	private Socket s = null;
	private boolean bConnected = false;	//是否连接
	private DataOutputStream dos = null;
	private DataInputStream dis = null;
	private Thread tRecv = null;
	
	
	public void launchFrame() {
		qq = new ImageIcon("images\\QQ.jpg");
		label1 = new JLabel("服务器地址");
		label2 = new JLabel("端口");
		tf1 = new JTextField(10);
		tf2 = new JTextField(10);
		button1 = new JButton("连接");
		ta = new TextArea();
		button2 = new JButton("发送");
		tf3 = new JTextField(20);
		
		
		label1.setBounds(10, 5, 100, 25);
		add(label1);
		tf1.setBounds(80, 5, 130, 25);
		add(tf1);
		label2.setBounds(220, 5, 100, 25);
		add(label2);
		tf2.setBounds(250, 5, 80, 25);
		add(tf2);
		button1.setBounds(320, 5, 60, 25);
		add(button1);
		ta.setBounds(0, 40, 387, 160);
		ta.setEditable(false);	//设置其不可编辑
		ta.setBackground(Color.WHITE);
		add(ta);
		tf3.setBounds(25,220, 250, 26);
		add(tf3);
		button2.setBounds(290,220, 60, 26);
		add(button2);
		
		setSize(400,300);
		setLocation(20, 20);
		setLayout(null);
		setTitle("QQ客户端");
		setIconImage(qq.getImage()); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setResizable(false); //设置不能改变窗口大小
	
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String tf1Str = tf1.getText();
					int portNum = Integer.parseInt(tf2.getText());
					s = new Socket(tf1Str, portNum);
					if(s != null) {
						bConnected = true;
						ta.setText("连接服务器成功!\n");
					}
				} catch (ConnectException e1) {
					ta.setText("服务器超时,请核对输入内容\n");
				}catch(NumberFormatException e1) {
					ta.setText("输入内容不能为空\n");
				}catch(SocketException e1) {
					ta.setText("连接失败!请核对端口号\n");
				} catch (UnknownHostException e1) {
					ta.setText("连接失败!请核对IP地址\n");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
				if(bConnected) {
					try {
						dis = new DataInputStream(s.getInputStream());
						dos = new DataOutputStream(s.getOutputStream());
						tRecv = new Thread(new RecvThread());
						tRecv.start();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
//点击发送后传输数据
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(bConnected) {
					String str = tf3.getText().trim();
					tf3.setText("");
					ta.setText(ta.getText() + "客户端说:" + str + '\n');
					try {
						dos.writeUTF(str);
						dos.flush();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				} else {
					ta.setText(ta.getText() +"未连接,发送失败\n");
				}
			}
		});
//点击回车也可以传输数据		
		tf3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(bConnected) {
					String str = tf3.getText().trim();
					tf3.setText("");
					ta.setText(ta.getText() + "客户端说:" + str + '\n');
					try {
						dos.writeUTF(str);
						dos.flush();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				} else {
					ta.setText(ta.getText() +"未连接,发送失败\n");
				}
			}
		});
		
//窗口关闭后close各种连接		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				closeConnect();
			}
		});
	}

///
//窗口关闭后调用函数关闭连接
	public void closeConnect() {
		try {
			if(dis != null)
				dis.close();
			if(dos != null)
				dos.close();
			if(s != null)
				s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}

//
//连接后启动线程接收服务器内容
	private class RecvThread implements Runnable {
		public void run() {
			while(bConnected) {
				try {
					String str = dis.readUTF();
					ta.setText(ta.getText() + "服务端说:" + str + '\n');
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args) {
		Client client = new Client();
		client.launchFrame();
	}

	

}

import java.awt.Color;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

public class Server extends JFrame{
	private ImageIcon qq = null;
	private TextArea ta = null;
	private JButton button = null;
	private JTextField tf = null;
	private ServerSocket ss = null;
	private Socket s = null;
	private DataOutputStream dos = null;
	private DataInputStream dis = null;
	private boolean bStarted = false;	//服务器是否启动
	private boolean bConnected = false;	//是否连接
	private Thread tRecv = null;

/
//加载窗口
	public void launchFrame() {
		qq = new ImageIcon("images\\QQ.jpg");
		ta = new TextArea();
		button = new JButton("发送");
		tf = new JTextField(20);
		
		ta.setBounds(0, 0, 387, 210);
		ta.setEditable(false);	//设置其不可编辑
		ta.setBackground(Color.WHITE);
		add(ta);
		
		tf.setBounds(25,220, 250, 26);
		add(tf);
		button.setBounds(290,220, 60, 26);
		add(button);
		
		setSize(400,300);
		setLocation(20, 20);
		setLayout(null);
		setTitle("QQ服务器");
		setIconImage(qq.getImage()); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

//点击发送的时候传输数据到服务端和客户端
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(bConnected) {
					String str = tf.getText().trim();
					tf.setText("");
					ta.setText(ta.getText() + "服务器说:" + str + '\n');
					try {
						dos.writeUTF(str);
						dos.flush();
					} catch (IOException e1) {
						e1.printStackTrace();
					}			
				} else {
					ta.setText(ta.getText() +"未连接,发送失败\n");
				}
			}
		});

//点击回车也可以传输数据		
		tf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(bConnected) {
					String str = tf.getText().trim();
					tf.setText("");
					ta.setText(ta.getText() + "服务器说:" + str + '\n');
					try {
						dos.writeUTF(str);
						dos.flush();
					} catch (IOException e1) {
						e1.printStackTrace();
					}			
				} else {
					ta.setText(ta.getText() +"未连接,发送失败\n");
				}
			}
		});
//窗口关闭后close各种连接			
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				closeConnect();
			}
			
		});
	}
	
///
//连接
	public void connect() {
		
		try {
			ta.setText("启动服务器……\n本服务器地址为:" + InetAddress.getLocalHost().getHostAddress() + ";端口号为:5555\n");
			ss = new ServerSocket(5555);
			bStarted = true;
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//当已经启动的时候死循环等待接收客户端,并且启动一个线程进行数据接收	
		if(bStarted) {
			try {
				s = ss.accept();
				dos = new DataOutputStream(s.getOutputStream());
				dis = new DataInputStream(s.getInputStream());
				bConnected = true;
				ta.setText(ta.getText() + "客户端已连接!\n");
				tRecv = new Thread(new RecvThread());
				tRecv.start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}


//RecvThead接受内容的线程
	private class RecvThread implements Runnable {
		public void run() {
			while(bConnected) {
				try {
					String str = dis.readUTF();
					ta.setText(ta.getText() + "客户端说:" + str + '\n');
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
///
	//窗口关闭后调用函数关闭连接
	public void closeConnect() {
		try {
			if(dis != null)
				dis.close();
			if(dos != null)
				dos.close();
			if(s != null)
				s.close();
			if(ss != null)
				ss.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}

	
	public static void main(String[] args) {
		Server server = new Server();
		server.launchFrame();
		server.connect();
	}
	
	
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值