关闭

Java实现最简单局域网QQ

标签: java局域网QQ
925人阅读 评论(0) 收藏 举报
分类:

        Java实现局域网QQ,这是最简单的实现了。整个系统只有一个服务器端和一个客户端,成功建立连接后,服务器端发消息给客户端,客户端发消息给服务器端。最上边的文本框是显示接收到的消息的,左边清屏按钮是把接收区域清空的,中间空白的是输入框,右边发送按钮是发送到另一端的,最下面的文本框显示QQ的连接状态。


先来一张图



源代码-服务器端

package server;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Server extends JFrame {
	private JButton b1 = new JButton("发送");
	private JButton b2 = new JButton("清屏");
	// 输入框
	private JTextField text = new JTextField(10);
	// 显示连接状态
	private JTextField info = new JTextField(10);
	// 显示文本框
	private JTextArea area = new JTextArea(10, 25);
	
	private DataInputStream in = null;
	private DataOutputStream out = null;
	// 是否和客户端已建立连接标识
	private boolean connect = false;
	
	public static void main(String[] args){
		Server frame = new Server();
		frame.setTitle("QQ-服务器端");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 500);
		frame.setVisible(true);
		frame.showInfo("QQ服务器开始启动...");

		frame.workCycle(frame);
	}
	
	public Server() {
		// 为b1按钮添加监听事件,“发送”
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (connect) {
					String str = text.getText();
					text.setText(""); // 设置为空
					
					Date date = new Date();
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					area.append(format.format(date) + '\n');
					area.append("我:" + str + '\n');
					try {
						out.writeBytes(str + '\n'); // 加上最后的'\n'为了在另一端显示换行
					} 
					catch (IOException ex) {
						ex.printStackTrace();
					}
				}
			}
		});
		
		// 为b2按钮添加监听事件,“清屏”
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					area.setText("");
			}
		});
		
		add(BorderLayout.NORTH, new JScrollPane(area));
		add(BorderLayout.CENTER, text);
		add(BorderLayout.SOUTH, info);
		add(BorderLayout.EAST, b1);
		add(BorderLayout.WEST, b2);
	}
	
	private void showInfo(String str) {
		info.setText(str);
	}
	
	public void workCycle(Server frame) {
		try {
			ServerSocket server = new ServerSocket(60000);
			
			Socket socket = server.accept();
			
			frame.showInfo("客户端连接成功...");
			connect = true;
			in = new DataInputStream(socket.getInputStream());
			out = new DataOutputStream(socket.getOutputStream());

			for (;;) { // 始终和客户端连接
				/*
				char c = in.readChar();
				if (c != '\n')
					area.append("" + c); // (String)c 这样转换竟然不行...
				else
					area.append("" + '\n');
				*/
				String str = in.readLine();
				
				Date date = new Date();
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				area.append(format.format(date) + '\n');
				area.append("client: " + str + '\n');
			}
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}

源代码-客户端

package client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client extends JFrame {
	private JButton b1 = new JButton("发送");
	private JButton b2 = new JButton("清屏");
	// 输入框
	private JTextField text = new JTextField(10);
	// 显示连接状态
	private JTextField info = new JTextField(10);
	// 显示文本框
	private JTextArea area = new JTextArea(10, 25);
	
	private DataInputStream in = null;
	private DataOutputStream out = null;
	// 是否和服务器端已建立连接标识
	private boolean connect = false;
	
	public static void main(String[] args){
		Client frame = new Client();
		frame.setTitle("QQ-客户端");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 500);
		frame.setVisible(true);
		frame.showInfo("QQ客户端开始启动...");

		frame.workCycle(frame);
	}
	
	public Client() {
		// 为b1按钮添加监听事件,“发送”
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (connect) {
					String str = text.getText();
					text.setText(""); // 设置为空
					
					Date date = new Date();
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					area.append(format.format(date) + '\n');
					area.append("我:" + str + '\n');
					try {
						out.writeBytes(str + '\n'); // 加上最后的'\n'为了在另一端显示换行
					} 
					catch (IOException ex) {
						ex.printStackTrace();
					}
				}
			}
		});
		
		// 为b2按钮添加监听事件,“清屏”
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					area.setText("");
			}
		});
		
		add(BorderLayout.NORTH, new JScrollPane(area));
		add(BorderLayout.CENTER, text);
		add(BorderLayout.SOUTH, info);
		add(BorderLayout.EAST, b1);
		add(BorderLayout.WEST, b2);
	}
	
	private void showInfo(String str) {
		info.setText(str);
	}
	
	public void workCycle(Client frame) {
		try {
			Socket socket = new Socket("localhost", 60000);
			
			frame.showInfo("连接服务器成功...");
			connect = true;
			in = new DataInputStream(socket.getInputStream());
			out = new DataOutputStream(socket.getOutputStream());

			for (;;) { // 始终和服务器端连接
				/*
				char c = in.readChar();
				if (c != '\n')
					area.append("" + c); // (String)c 这样转换竟然不行...
				else
					area.append("" + '\n');
				*/
				String str = in.readLine();
				
				Date date = new Date();
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				area.append(format.format(date) + '\n');
				area.append("server: " + str + '\n');
			}
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:99411次
    • 积分:2102
    • 等级:
    • 排名:第18847名
    • 原创:112篇
    • 转载:15篇
    • 译文:1篇
    • 评论:8条
    博客专栏
    最新评论