Antony Joslin
Antony Joslin

Reputation: 319

How to send message to multiple clients in a list from server

I had tried the following code as server to send message to the clients whose ip is in List mSession

for (MSession mSession : mSessions)
{
   System.out.println("Inside for each");
   System.out.println("IP TEST : " + mSession.getRemote_Addr());
   ServerSocket srvr = new ServerSocket(1324, 5, InetAddress.getByName(mSession.getRemote_Addr()));
   Socket skt = srvr.accept();
   System.out.println("IP1:" + InetAddress.getByName(mSession.getRemote_Host()));
   System.out.println("IP2:" + skt.getInetAddress().getHostAddress());           
   System.out.print("Server has connected!\n");
   PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
   System.out.print("Sending string: '" + data + "'\n");
   out.print(data);
   out.close();
   skt.close();
   srvr.close();                                         
}

The client side code:

InetAddress ip = InetAddress.getByName(host[0]);
System.out.println("IP SERVER : " + ip);
Socket skt = new Socket(ip, 1324);// Ip address of server
in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
System.out.print("Received string: ");

while (!in.ready()) {}
data = in.readLine();
System.out.println(in.readLine());

System.out.print("'\n");
Popupapp app = new Popupapp();
app.fdis(data);
in.close();
skt.close();

I am getting bindexception cannot assign requested address : JVM_Bind I have tried other ports but the exception remains the same I had tried ports 8080, 8089,8086,8009,1234,8242,8006

Please help me to fix this.

I had tried on this simple program and it is showing same exception

public class ServerSide extends Thread{

public void run()
{
    while(true)
    {
        String data = "Recieved new notification";
        try {

             ServerSocket srvr = new ServerSocket(1234, 5, InetAddress.getByName("192.168.168.40"));
             Socket skt = srvr.accept();

             skt.getInputStream();

             System.out.print("Server has connected!\n");
             PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
             System.out.print("Sending string: '" + data + "'\n");
             out.print(data);
             out.close();
             skt.close();
             srvr.close();
             Thread.sleep(500);



      }

   catch(Exception e) {
      System.out.print("Whoops! It didn't work!\n" + e);
      try {
        Thread.sleep(1000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
   }

}
}

Upvotes: 1

Views: 549

Answers (1)

Am_I_Helpful
Am_I_Helpful

Reputation: 19178

The reason is that you're creating server socket inside the foreach loop. So, after the first time, when the first client is processed and the serversocket is created for the first time, this code again tries to create another serversocket on the same port in the next client processing, thereby resulting in the bind-exception.

The solution is to create a separate serversocket before the loop, which would keep on accepting clients inside the foreach loop. It is shown below :

ServerSocket srvr = new ServerSocket(1324, 5, InetAddress.getByName(mSession.getRemote_Addr()));
for (MSession mSession : mSessions)
              {
                  System.out.println("Inside for each");
                  System.out.println("IP TEST : " + mSession.getRemote_Addr());
                  // start accepting client-requests                     
                  Socket skt = srvr.accept();
                  System.out.println("IP1:" + InetAddress.getByName(mSession.getRemote_Host()));
                  System.out.println("IP2:" + skt.getInetAddress().getHostAddress());            
                  System.out.print("Server has connected!\n");
                  PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
                  System.out.print("Sending string: '" + data + "'\n");
                  out.print(data);
                  out.close();
                  skt.close();                                     
              }
                // finally close the server-socket!                
                  srvr.close(); 

Based on your EDIT :

You're again creating your ServerSocket inside the loop! DON'T!!!* Do it in the constructor(or in some another method).

public class ServerSide extends Thread{

public ServerSide(){
ServerSocket srvr = new ServerSocket(1234, 5, InetAddress.getByName("192.168.168.40"));
}
public void run()
{
while(true)
{
    String data = "Recieved new notification";
    try {             
         Socket skt = srvr.accept();
         skt.getInputStream();
         System.out.print("Server has connected!\n");
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending string: '" + data + "'\n");
         out.print(data);
         out.close();
         skt.close();
         srvr.close();
         Thread.sleep(500);
  }
catch(Exception e) {
  System.out.print("Whoops! It didn't work!\n" + e);
  try {
    Thread.sleep(1000);
} catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
}

Now, call it from your main-method on the server-side like :

new ServerSide().start();

Upvotes: 2

Related Questions