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

📄 client.java

📁 Simple Chat Application using Java Socket Programming.
💻 JAVA
字号:
import java.awt.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.awt.event.*;

public class Client extends Applet implements Runnable
{
	private TextField txt;			
	private TextArea txtout;
	private TextArea message;
	private Label lbl;						//all objects to form the applet
	private Button btn = new Button ("Send");
	private BufferedReader in;			//reading(buffering) incoming messages
	private PrintWriter out;			//writing outgoing messages
	private Socket connection;		//socket object to create the connection
	
	public void init() 		//initializing the applet
	{	setLayout(new BorderLayout());   //setting the layout
		txtout = new TextArea(20,5);		//Setting objects
		txtout.setFont(new Font("Courier", Font.PLAIN, 11));
		txtout.setEditable(false);
		
		message = new TextArea(20,5);
		message.setFont(new Font("Courier", Font.PLAIN, 11));
		message.setEditable(false);
		
		setLayout(new BorderLayout()); 
		txt = new TextField(30);
		
		lbl = new Label("LABEL");

		Sender sender = new Sender();
		
		Panel pnl = new Panel();
		pnl.add(txt);
		pnl.add(btn);
		add("North",pnl);
		add(message);
		add("South",txtout);
		
		btn.addActionListener(sender);	//adding action listeners to button and textfield to be able to send messages to other client(s)
		txt.addActionListener(sender);
		
		setSize( 300, 500 ); //setting size of the window

		try
		{
			int port = 9001;	//setting default port. Port number must be equal to the port number of the Server to manage connection and for Server to listen the clients
			connection = new Socket("localhost", port);	//managing the connection by specifying the host and port number
			in = new BufferedReader(new InputStreamReader( connection.getInputStream()));	//used for reading messages
			out = new PrintWriter( connection.getOutputStream(),true);	//used for writing messages
    	}
    	catch(IOException e)	//if any exception occurs print the exception message
    	{
    		txtout.append("Startup I/O Error: " +e);
    	}
    		
    	new Thread(this).start();	//starting the thread
   		txt.selectAll();
		setVisible(true);
	}
	
	public void run()	//running the client class
	{	try
		{	while (true)	//always perform the actions in the block
			{	String fromServer = in.readLine(); //gets server messages
				if (fromServer != null)	//if detects an incoming message append the message to the textarea
				{	
					if(fromServer.startsWith("Server"))
					{
						message.append(fromServer + '\n');
					}
					else
					{
						txtout.append(fromServer + '\n');
					}
					txt.requestFocus();
				}
				else break;
			}
    	}
    	catch(IOException e)
    	{
    		System.out.println("Error: " +e);
    	}
	}
	
	public void close() //closing the client by closing streams and client socket
	{	
		try
		{
			in.close();
			out.close();
			connection.close();
		}
		catch(IOException e)
		{
			System.out.println("IO Error: "+e);
		}
	}
	
	private class Sender implements ActionListener	//Sender class gets messages from the textfield and sends it to other client(s)
	{	public void actionPerformed(ActionEvent e)
		{
			String s = txt.getText();
			if(!s.equals(null) && !s.equals(""))
			{	
				out.println(s);
				txt.setText("");
			}
		}
	}	
}

⌨️ 快捷键说明

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