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

📄 server.java

📁 现两台计算机相互通讯数据
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame {
	private JTextField enterField;
	private JTextArea displayArea;
	private ObjectOutputStream output;
	private ObjectInputStream input;
	private ServerSocket server;
	private Socket connection;
	private int counter=1;
	public Server()
	{
		super("Server");
		Container container=getContentPane();
		enterField=new JTextField();
		enterField.setEnabled(false);
		enterField.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event)
					{
						sendData(event.getActionCommand());
						enterField.setText("");
					}
				}
				);
		container.add(enterField,BorderLayout.NORTH);
		displayArea=new JTextArea();
		container.add(new JScrollPane(displayArea),BorderLayout.CENTER);
		setSize(450,200);
		setVisible(true);
		
	}
	public void runServer()
	{
		try{
			server=new ServerSocket(5000,100);
			while(true)
			{
				waitForConnection();
				getStreams();
				processConnection();
				closeConnection();
				++counter;
			}
		}
		catch(EOFException eofException){
			System.out.println("Client terminated connection");
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	private void waitForConnection() throws IOException
	{
		displayArea.setText("Waiting for connection\n");
		connection=server.accept();
		displayArea.append("Connection" + counter + 
				"received from:"+connection.getInetAddress().getHostName());
	}
	private void getStreams() throws IOException
	{
		output=new ObjectOutputStream(
				connection.getOutputStream());
		output.flush();
		input=new ObjectInputStream(
				connection.getInputStream());
		displayArea.append("\nGot I/O stream\n");
	}
	private void processConnection()throws IOException
	{
		String message="SERVER>>>Connection successful";
		output.writeObject(message);
		output.flush();
		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("CLIENT>>>TERMINATE"));
	}
	private void closeConnection()throws IOException
	{
		displayArea.append("\nUser terminates connection");
		enterField.setEnabled(false);
		output.close();
		input.close();
		connection.close();
	}
	private void sendData(String message)
	{
		try{
			output.writeObject("SERVER>>>"+message);
			output.flush();
			displayArea.append("\nSERVER>>>"+message);
		}
		catch(IOException ioexception){
			displayArea.append("\nError writing object");
		}
	}
	public static void main(String args[])
	{
		Server application=new Server();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		application.runServer();
	}
}

⌨️ 快捷键说明

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