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

📄 chatclient.java

📁 J2SE视频 第0讲_1 视频ppt及源码.rar
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class ChatClient extends Frame
{
	TextArea ta = new TextArea();
	TextField tf = new TextField();
	public void launchFrame() throws Exception
	{
		this.add(ta, BorderLayout.CENTER);
		this.add(tf, BorderLayout.SOUTH);
		tf.addActionListener(
			new ActionListener() 
			{
				public void actionPerformed(ActionEvent ae)
				{
					try {
						String sSend = tf.getText();
						if(sSend.trim().length() == 0) return;
						ChatClient.this.send(sSend);
						tf.setText("");
						ta.append(sSend + "\n");
					}
					catch (Exception e) { e.printStackTrace(); }
				}
			}
			);
		
		setBounds(300,300,300,400);
		setVisible(true);
		tf.requestFocus();
	}
	
	Socket s = null;
	
	public ChatClient() throws Exception
	{
		s = new Socket("127.0.0.1", 8888);
		launchFrame();
		(new Thread(new ReceiveThread())).start();
	}
	
	public void send(String str) throws Exception
	{
		DataOutputStream dos = new DataOutputStream(s.getOutputStream());
		dos.writeUTF(str);
	}
	
	public void disconnect() throws Exception
	{
		s.close();
	}
	
	public static void main(String[] args) throws Exception
	{
		BufferedReader br = new BufferedReader (
								new InputStreamReader(System.in));
		ChatClient cc = new ChatClient();
		String str = br.readLine();
		while(str != null && str.length() != 0)
		{
			cc.send(str);
			str = br.readLine();
		}
		cc.disconnect();
	}
	
	class ReceiveThread implements Runnable
	{
		public void run()
		{
			if(s == null) return;
			try {
				DataInputStream dis = new DataInputStream(s.getInputStream());
				String str = dis.readUTF();
				while (str != null && str.length() != 0)
				{
					//System.out.println(str);
					ChatClient.this.ta.append(str + "\n");
					str = dis.readUTF();
				}
			} 
			catch (Exception e)
			{
				e.printStackTrace();
			}
			
		}
	}
}

⌨️ 快捷键说明

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