⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatclient.java

📁 小小聊天室
💻 JAVA
字号:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;
/**
 *聊天程序的客户端
 **/
class ChatClient extends JFrame implements Runnable
{
	//声明使用到的控件
	JPanel p1 = new JPanel(new BorderLayout());
	JPanel p11 = new JPanel(new FlowLayout(FlowLayout.CENTER,10,5));
	JLabel lb1 = new JLabel("服务器地址:");
	JLabel lb2 = new JLabel("服务器端口:");
	JTextField tfip = new JTextField();
	JTextField tfport = new JTextField();
	JTextPane tpcontent = new JTextPane();
	JButton btnsend = new JButton("发送");
	JTextPane tplog = new JTextPane();
	JPanel p2 = new JPanel(new BorderLayout());
	JLabel lbstate = new JLabel("初始化");
	//声明网络编程所需的变量
	public static Socket sock;
	public static InputStream in;
	public static OutputStream out;
	public static String strcontent = new String("");
	/**
	 *构造方法。首先构造界面,然后不断尝试与服务器连接
	 **/
	ChatClient()
	{
		super("小小聊天室-客户端");
		Font ft = new Font("宋体",0,12);
		btnsend.setFont(ft);
		tfport.setText(new String(String.valueOf(ChatServer.PORT)));
		tfip.setText("127.0.0.1");
		lb1.setFont(ft);
		lb2.setFont(ft);
		tplog.disable();
		tfip.disable();
		tfport.disable();
		p11.add(lb1);
		p11.add(tfip);
		p11.add(lb2);
		p11.add(tfport);
		tpcontent.setFont(ft);
		tplog.setFont(ft);
		JScrollPane scrp1 = new JScrollPane(tpcontent);
		TitledBorder tb = new TitledBorder("需要发送的信息");
		tb.setTitleFont(ft);
		scrp1.setBorder(tb);
		p1.add(p11,BorderLayout.NORTH);
		p1.add(Box.createHorizontalStrut(10),BorderLayout.WEST);
		p1.add(scrp1,BorderLayout.CENTER);
		p1.add(Box.createHorizontalStrut(10),BorderLayout.EAST);
		JScrollPane scrp2 = new JScrollPane(tplog);
		TitledBorder tb2 = new TitledBorder("日志");
		tb2.setTitleFont(ft);
		scrp2.setBorder(tb2);
		lbstate.setFont(ft);
		p2.add(scrp2,BorderLayout.CENTER);
		p2.add(Box.createVerticalStrut(10),BorderLayout.NORTH);
		p2.add(Box.createHorizontalStrut(10),BorderLayout.WEST);
		p2.add(Box.createHorizontalStrut(10),BorderLayout.EAST);
		p2.add(lbstate,BorderLayout.SOUTH);
		//总体的布局
		setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
		add(p1);
		add(Box.createVerticalStrut(10));
		btnsend.addActionListener(new SendListener());
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		panel.add(btnsend);
		add(panel);
		add(p2);
		//显示界面
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(500,500);
		setVisible(true);
		//尝试与服务器建立连接
		int failedTimes=0;
		while(sock == null)
		{
			if(failedTimes>=1){
				System.out.println("与服务器连接失败");
				lbstate.setText("与服务器连接失败");
				btnsend.setEnabled(false);
				tpcontent.setEnabled(false);
				return;
			}

			try{
				InetAddress ip = InetAddress.getByName("127.0.0.1");
				int port  = ChatServer.PORT;
				sock = new Socket(ip,port);
				in = sock.getInputStream();
				out = sock.getOutputStream();
			}catch(IOException e)
			{
				failedTimes++;
				sock = null;
				lbstate.setText("正在试图与服务器建立连接...尝试"+failedTimes+"次");
				System.out.println("正在试图与服务器建立连接...尝试"+failedTimes+"次");
				try{
					Thread.sleep(5000);
				}catch(InterruptedException ine)
				{
					System.out.println("中断异常!");
				}
			}
		}
	}
	/**
	 *线程函数。不断的接受服务器的信息
	 **/
	public void run()
	{
		try{
			String temp = new String("");
			byte[] infor = new byte[200];
			int len;
			while(true)
			{
				if((in==null)||(out==null))
					continue;
				temp = "IP= " + sock.getInetAddress() + " PORT = " + sock.getPort();
				len = in.read(infor);
				if(len != -1)
				{
					strcontent += temp + "发送:\n";
					temp = new String(infor,0,len);
					strcontent += temp + "\n";
					tplog.setText(strcontent);
				}
			}
		}catch(IOException er)
		{
			er.printStackTrace();
		}
	}
	/**
	 *主函数。初始化界面,最后关闭套接字
	 **/
	public static void main(String[] args)throws IOException
	{
		try{
			ChatClient cc = new ChatClient();
			cc.run();
		}finally
		{
			sock.close();
		}

	}
	/**
	 *接收数据的线程类
	 **/
	class ClientRcvThread extends Thread
	{

	/**
	 *构造方法。仅仅开启线程
	 **/
		ClientRcvThread()
		{
			start();
		}
		/**
		 *得到用户想要发送的内容发送出去,之后清空内容面板
		 **/
		public void run()
		{
			try{
				String str = tpcontent.getText();
				out.write(str.getBytes());
				tpcontent.setText(new String(""));
			}catch(IOException e)
			{
				System.out.println("发送数据错误!");
			}
		}
	}
	/**
	 *"发送"按钮的监听器类
	 **/
	class  SendListener implements ActionListener
	{
		/**
		 *仅仅开启一个用于发送数据的线程
		 **/
		public synchronized void actionPerformed(ActionEvent event)
		{
			new ClientRcvThread();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -