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

📄 client.java

📁 用VLML和java实现的虚拟手机
💻 JAVA
字号:
//Java core packages
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;


public class Client extends Frame{
	private TextField enterField;
	private TextArea displayArea;
	private ObjectOutputStream output;
	private ObjectInputStream input ;
	private String message = "";
	private String chatServer;
	private Socket client;


	//set up GUI
	public Client( String host )
	{
        super("Client");

		chatServer = host;


		enterField = new TextField();
		enterField.setEnabled(false);

		enterField.addActionListener(
			new ActionListener(){

			public void actionPerformed(ActionEvent event)
			{
				sendData(event.getActionCommand());
			}
		}
	);

  add(enterField,BorderLayout.NORTH);


	displayArea = new TextArea();
	add(displayArea);

	setSize(300,150);
	setVisible(true);
}

//public void runServer()
public void runClient()
{

	try{
		connectToServer();
		getStreams();
		processConnection();
		closeConnection();
	}

	catch ( EOFException eofException){
		System.out.println("Server terminated connection");
	}

	catch ( IOException ioException){
		ioException.printStackTrace();
	}
}


private void getStreams() throws IOException
{
	output=new ObjectOutputStream(client.getOutputStream());
	output.flush();

	input = new ObjectInputStream(client.getInputStream() );
	displayArea.append("\nGot I/O streams\n");
}

private void connectToServer() throws IOException
{
	displayArea.setText("Attemption connection\n");
	client = new Socket(InetAddress.getByName(chatServer),5000);
	displayArea.append("Connected to:" + client.getInetAddress().getHostName());
}

private void processConnection() throws IOException
{
	enterField.setEnabled(true);
	do{
		try{
			message = (String)input.readObject();
			displayArea.append("\n"+message);
			displayArea.setCaretPosition(displayArea.getText().length());
		}

		catch ( ClassNotFoundException classNotFoundException){
			displayArea.append("\nUnknown object type received");
		}

	}while (!message.equals("SERVER>>>TERMINATE"));
}

private void closeConnection()throws IOException{
	displayArea.append("\nClosing connection");
	output.close();
	input.close();
	client.close();
}

private void sendData(String message)
{
	try{
		output.writeObject("CLIENT>>>" + message);
		output.flush();
		displayArea.append("\nCLIENT>>>"+message);
	}
	catch(IOException ioException){
		displayArea.append("\nError writing object");
	}
}
	public static void main(String args[])
	{
		Client application;

		if( args.length == 0 )
			application = new Client("127.0.0.1");
		else
			application = new Client(args[0]);

		application.runClient();
	}
}

⌨️ 快捷键说明

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