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

📄 server.java

📁 东西比较杂
💻 JAVA
字号:
/**
 * 
 */
package com.tiankong;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Administrator
 *
 */
public class Server extends JFrame{
	private JTextField enter;
	private JTextArea display;
	ObjectOutputStream output;
	ObjectInputStream input;
	
	public Server(){
		super("Server");
		Container c=getContentPane();
		enter=new JTextField();
		enter.setEnabled(false);
		enter.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				sendData(e.getActionCommand());
			}
		});
		c.add(enter,BorderLayout.NORTH);
		display=new JTextArea();
		c.add(new JScrollPane(display),BorderLayout.CENTER);
		setSize(300,150);
		setVisible(true);
	}
	
	public void runServer(){
		ServerSocket server;
		Socket connection;
		int counter=1;
		try{
			server=new ServerSocket(5000,100);
			while(true){
				display.setText("Waiting for connection\n");
				connection=server.accept();
				display.append("Connection "+counter+"received from : "+connection.getInetAddress().getHostName());
				output=new ObjectOutputStream(connection.getOutputStream());
				output.flush();
				input=new ObjectInputStream(connection.getInputStream());
				display.append("\nGot I/O stream\n");
				
				String message="SERVER>>>Connection successful";
				output.writeObject(message);
				output.flush();
				enter.setEnabled(true);
				do{
					try{
						message=(String)input.readObject();
						display.append("\n"+message);
						display.setCaretPosition(display.getText().length());
					}catch(ClassNotFoundException e){
						display.append("\nUnknow object type received");
					}
				}while(!message.equals("CLIENT>>>TERMINATE"));
				display.append("\nUser terminated connection");
				enter.setEnabled(false);
				output.close();
				input.close();
				connection.close();
				++counter;
			}
		}catch(EOFException eof){
			System.out.println("Client terminted connection");
		}catch(IOException io){
			io.printStackTrace();
		}
	}
	
	private void sendData(String s){
		try{
			output.writeObject("SERVER>>> "+s);
			output.flush();
			display.append("\nSERVER>>> "+s);
		}catch(IOException cnfex){
			display.append("\nError writing object");
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Server app=new Server();
		app.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		app.runServer();
	}

}

⌨️ 快捷键说明

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