獲取ip地址課程設計
㈠ 計算機網路課程設計使用ARP協議獲取區域網內部活動主機的物理地址java
提供目的設備的Mac地址。例如計算機A和計算機B通訊,計算機A的arp表中,會版有計算機權B的IP及對應計算機B的Mac。同理,計算機B中的arp表也有計算機A的IP及對應計算機A的Mac。可在dos命令框里使用arp —a查看
㈡ 誰有監控IP包流量的課程設計,跪求給一個
㈢ 最近在做一個課設:基於Socket 的區域網聊天工具。請問伺服器端和客戶端是怎麼通過IP地址找到對方的
Server端不是要建立抄serversocket么,這個類會監聽socket連接,客戶端連接服務端的時候會攜帶本機(客戶端)的ip,伺服器接收連接,經過三次握手之後雙方建立tcp連接,然後就可以通訊了,編寫服務端的時候並不需要關心客戶端發起連接的埠。
獲取ip:socket.getInetAddress()
獲取埠:socket.getPort();
㈣ 計算機網路課程設計《公司區域網設計》或者《校園區域網設計》,按下面的要求
於加深網路模型的各層功能和設計思想的理解
㈤ 用java 編寫一個可以實現IP地址查詢功能的課程設計
下面是獲得本機IP地址的方法,跟你的程序捆綁起來,互相發送消息的時候直接將IP發送過去
private static String[] getAllLocalHostIP(){
List<String> res=new ArrayList<String>();
Enumeration netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
Enumeration nii=ni.getInetAddresses();
while(nii.hasMoreElements()){
ip = (InetAddress) nii.nextElement();
if (ip.getHostAddress().indexOf(":") == -1) {
res.add(ip.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return (String[])res.toArray(new String[0]);
}
這是個掃描區域網ip的windows解決方案,在unix系統下可能有問題
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class LanIP {
public ArrayList<String> getLanIPArrayList() {
ArrayList<String> arrayIP = null;
try {
InitSystem initSystem = null;
initSystem = new InitSystem();
Thread thread = new Thread(initSystem);
thread.start();
thread.join();
arrayIP = initSystem.getArrayIPUsed();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return arrayIP;
}
private class InitSystem implements Runnable {
private int firstIP = 2;// 查詢的 IP 地址的最後一位起始點
private int lastIP = 255;// 查詢的 IP 地址的最後一位結束點
private volatile ArrayList<Thread> arrayThread;// 子線程段
private final int MAXTHREADNUM = 30; // 最大同時進行的子線程數量
private int threadNumNow;// 當前正在進行的子線程數量
private volatile ArrayList<String> arrayIP;// 區域網查詢所有可能的 IP 地址的結果集
private volatile ArrayList<String> arrayIPUsed;// 區域網查詢已經使用的 IP 地址的結果集
private InitSystem(String ip) {
arrayIP = new ArrayList<String>();
arrayIPUsed = new ArrayList<String>();
arrayThread = new ArrayList<Thread>();
setIPAddressList(ip);
}
private InitSystem() throws UnknownHostException {
this(InetAddress.getLocalHost().getHostAddress());
}
private synchronized ArrayList<String> getArrayIPUsed() {
try {
while (arrayIP.size() > 0) {
Thread.sleep(300);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return arrayIPUsed;
}
private void setIPAddressList(String ip) {
// 根據這個 ip 地址查詢它所在的區域網的所有可能 IP 地址的集合
int lastPointIndex = ip.lastIndexOf('.');
String stringIPHead = ip.substring(0, ++lastPointIndex);
String stringIP = null;
for (int i = firstIP; i <= lastIP; i++) {
stringIP = stringIPHead + i;
arrayIP.add(stringIP);
}
}
public void run() {
synchronized (this) {
try {
while (arrayIP.size() > 0) {
while (threadNumNow >= MAXTHREADNUM) {
for (Thread thread : arrayThread) {
if (!thread.getState().equals(
Thread.State.TERMINATED)) {
thread.join(5);
}
--threadNumNow;
}
arrayThread = new ArrayList<Thread>();
}
Thread thread = new Thread(new InnerClass(arrayIP
.remove(0)));
thread.start();
threadNumNow++;
arrayThread.add(thread);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private class InnerClass implements Runnable {
// 線程查詢一個 IP 是否是可以連接的 是則加入到相應的 IP 數組
private String ip;
private InnerClass(String ip) {
this.ip = ip;
}
private boolean isUsedIPAddress(String ip) {
synchronized (this) {
// 判斷這個 IP 地址在當前區域網中是否是可連接的 IP
Process process = null;
BufferedReader bufReader = null;
String bufReadLineString = null;
try {
process = Runtime.getRuntime().exec(
"ping " + ip + " -w 100 -n 1");
bufReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
for (int i = 0; i < 6 && bufReader != null; i++) {
bufReader.readLine();
}
bufReadLineString = bufReader.readLine();
if (bufReadLineString == null) {
process.destroy();
return false;
}
if (bufReadLineString.indexOf("timed out") > 0
|| bufReadLineString.length() < 17
|| bufReadLineString.indexOf("invalid") > 0) {
process.destroy();
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
process.destroy();
return true;
}
}
public void run() {
synchronized (this) {
if (isUsedIPAddress(ip)) {
arrayIPUsed.add(ip);
}
}
}
}
}
}