Java 中通过 Windows 用户 ID 进行即时消息传递?

发布于 2024-11-28 19:28:01 字数 170 浏览 1 评论 0原文

我已经使用 Sockets 和 Swing 用 Ja​​va 创建了简单的即时消息应用程序。现在它通过解析作为参数传递的主机名(PC 名称)或 IP 进行通信。但有没有办法让它发送一个以Windows用户ID(即你登录Windows时使用的用户ID)为参数的消息呢?这在 C# 中似乎很容易完成,但在 Java 中如何做到呢?

I have already created simple instant messaging application in Java using Sockets and Swing. Right now it's communicating by resolving the hostname (PC name) or IP that's passes as a parameter. But is there a way to make it send a message with the Windows user ID (i.e. the user ID you use when you logon to Windows) as the parameter? This seems be easily done in C#, but how do I do it in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我 2024-12-05 19:28:02

可以使用 System.getProperty 来获取用户名:

String name = System.getProperty("user.name");

Getting the username can be done using System.getProperty:

String name = System.getProperty("user.name");

如梦亦如幻 2024-12-05 19:28:02

这似乎在 C# 中很容易完成

第 3 方应用程序(Winsent 的发送实用程序 -winsentmessenger.com/sent)显然可以做到这一点。

http://www.winsentmessenger.com/netsend/

相关应用程序只是一个包装器网发送。

您可以执行相同的操作,并直接调用该流程。

解决方案来自:
http://members.iinet.net.au/~alw1746/ awhome/freeware/WinPopup_java.txt

/*
 WinPopup: send message to PC(s) on a Windows network from a Java program (like winpopup or net send).
 Usage:
   java WinPopup "user1,user2,..." "message"
   eg. java WinPopup "peter" "where are you?" or java WinPopup 192.168.14.20 "Hello"
 Build:
   javac WinPopup.java

 Alex Wong, Feb 2001
*/
import java.util.*;
import java.text.*;
import java.io.*;

public class WinPopup {

  public static void main(String args[]) throws Exception {
    String status;

    if (args.length < 2) {
      System.out.println("Usage: java WinPopup \"user1,user2,...\" \"msg\"");
      System.exit(1);
    }
    if (args[0].length() < 1) {
      System.out.println("User not found");
      System.exit(1);
    }
    if (args[1].length() < 1) {
      System.out.println("Message not found");
      System.exit(1);
    }
    WinPopup popup=new WinPopup();
    status=popup.alert(args[0],args[1]);
    if (!status.equals("OK"))
      System.out.println(status);
  }
  
  public String alert(String users,String msg) {
  //loop thru list of users and net send the msg.
    String buf,userList,user;
    StringBuffer popup;
    int ulen;

    try {
      if (users.length() < 1)
        throw new Exception("User list not found.");
      if (msg.length() < 1)
        throw new Exception("Message not found.");

      popup=new StringBuffer();
      StringTokenizer st=new StringTokenizer(users,",");
      while (st.hasMoreTokens()) {
        buf=st.nextToken();
        popup.append(buf).append(",");
      }
      if (popup.length() > 0) {
        popup=popup.deleteCharAt(popup.length()-1);
        userList=popup.toString();
        ulen=userList.length();
        for (int start=0,fin=0; fin <= ulen; fin++) {
          if ((fin==ulen && fin > start) || userList.charAt(fin)==',') {
            user=userList.substring(start,fin);
            dosCmd("net send "+user+" \""+msg+"\"");
            fin++;
            start=fin;
          }
        }
      }
      return "OK";
    }
    catch (Exception e) {
      return e.toString();
    }
  }

  public void dosCmd(String cmd) {
  //spawns a DOS process to run the net send command.
    java.lang.Runtime rt;
    Process proc;

    try {
      rt=java.lang.Runtime.getRuntime();
      proc=rt.exec("c:\\winnt\\system32\\cmd.exe /C "+cmd);

      StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
      StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
      errorGobbler.start();
      outputGobbler.start();
      int exitVal=proc.waitFor();
    }
    catch (Exception e1) {
      System.out.println("dosCmd exception.");
      System.out.println(e1.toString());
    }
  }
  
