Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send message to specific clients using java

Tags:

java

How can i send message from server to any specific client. I have the concept of how to do it like i have to make a list of all the clients connected to server and then by iterating each client i can send message but i will be thankful if any one can help me by code.I have searched many codes but i didn't get any considerable help from them Code shouldn't be GUI based. Thanks in advance.Sorry for my bad English. This is my code in which message is send to all clients but i want to send message to a client of my choice using clients ipaddress

Map<Integer, java.net.Socket> clients = new HashMap<Integer, java.net.Socket> ();
socket = serverSocket.accept();

// Add the socket to a HashMap
clients.put(socket.getPort(), socket);
for (Iterator<Integer> iter = clients.keySet().iterator(); iter.hasNext(); )
{
    int key = iter.next();

    java.net.Socket client = clients.get(key);

    // Sending the response back to the client.
    // Note: Ideally you want all these in a try/catch/finally block
    OutputStream os = client.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os);
    BufferedWriter bw = new BufferedWriter(osw);
    bw.write("Some message");
    bw.flush();
}
like image 900
zeeshan nisar Avatar asked Apr 25 '16 19:04

zeeshan nisar


3 Answers

What I would do is create a Client class:

class Client
{
   private String userName;
   private String ipAddress;
   private java.net.Socket socket = null;

   public Client (String userName, String ipAddress, java.net.Socket socket)
   {
      this.userName = userName;
      this.ipAddress = ipAddress;
      this.socket = socket;
   }

   public java.net.Socket getSocket()
   {
       return this.socket;
   }
}

Instead of adding just the socket and port number to the map, I would map the combination of the userName and ipAddres to a Client object.

socket = serverSocket.accept();

// get the username from the socket
// get the ipAddress from the socket
Client c = new Client(userName, ipAddress, socket);

// Add the client to a HashMap
clients.put(userName + ":" + ipAddress, c);

Now, you can send a message to a specific client based on the username and ipAddress:

public void sendToOneClient (String userName, String ipAddress, Map<String, Client> clients)
{
    Client c = clients.get(userName + ":" + ipAddress);

    java.net.Socket socket = c.getSocket();

    // Sending the response back to the client.
    // Note: Ideally you want all these in a try/catch/finally block
    OutputStream os = socket.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os);
    BufferedWriter bw = new BufferedWriter(osw);
    bw.write("Some message");
    bw.flush();
}
like image 124
Michael Markidis Avatar answered Oct 05 '22 19:10

Michael Markidis


I'd use Socket.getInetAddress() and compare the result to whatever you have the IPs you want to send to in. Personally, I'd use a String[] or ArrayList<String> for that. Here's an example:

ArrayList<String> addresses;
//TODO: Add things to 'addresses'

clients.put(socket.getPort(), socket);
for (Iterator<Integer> iter = clients.keySet().iterator(); iter.hasNext(); )
{
    int key = iter.next();

    java.net.Socket client = clients.get(key);

    //Checking to make sure it's a client we want to send to.
    if (addresses.contains(client.getInetAddress().toString()) {
        // Sending the response back to the client.
        // Note: Ideally you want all these in a try/catch/finally block
        OutputStream os = client.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write("Some message");
        bw.flush();
    }
}

Alternatively, you could store the sockets in your HashMap by the InetAddress instead.

like image 2
sockpuppetcow Avatar answered Oct 05 '22 19:10

sockpuppetcow


You can store a relationship between how you want to look up the client with the socket that they are on. The natural way to do this is with a map, like

Map<String, Socket> sockets = new HashMap<String,Socket>();
...
ServerSocket ss = ...;
Socket s = ss.accept();
String username = getUserName(s);
sockets.put(username, s);

Obviously, in this example here, the client would have to send his/her userName in a format which you expect to receive after making the Socket connection

like image 1
ControlAltDel Avatar answered Oct 02 '22 19:10

ControlAltDel