我想编个局域网聊天软件,但每次得输入对方IP才能连接,我想问怎么才能直接就显示出对方IP,象飞鸽传书那样显示?SOCKET使的,但我搞不出来了,求高手相助.......

解决方案 »

  1.   

    最简单的 在你的客户端程序里面 写一段代码 就是发送消息的时候 把自己的ip信息和message一起 发给消息的接收方
      

  2.   

    下面是获得本机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]);
        }
      

  3.   

    InetAddress.getLocalHost().getHostAddress();
    注意catch Exception
      

  4.   

    不是啊,我能得到自己IP,可对方IP不知道啊,怎么连接发消息啊?得先得到对方IP啊
      

  5.   

    这是个扫描局域网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);                                    }                             }                      }               }        } } 
      

  6.   

    其实飞鸽应该也不是直接找到对方ip的,对方信息是对方传过来的。你要实现的方法建议这样处理:
    1:当你登陆聊天软件,就向指定端口发出信息,包含自己的用户名、工作组、主机名、IP等
    2:开始监听指定端口以得到其他用户登录的信息,然后刷新用户列表
      

  7.   

    还是没实现啊,可以把本机的IP给服务端,可要问题是怎么把IP给对方呢,因为还是不知道对方IP,有高手没啊,拜托
      

  8.   

    如果仅是局域网的话可以这样。
    1.获得本机Ip和子网掩码,这样就可以知道你所在的局域网会有多少个ip
    2.逐个ip ping一下就可以知道哪些ip存在或机器开着。
    这样不就得到了局域网内的所有机器的ip了吗
      

  9.   

    嗯,谢谢大家先,目前得到的方法是:先得到自己IP,然后用FOR语句从1到255(除自己的)依次测试下,可得到在线的连接。