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

📄 client.java

📁 用JAVA编写的有关使用套接字读取服务器端对象的应用
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Client extends Frame implements Runnable,ActionListener
{
	Button connection;
	Socket socket=null;
	ObjectInputStream in=null;
	ObjectOutputStream out=null;
	Thread thread;
	public Client()
	{
		socket=new Socket();
		connection=new Button("连接服务器,读取文本区对象");
		add(connection,BorderLayout.NORTH);
		connection.addActionListener(this);
		thread=new Thread(this);
		setBounds(100,100,360,310);
		setVisible(true);
		addWindowListener(new WindowAdapter()
		                  {
							  public void windowClosing(WindowEvent e)
							  {
								  System.exit(0);
							   }
						   }
		                 );
	}
	public void run()
	{
		while(true)
		{
			try{
				TextArea text=(TextArea)in.readObject();
				add(text,BorderLayout.CENTER);
				validate();
		        }
			catch(Exception e)
			    {
				  break;
				}
	     }
	 }
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==connection)
        {
			try
			{
				if(socket.isConnected()){}
				else
				{
					InetAddress address=InetAddress.getByName("127.0.0.1");
					InetSocketAddress socketAddress=new InetSocketAddress(address,4331);
					socket.connect(socketAddress);
					in=new ObjectInputStream(socket.getInputStream());
					out=new ObjectOutputStream(socket.getOutputStream());
					thread.start();
				 }
			}
			catch(Exception ee){}
		 }
     }
     public static void main(String args[])
     {
		 Client win=new Client();
	  }
}

⌨️ 快捷键说明

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