  class StreamGobbler extends Thread {
  //eat all stderr and stdout output.
    InputStream is;
    String type;
    
    StreamGobbler(InputStream is, String type) {
      this.is = is;
      this.type = type;
    }
    
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
              ;
         } catch (IOException ioe) {
            ioe.printStackTrace();
         }
    }
  }
}

This seems be easily done in C#

A 3rd party app (Winsent's Sent utility - winsentmessenger.com/sent) apparently can do this.

http://www.winsentmessenger.com/netsend/

The application in question is simply a wrapper around net send.

You could do the same, and invoke the process directly.

A solution lifted from:
http://members.iinet.net.au/~alw1746/awhome/freeware/WinPopup_java.txt

/*
 WinPopup: send message to PC(s) on a Windows network from a Java program (like winpopup or net send).
 Usage:
   java WinPopup "user1,user2,..." "message"
   eg. java WinPopup "peter" "where are you?" or java WinPopup 192.168.14.20 "Hello"
 Build:
   javac WinPopup.java

 Alex Wong, Feb 2001
*/
import java.util.*;
import java.text.*;
import java.io.*;

public class WinPopup {

  public static void main(String args[]) throws Exception {
    String status;

    if (args.length < 2) {
      System.out.println("Usage: java WinPopup \"user1,user2,...\" \"msg\"");
      System.exit(1);
    }
    if (args[0].length() < 1) {
      System.out.println("User not found");
      System.exit(1);
    }
    if (args[1].length() < 1) {
      System.out.println("Message not found");
      System.exit(1);
    }
    WinPopup popup=new WinPopup();
    status=popup.alert(args[0],args[1]);
    if (!status.equals("OK"))
      System.out.println(status);
  }
  
  public String alert(String users,String msg) {
  //loop thru list of users and net send the msg.
    String buf,userList,user;
    StringBuffer popup;
    int ulen;

    try {
      if (users.length() < 1)
        throw new Exception("User list not found.");
      if (msg.length() < 1)
        throw new Exception("Message not found.");

      popup=new StringBuffer();
      StringTokenizer st=new StringTokenizer(users,",");
      while (st.hasMoreTokens()) {
        buf=st.nextToken();
        popup.append(buf).append(",");
      }
      if (popup.length() > 0) {
        popup=popup.deleteCharAt(popup.length()-1);
        userList=popup.toString();
        ulen=userList.length();
        for (int start=0,fin=0; fin <= ulen; fin++) {
          if ((fin==ulen && fin > start) || userList.charAt(fin)==',') {
            user=userList.substring(start,fin);
            dosCmd("net send "+user+" \""+msg+"\"");
            fin++;
            start=fin;
          }
        }
      }
      return "OK";
    }
    catch (Exception e) {
      return e.toString();
    }
  }

  public void dosCmd(String cmd) {
  //spawns a DOS process to run the net send command.
    java.lang.Runtime rt;
    Process proc;

    try {
      rt=java.lang.Runtime.getRuntime();
      proc=rt.exec("c:\\winnt\\system32\\cmd.exe /C "+cmd);

      StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
      StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
      errorGobbler.start();
      outputGobbler.start();
      int exitVal=proc.waitFor();
    }
    catch (Exception e1) {
      System.out.println("dosCmd exception.");
      System.out.println(e1.toString());
    }
  }
  
  class StreamGobbler extends Thread {
  //eat all stderr and stdout output.
    InputStream is;
    String type;
    
    StreamGobbler(InputStream is, String type) {
      this.is = is;
      this.type = type;
    }
    
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
              ;
         } catch (IOException ioe) {
            ioe.printStackTrace();
         }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文