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

📄 commandthread.java

📁 这是一个用Java编写的路由器模拟程序。可以利用计算机模拟路由器实现路由表的生成、转发等等功能。
💻 JAVA
字号:
import java.net.*;
import java.util.*;
import java.io.*;
import java.lang.*;
class CommandThread extends Thread
{
    //the instance of the router that spawned this thread
    Router r;
    //the port on which the thread listens for commands from the Command program
    int port;
    public CommandThread(Router r, int port)
    {
	//create the thread
	super("Command Thread");

	this.r = r;
	this.port = port;

	//start the thread;
	start();
    }

    /* Method Name: run
     *
     * Use: To start the execution of the thread
     */
    public void run()
    {
	//socket to listen on the command port
	ServerSocket l = null;

	//socket to represent the accepted connection
	Socket t;

	//the input and output streams associated with the connection
	ObjectOutputStream oos;
	ObjectInputStream ois;

	try
	    {
		//listen on the command port
		l = new ServerSocket(port);
		//System.out.println("CommandThread: Listening for commands on port " + port);
	    }
	catch(Exception e)
	    {
		System.out.println("CommandThread: Unable to open a listening socket on port: " + port);
		System.out.println("Quitting.");
		System.exit(1);
	    }

	while(true)
	    {
		try
		    {
			//accept a connection
			t = l.accept();
		    }
		catch(Exception e)
		    {
			System.out.println("CommandThread: An I/O error occured while accepting a connection");
			continue;
		    }

		try
		    {
			//open the streams
			//oos = new ObjectOutputStream(t.getOutputStream());
			ois = new ObjectInputStream(t.getInputStream());

			//read the command
			String in = (String)ois.readObject();	//这里是接受命令,而命令是一个字符串

			//execute the command and send the response
			//String out = r.executeCommand(in);
			//oos.writeObject(out);

			//close the streams and the connection
			ois.close();
			//oos.close();
			t.close();
		    }
		catch(Exception e)
		    {
			System.out.println("CommandThread: An I/O error occured while communicating with COMMAND");
			continue;
		    }
	    }
    }
}

⌨️ 快捷键说明

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