Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Reverse roles of client/server communication

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Reverse roles of client/server communication

    I am writing a Java alert program that requires each client PC to listen for a notification from the server rather than having each PC call the server every few seconds. I am trying to make the program as quiet as possible so that only the server send outs a broadcast to all network PCs when an approved user is ready to send the alert. The server will not be constantly broadcasting over the network. The issue I am having is getting the code to compile. Since there are multiple servers (client PCs in this case) I can't input an IP address because there are well over 100 PCs that need to run the instructions when the notification goes out.

    Any suggestions with what needs to change would be appreciated.

    package alert.bat;
     
    import java.io.*;
    import java.net.*;
     
    public class Alert {
     
    	public static void main(String args[]) {
     
    		try {
    			DatagramSocket datsock = new DatagramSocket(444);
    			DataOutputStream output = new DataOutputStream (datsock.getOutputStream());
    			output.writeUTF("Broadcast Alert!!!");
    			output.flush();
    			output.close();
    			datsock.close();
    		}
     
    		catch(Exception e) {
    			System.out.println(e);
    		}
    	}
     
    }
    Last edited by warcode; March 21st, 2020 at 08:34 AM.

  2. #2
    Junior Member
    Join Date
    Mar 2020
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse roles of client/server communication

    If I understand your problem statement correctly you probably need something like the observer pattern.
    https://sourcemaking.com/design_patterns/observer

    Basically you would have the client PCs be observers of the main server and when the main server needs to send out a notification it iterates through the list of observers and updates them.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,337
    Thanks
    53
    Thanked 2,486 Times in 2,437 Posts

    Default Re: Reverse roles of client/server communication

    Can the client PCs register with the server and give it their IP address? Then the clients each act as servers allowing the server pc to connect to each client in turn.

    Also look at MulticastSocket
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. server/client communication problem
    By ark in forum Java Networking
    Replies: 6
    Last Post: December 8th, 2018, 09:50 AM
  2. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  3. server/client communication problem
    By perl0101 in forum Java Networking
    Replies: 8
    Last Post: May 24th, 2011, 01:58 PM
  4. Replies: 0
    Last Post: January 22nd, 2011, 11:52 AM
  5. client server communication
    By Brt93yoda in forum Java Theory & Questions
    Replies: 4
    Last Post: September 2nd, 2010, 04:49 PM

Tags for this Thread