2

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?

Mark Logan
  • 203
  • 5
  • 19
  • *"..is there a way to make it send a message with the Windows user ID..?"* Not on OS X, Debian, Ubuntu, Solaris.. – Andrew Thompson Aug 09 '11 at 13:58
  • How is this "easily done in C#"? If worse-comes-to-worse you could always look at using JNI to make the native calls out to a C# routine that resolves username to an IP. – Dan Aug 09 '11 at 13:58
  • I can log in to two computers with the same user name. Which IP should be resolved? – Atreys Aug 09 '11 at 14:03
  • A 3rd party app (Winsent's Sent utility - http://www.winsentmessenger.com/sent/) apparently can do this. You can send a message to someone via PC name or Windows ID. When I have my extra laptop with me, I'll test sending a message when the same user ID logged onto two machines. – Mark Logan Aug 09 '11 at 14:19
  • Are you sure you want to do this completely peer to peer? A lot of things, including firewall's et al will do their best to block it. You might have better luck using some sort of SIP server. – mezmo Aug 09 '11 at 15:11

2 Answers2

1

Getting the username can be done using System.getProperty:

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

Atreys
  • 3,741
  • 1
  • 17
  • 27
  • I know how to get the Windows user ID. Problem is how do I translate it to an IP? You cannot ping a Windows user ID. Also, getting your own user ID via the command above is sort of useless, since what you need to pass is the user ID of the remote computer you want to send a message too. – Mark Logan Aug 09 '11 at 13:52
  • I apparently misunderstood your question. I thought you wanted to send your own username as a parameter to a method which would then send the message as you have already been doing. For identification purposes. – Atreys Aug 09 '11 at 14:02
0

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();
         }
    }
  }
}
Community
  • 1
  • 1
Dan
  • 550
  • 4
  • 3
  • I guess that was a bad example. I don't want to go the "net send" or "msg.exe" (in later Windows versions) route since they just show the message in a Windows alert box. Unless there's an easy way to listen and extract Windows alert messages received in Java, this will be a dead end for me. – Mark Logan Aug 09 '11 at 15:09
  • Then provide a better example showing where this is "easily done in C#", or update your question. As others have mentioned, sending a message to a "Windows user ID" is inherently *not* cross-platform compatible, it is a Windows only solution. If you want a full blown instant messaging client, then use an existing open source instant messaging solution; Jabber, Rendezvous etc – Dan Aug 09 '11 at 16:10