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

📄 chatclient.java

📁 Java编写的用socekt实现的聊天程序的客户端
💻 JAVA
字号:
package Client;

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ChatClient extends Frame implements Runnable, ActionListener {
	public static final int Port = 8765;

	OutputStream os;

	/** **************************************** */
	Panel panel1 = new Panel();

	Panel panel2 = new Panel();

	Panel panel3 = new Panel();

	Panel panel4 = new Panel();

	/** **************************************** */

	static ChatClient frm = new ChatClient();

	/** **************************************** */
	BorderLayout borderLayout1 = new BorderLayout();

	ScrollPane Scrollpane2 = new ScrollPane();

	FlowLayout flowLayout = new FlowLayout();

	GridLayout gridLayout = new GridLayout();

	/** **************************************** */

	static Button Enter = new Button("enter");

	static Button Send = new Button("send");

	static Button Exit = new Button("exit");

	static TextField textField1 = new TextField();

	static TextArea textArea1 = new TextArea();

	static TextField textField2 = new TextField();

	static TextField textField3 = new TextField();

	static TextField textField4 = new TextField();

	static Label label1 = new Label();

	static Label label2 = new Label();

	static Label label3 = new Label();

	Socket socket;

	Thread thread;

	DataInputStream in;

	DataOutputStream out;

	boolean bool = false;

	String name, chat_txt, chat_in;

	String ip = null;

	/** **************************************** */
	/*
	 * public ChatClient() { frm.setTitle("client"); frm.addWindowListener(new
	 * WindowAdapter(){ public void windowClosing(WindowEvent
	 * e){System.exit(0);}}); frm.setSize(400, 400); frm.setLayout(null);
	 * 
	 * textArea1.setText(""); textField1.setText("input user name");
	 * textField1.setBounds(103, 4, 77, 25); textField2.setText("");
	 * textField2.setBounds(4,6,311,22); textField3.setText("");
	 * textField4.setText("8765");
	 * 
	 * button1.setBounds(210,4, 89, 25); button1.addActionListener(frm);
	 * button2.setBounds(322, 5, 73, 25); button2.addActionListener(frm);
	 * button3.setBounds(302, 5, 97, 24); button3.addActionListener(frm);
	 * 
	 * label1.setText("user name"); label1.setBounds(46, 5, 109, 23);
	 * label2.setText("server Addr"); label3.setText("Port");
	 * 
	 * frm.add(textArea1); frm.add(textField1); frm.add(textField2);
	 * frm.add(textField3); frm.add(textField4);
	 * 
	 * frm.add(button1); frm.add(button2); frm.add(button3);
	 * 
	 * frm.add(label1); frm.add(label2); frm.add(label3);
	 * 
	 * frm.setVisible(true); }
	 */
	/** **************************************** */

	public static void main(String args[]) {
		// ChatClient client = new ChatClient();
		frm.setTitle("client");
		frm.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		frm.setSize(400, 400);
		frm.setLayout(null);

		textArea1.setText("");
		textArea1.setBounds(150, 50, 250, 300);
		textField1.setText("input user name");
		textField1.setBounds(10, 150, 77, 25);
		textField2.setText("word");
		textField2.setBounds(10, 200, 100, 22);
		textField3.setText("ip");
		textField3.setBounds(10, 250, 100, 22);
		textField4.setText("8765");
		textField4.setBounds(10, 300, 100, 22);

		Enter.setBounds(10, 40, 89, 25);
		Enter.addActionListener(frm);
		Send.setBounds(10, 70, 73, 25);
		Send.addActionListener(frm);
		Exit.setBounds(10, 100, 97, 24);
		Exit.addActionListener(frm);

		label1.setText("user name");
		label1.setBounds(90, 150, 109, 23);
		label2.setText("server Addr");
		label3.setText("Port");

		frm.add(textArea1);
		frm.add(textField1);
		frm.add(textField2);
		frm.add(textField3);
		frm.add(textField4);

		frm.add(Enter);
		frm.add(Send);
		frm.add(Exit);

		frm.add(label1);
		frm.add(label2);
		frm.add(label3);

		frm.setVisible(true);

	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		if (e.getSource() == Enter)// enter chat room
		{
			/** **************************************** */
			name = textField1.getText();
			ip = textField3.getText();
			/** **************************************** */

			if (name != "input user name" && ip != null) {

				System.out.println("connect");
				try {
					socket = new Socket("localhost", 8765);
					os = socket.getOutputStream();
					InputStream is = socket.getInputStream();

					/** **************************************** */
					BufferedReader cin = new BufferedReader(
							new InputStreamReader(is));
					OutputStream os = socket.getOutputStream();
					/** **************************************** */

					in = new DataInputStream(is);
					out = new DataOutputStream(os);
					/** **************************************** */
				/*	out = new DataOutputStream(socket.getOutputStream());
					in = new DataInputStream(socket.getInputStream());
					socket = new Socket("localhost", Port);
					socket = new Socket(ip, Port);
					is = socket.getInputStream();
					cin = new BufferedReader(new InputStreamReader(is));
					os = socket.getOutputStream();

					in = new DataInputStream(socket.getInputStream());
					out = new DataOutputStream(socket.getOutputStream());*/
					/** **************************************** */
				} catch (IOException e1) {
					System.out.println("can not connect");
				}
				thread = new Thread();
				thread.start();
				bool = true;
			}
		} else if (e.getSource() == Send) {
			chat_txt = textField2.getText();
			if (chat_txt != null) {
				try {
					socket = new Socket("localhost", 8765);
					os = socket.getOutputStream();
					out = new DataOutputStream(os);
					out.writeUTF(chat_txt + "\n");
					System.out.println(chat_txt);
				} catch (IOException e2) {
					System.out.println("发送不能为空");
				}
			} else {
				try {
					out.writeUTF("Speaking");
				} catch (IOException e3) {
				}
			}
		} else if (e.getSource() == Exit)

		{
			System.out.println("ds");
			if (bool == true) {
				try {
					socket.close();
				} catch (IOException e4) {
				}
			}
			// thread.stop();//.destroy();
			bool = false;
			frm.setVisible(false);
		}

	}

	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				chat_in = in.readUTF();
				textArea1.append(chat_in);
			} catch (IOException e) {
			}
		}
	}

}

⌨️ 快捷键说明

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