JAVA怎么用程序知道局域网IP,

lonefeifei 2010-04-04 11:31:17
我想编个局域网聊天软件,但每次得输入对方IP才能连接,我想问怎么才能直接就显示出对方IP,象飞鸽传书那样显示?SOCKET使的,但我搞不出来了,求高手相助.......
...全文
370 25 打赏 收藏 转发到动态 举报
写回复
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
hebeiliuzhao0909 2010-04-05
  • 打赏
  • 举报
回复
扫描得到ip
KaneTing 2010-04-05
  • 打赏
  • 举报
回复
SEXING!!
whvc2010 2010-04-05
  • 打赏
  • 举报
回复
UPUPUPUPUP!
tcm441224375 2010-04-05
  • 打赏
  • 举报
回复
不懂得先顶个,关注
lonefeifei 2010-04-05
  • 打赏
  • 举报
回复
各位高手,给点指点啊,我可以加分的,急
lonefeifei 2010-04-05
  • 打赏
  • 举报
回复
还是没实现啊,可以把本机的IP给服务端,可要问题是怎么把IP给对方呢,因为还是不知道对方IP,有高手没啊,拜托
lonefeifei 2010-04-05
  • 打赏
  • 举报
回复
嗯,谢谢大家先,目前得到的方法是:先得到自己IP,然后用FOR语句从1到255(除自己的)依次测试下,可得到在线的连接。
p2p81 2010-04-05
  • 打赏
  • 举报
回复
如果仅是局域网的话可以这样。
1.获得本机Ip和子网掩码,这样就可以知道你所在的局域网会有多少个ip
2.逐个ip ping一下就可以知道哪些ip存在或机器开着。
这样不就得到了局域网内的所有机器的ip了吗

hejp029 2010-04-05
  • 打赏
  • 举报
回复
帮忙顶下!!
tdgwj 2010-04-04
  • 打赏
  • 举报
回复
不懂socket路过。。
xsj__2006 2010-04-04
  • 打赏
  • 举报
回复
楼主的头像在那里搞的,有电影没?
lonefeifei 2010-04-04
  • 打赏
  • 举报
回复
能不能给个程序啊?
零起跑线 2010-04-04
  • 打赏
  • 举报
回复
纯顶一下,为了每天的十分可以用分
rumlee 2010-04-04
  • 打赏
  • 举报
回复
向局域网广播消息就可以了。。。。。
kimiH 2010-04-04
  • 打赏
  • 举报
回复
在同一局域网获IP,没实现过!
lonefeifei 2010-04-04
  • 打赏
  • 举报
回复
嗯,感谢大家先,明天散分吧
justlearn 2010-04-04
  • 打赏
  • 举报
回复
其实飞鸽应该也不是直接找到对方ip的,对方信息是对方传过来的。

你要实现的方法建议这样处理:
1:当你登陆聊天软件,就向指定端口发出信息,包含自己的用户名、工作组、主机名、IP等
2:开始监听指定端口以得到其他用户登录的信息,然后刷新用户列表
justlearn 2010-04-04
  • 打赏
  • 举报
回复
这是个扫描局域网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);

}

}

}

}

}

}
lonefeifei 2010-04-04
  • 打赏
  • 举报
回复
不是啊,我能得到自己IP,可对方IP不知道啊,怎么连接发消息啊?得先得到对方IP啊
justlearn 2010-04-04
  • 打赏
  • 举报
回复
InetAddress.getLocalHost().getHostAddress();
注意catch Exception
加载更多回复(5)
相关推荐

62,567

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告