下面把网络课设的源码拿出来晒晒,供大家参考
//检测线程
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.wst;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
*
* @author Administrator
*/
public class TelnetThread implements Runnable {
private int port;
private String ip;
public void setPort(int port) {
this.port = port;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public String getIp() {
return ip;
}
public TelnetThread() {
ip = "127.0.0.1";
port = 8080;
}
public TelnetThread(String ip, int port) {
this.port = port;
this.ip = ip;
}
public void run() {
try {
Socket server = new Socket();
InetSocketAddress address = new InetSocketAddress(ip, port);
server.connect(address, 1000);
System.out.println(address.toString() + "可达");
} catch (IOException e) {
System.out.println("Thread: "+ip+":"+port+"不可达");
}
}
}
//检测主函数
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.wst;
import java.io.IOException;
import java.net.InetAddress;
/**
*
* @author Administrator
*/
public class TelnetTest {
public static void main(String[] args) {
try {
System.out.println("======InetAddress测试IP是否可达======");
InetAddress addressLocal = InetAddress.getLocalHost();
InetAddress addressRobot = InetAddress.getByName("210.45.251.3");
if (addressLocal.isReachable(3000)) {
System.out.println(addressLocal.toString() + "可达");
} else {
System.out.println(addressLocal.toString() + "不可达");
}
if (addressRobot.isReachable(3000)) {
System.out.println(addressRobot.toString() + "可达");
} else {
System.out.println(addressRobot.toString() + "不可达");
}
System.out.println("======Telnet测试IP是否可达======");
for (int i = 20; i < 8000; i++) {
Thread socketThread = new Thread(new TelnetThread("127.0.0.1",i));
socketThread.start();
}
} catch (IOException e) {
System.out.println("Main");
}
// Runtime run = Runtime.getRuntime();
//
// String msg = "";
// Process p = null;
// for (int i =20; i < 4000; i++) {
// StringBuffer command = new StringBuffer("telnet localhost ");
// p = run.exec((command.append(" ").append(i)).toString());
// BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
// while ((msg = in.readLine()) != null) {
// System.out.println("returnMsg: "+msg);
// }
//// System.out.println("End: "+(command.toString()));
// }
}
}