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

📄 chatclient.java

📁 下载的JAVA局域网的聊天程序,请大下们帮我看看 里面有几个问题
💻 JAVA
字号:
/*	范例名称:聊天室程序
 * 	源文件名称:ChatClient.java/ChatServer.java
 *	要  点:
 *		1. Java Socket编程
 *		2. 多线程编程(突出将收、发消息线程分成两个线程)
 *		3. 程序体系结构及设计思想
 */

import  java.io.*;
import  java.net.*;
import  java.awt.*;
import  java.awt.event.*;

public class ChatClient extends Frame implements Runnable,WindowListener,ActionListener //Runnable接口实用性高
{
	int num=1;
	boolean flag=true;
	Thread receiveThread; //客户端收消息线程
	Socket sClient;
	BufferedReader input;
	PrintWriter output;
	String name;
	Label helloLabel=new Label("请输入 :");
	TextField sendText=new TextField(20);
	Button sendButton=new Button("发送");
	TextArea messageText=new TextArea(100,20);
	
	public ChatClient(String name,String servername)
	{
		super("我是 "+name); //接受登录框输入的name
		try
		{
			sClient = new Socket(servername,7); //连接服务器通过servername,port
			input =new BufferedReader(new InputStreamReader(sClient.getInputStream()));//封装客户端输入流(对应于服务器端的输出流)
			output = new PrintWriter(sClient.getOutputStream(),true); //封装客户端输出流(对应于服务器端的输入流)
			output.println(name); //向服务器发送自己的登录名
		}
		catch(IOException e)
		{
			System.out.println("连接不成功!");
			System.exit(1);
		}
		
  		setBackground(new Color(220,220,255));
  		sendText.addActionListener(this);   //绑定监听器
  		sendButton.addActionListener(this); //绑定监听器
  		sendButton.setSize(20,30);
  		Panel cp=new Panel();
  		cp.add(helloLabel);
 		cp.add(sendText);
  		cp.add(sendButton);
  		Panel sp=new Panel();
  		sp.add(messageText);
  		add("North",cp);
  		add("Center",messageText);
  		addWindowListener(this);
  		setSize(400,400);
  		setVisible(true);
  		receiveThread=new Thread(this); 
  		receiveThread.start(); //启动收消息线程
	}
	public void windowActivated(WindowEvent e){
  	}
	public void windowClosed(WindowEvent e){
  	}
	public void windowClosing(WindowEvent e)
	{
		flag=false;
		try
		{
			Thread.sleep(5000); //让关闭窗口的动作延长5秒
		}
		catch(Exception e2)
		{
			System.out.println(e2);
		}
		output.println("End"); //向服务器端输出End
		dispose(); //关闭窗口
		System.exit(0);//退出程序
	}
	
	public void windowDeactivated(WindowEvent e){
  	}
	public void windowDeiconified(WindowEvent e){
  	}
	public void windowIconified(WindowEvent e){
  	}
	public void windowOpened(WindowEvent e){
  	}
	public void actionPerformed(ActionEvent e)
	{
    	
     		sendmessage(); //向服务器端发消息
   		
	}
	synchronized public void run() //收服务端发来的消息
	{
		while(flag)
		{
			try
			{
				Thread.sleep(5000);
			}
			catch(Exception e)
			{
			}			
	  	output.println("PleaseSendALL"); //由此判断是否将消息发给所有的聊天者
	  	try{
	     	String line;
	     	line = input.readLine();
	     	if(line.equals("end")) line = input.readLine();
	     		     	
	     	int ii=Integer.parseInt(line);
	       	messageText.setText("");	
	     	if(ii!=0){
	       		for(int i=0;i<ii*2;i++)
	       		{
	       			line = input.readLine();
	       			//如果得到的消息是"shutdown",表明chat server正在关闭,则显示
	       			//Server has shut down,并停止接受线程
	       			if(line.equals("shutdown"))
	     			{
	     				messageText.insert("服务器已经关闭!",0);
	     				flag = false; // 停止收消息线程
	     			}
	       			messageText.insert(line+"\n",0);  
	       		}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
     }
	public void sendmessage()
	{
		try
		{
			String line;
			line= sendText.getText();
			output.println(line); //向服务器端发消息
			sendText.setText(""); //发送后设置sendText为空
			sendText.requestFocus(); //sendText 获得输入焦点
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
	}
	
	
}

⌨️ 快捷键说明

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