2011-08-09 3 views
2

私はすでにソケットとスイングを使って簡単なインスタントメッセージングアプリケーションをJavaで作成しました。現在は、パラメータとして渡されるホスト名(PC名)またはIPを解決することによって通信しています。しかし、WindowsユーザーID(つまり、Windowsにログオンするときに使用するユーザーID)をパラメータとしてメッセージを送信する方法はありますか?これはC#で簡単にできているようですが、Javaでどのように行うのですか?WindowsユーザーIDによるJavaのインスタントメッセージング?

+0

* "..それはWindowsユーザIDを持つメッセージを送信するようにする方法ですが..?" *未OS X、Debianの上、Ubuntuの、 Solaris .. –

+0

「これはC#で簡単にできますか?さらに悪い場合は、JNIを使​​用してネイティブ呼び出しをC#ルーチンに出力し、ユーザー名をIPに解決します。 – Dan

+0

同じユーザー名で2台のコンピュータにログインできます。どのIPを解決する必要がありますか? – Atreys

答えて

1

は、ユーザ名ははSystem.getPropertyを使用して行うことができるガイド:

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

+0

私はWindowsユーザーIDを取得する方法を知っています。問題はどのようにIPに変換するのですか? WindowsユーザーIDにpingを実行することはできません。また、あなたが渡す必要があるのは、あまりにもメッセージを送信したいリモートコンピュータのユーザーIDなので、上記のコマンドで自分のユーザーIDを取得することは役に立たないです。 –

+0

私は明らかにあなたの質問を誤解しました。私はあなたが既にやっているようにメッセージを送信するメソッドにパラメータとしてあなた自身のユーザ名を送ることを望んでいると思った。識別のため。 – Atreys

0

これは簡単にC#の

サードパーティ製アプリ(Winsentの送信ユーティリティで行うことと思われる - winsentmessenger.com/明らかにこれを行うことができます)。

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

問題のアプリケーションは、単純にNET SENDのラッパーです。

同じことができ、プロセスを直接呼び出すことができます。

から持ち上げソリューション: 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(); 
     } 
    } 
    } 
} 
+0

私はそれが悪い例だったと思います。私はWindowsのアラートボックスにメッセージを表示するだけなので、 "net send"または "msg.exe"(それ以降のWindowsバージョンでは)ルートに行きたいとは思わない。 Javaで受信したWindowsアラートメッセージを簡単に聞き取り、抽出する方法がない限り、これは私にとっては終わりのないことです。 –

+0

これが「C#で簡単に実行される」場所を示す良い例を提供するか、質問を更新してください。他の人が触れたように、「WindowsユーザーID」にメッセージを送信することは、本質的に*クロスプラットフォーム対応ではなく、Windowsのみのソリューションです。完全なインスタントメッセージングクライアントが必要な場合は、既存のオープンソースのインスタントメッセージングソリューションを使用してください。ジャバー、ランデブーなど – Dan

関連する問題