954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

send the received message to all clients

hi there,
i want to implement a server that receives voice packets from many clients then send these packets to all the clients so that the clients are communicated to each other through the server.
how i can send the received packets to all the clients?
any good tutorials or examples?

i appreciate any help.

ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Have a list of the clients.
Go through the list and send to each client.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

but i want to send the packet to all clients almost in the same time
with list the sending would be sequentially

ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

What kind of computer do have that you can do many things at the same time?
Are you looking for something like broadcast with a Datagram?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
but i want to send the packet to all clients almost in the same time with list the sending would be sequentially


yup NormR1 is right you will never be able to send to every client at the exact same moment, the list might send sequentially but it will have less then a few milliseconds delay... so it can be considered immediate...

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

thanks NormR1 and cOrRuPtG3n3t!x
but can you tell me how to create such a list?
and how to send the packets for each IP in the list?

ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

First you'd need to create a class to hold the status and info for a connection. It should contain whatever is needed to send a message to the client on that connection.
Given that you can add instances of that class to an ArrayList.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
thanks NormR1 and cOrRuPtG3n3t!x but can you tell me how to create such a list? and how to send the packets for each IP in the list?


how is your source looking? because i would rather make a suggestion after i see your code, because i guess the best solution will be the easiest?

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

i found this code and modified it to accept multi clients , but this way the all the clients receive the server packets not other clients' packets

package sender;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;



public class sender {
        ServerSocket MyService;
        
        BufferedInputStream input;
        TargetDataLine targetDataLine;

        BufferedOutputStream out;
          ByteArrayOutputStream byteArrayOutputStream;
          AudioFormat audioFormat;
          

          SourceDataLine sourceDataLine;
         byte tempBuffer[] = new byte[10000];

         sender() throws LineUnavailableException{
            try {
                    audioFormat = getAudioFormat();
                    DataLine.Info dataLineInfo =  new DataLine.Info( SourceDataLine.class,audioFormat);
                    sourceDataLine = (SourceDataLine)
                AudioSystem.getLine(dataLineInfo);
                sourceDataLine.open(audioFormat);
                sourceDataLine.start();
                        MyService = new ServerSocket(500);
                       
                } catch (IOException e) {

                        e.printStackTrace();
                }
            try{
                while(true){
                   new Begin(MyService.accept()).start();
                    MyService.close();
                }
            }
            catch(Exception e){
                System.out.println(e);
            }

        }
         private AudioFormat getAudioFormat(){
                    float sampleRate = 8000.0F;
                    int sampleSizeInBits = 16;
                    int channels = 1;
                    boolean signed = true;
                    boolean bigEndian = false;
                    return new AudioFormat(
                                      sampleRate,
                                      sampleSizeInBits,
                                      channels,
                                      signed,
                                      bigEndian);
                  }
        public static void main(String s[]) throws LineUnavailableException{
                 sender s2=new sender();
        }


        private void captureAudio() {
                try {

                        Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
                        System.out.println("Available mixers:");
                        for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
                                System.out.println(mixerInfo[cnt].getName());
                        }
                        audioFormat = getAudioFormat();

                        DataLine.Info dataLineInfo = new DataLine.Info(
                                        TargetDataLine.class, audioFormat);

                        Mixer mixer = AudioSystem.getMixer(mixerInfo[2]);

                        targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);

                        targetDataLine.open(audioFormat);
                        targetDataLine.start();

                        Thread captureThread = new CaptureThread();
                        captureThread.start();
                } catch (Exception e) {
                        System.out.println(e);
                        System.exit(0);
                }
        }

        class CaptureThread extends Thread {

                byte tempBuffer[] = new byte[10000];

                public void run() {
                        try {
                                while (true) {
                                        int cnt = targetDataLine.read(tempBuffer, 0,
                                                        tempBuffer.length);
                                        out.write(tempBuffer);
                                }

                        } catch (Exception e) {
                                System.out.println(e);
                                System.exit(0);
                        }
                }
        }
         class Begin extends Thread{
             private Socket clientsocket;
             public Begin(Socket sock){
                 this.clientsocket=sock;
                 System.out.println(clientsocket.getInetAddress());
             }
             public void run(){
                 try{
                 captureAudio();
                        input = new BufferedInputStream(clientsocket.getInputStream());
                        out=new BufferedOutputStream(clientsocket.getOutputStream());

                        while(input.read(tempBuffer)!=-1){
                                sourceDataLine.write(tempBuffer,0,10000);
                        }
                 }
                 catch(Exception e){
                     System.out.println(e);
                     System.exit(0);
                 }
             }
         }

}

any suggestion?

ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

When you have a message to send to all the clients, how does the code you posted do it?
The source of the message should not control who the message is sent to.
The one consideration would be not to send a message to the client that sent it.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
When you have a message to send to all the clients, how does the code you posted do it?

i meant the captured voice from the server PC is send to all clients,but i need to send the data the reached to the server from one clients to all other clients .
according to cOrRuPtG3n3t!x,this is possible by create a list of the clients, want to know how to create such a list?i can get the IP of each client by getInetAddress() method ,then what's next?

ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

When a client connects to the server socket, the server gets a socket that connects to that client. You can use that socket to send data to the client.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

i know it's a stupid idea but i should ask ,after all this is my first java network code!
if i write this line

out.write(tempBuffer,0,tempBuffer.length);

inside the while loop of the Begins thread,does it work?

ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
i meant the captured voice from the server PC is send to all clients,but i need to send the data the reached to the server from one clients to all other clients . according to cOrRuPtG3n3t!x,this is possible by create a list of the clients, want to know how to create such a list?i can get the IP of each client by getInetAddress() method ,then what's next?

take a look at my other post here : http://www.daniweb.com/software-development/java/threads/401632 see how there are arrays for each socket as the client connects, and also see how i switch between each client in the server:

static int[] clientid = new int[100];
    static SSLSocket[] clientsocket = new SSLSocket[100];
....
 while (true) {
            try {
                // Accept incoming connections. 
 
                sslSocket = (SSLSocket) sslServerSocket.accept();
 
                // accept() will block until a client connects to the server. 
                // If execution reaches this point, then it means that a client 
                // socket has been accepted. 
 
                // For each client, we will start a service thread to 
                // service the client requests. This is to demonstrate a 
                // multithreaded server.
 
                // Start a service thread 
                jBotServer ct = new jBotServer(sslSocket, id++);
                ct.start();
 
                //save information of all clients in array;
                clientid[counter] = id - 1;
                clientsocket[counter] = sslSocket;
//then calls thread
......
     for (int i = 0; i < clientsocket.length; i++) {
                    if (clientid[i] == Integer.parseInt(clientCommand)) {
                        in = new BufferedReader(new InputStreamReader(clientsocket[i].getInputStream()));
                        out = new PrintWriter(new OutputStreamWriter(clientsocket[i].getOutputStream()));
                        System.out.println("Enter a command");
                        isr = new InputStreamReader(System.in);
                        br = new BufferedReader(isr);
                        clientCommand = br.readLine();

you could do this inside your server to send every message a client sends to all other clients etc

It wouldnt make sense to store the IP addresses on their own because when you want to connect it will re establish a connection. rather store an array of Sockets that have the already connected data

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

You need to get a design for the program you are trying to write before trying to write the code.
One approach is make a list of the events that you expect to happen and what the code will do when each event happens. The more details the better. As the list grows, you will see requirements that earlier steps should have done and you'll go back and add in extra steps at the point in the processing where they need to be done.

When you get the steps designed for the program, then you can come back to this code and copy and paste bits and pieces from it into your program.

It may be that the code you posted does most of the steps that need to be done and you can make a few changes to it to make the program the way you want it to be.

Your choice.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

sorry to be late but i have a suck internet connection
so,i modified the code like this:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;



public class sender {
        ServerSocket MyService;
        Socket clientSocket = null;
        Begin t[]=new Begin[5];
        

         sender() throws LineUnavailableException{
            try {
                    
                        MyService = new ServerSocket(500);
                        
                } catch (IOException e) {

                        e.printStackTrace();
                }
            try{
                while(true){
                    clientSocket=MyService.accept();
                    for(int i=0;i<=4;i++){
                        if(t[i]==null){
                            (t[i]=new Begin(clientSocket,t)).start();
                            break;
                        }
                    }
                   //new Begin(MyService.accept()).start();
                    //MyService.close();
                }
            }
            catch(Exception e){
                System.out.println(e);
            }

        }
        
        public static void main(String s[]) throws LineUnavailableException{
                 sender s2=new sender();
        }


      
        
         class Begin extends Thread{
             Socket clientsocket;
             BufferedInputStream input;
             BufferedOutputStream out;
             Begin t[];
             byte tempBuffer[]=new byte[10000];

             public Begin(Socket sock,Begin[] t){
                 this.clientsocket=sock;
                 this.t=t;
                 System.out.println(clientsocket.getInetAddress());
             }
             public void run(){
                 try{
                 
                        input = new BufferedInputStream(clientsocket.getInputStream());
                        out=new BufferedOutputStream(clientsocket.getOutputStream());

                        
                        input.read(tempBuffer);
                        while(true){

                            for(int i=0;i<=4;i++)
                                if(t[i]!=null)
                                    t[i].out.write(tempBuffer,0,tempBuffer.length);
                        }
                 }
                 catch(Exception e){
                     System.out.println(e);
                     System.exit(0);
                 }
             }
         }

}


but still can't make it work!

ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Can you explain your design for the program?
For example why do you have a loop after the accept() method returns with a connection to a client?

BTW Your code does NOT follow coding standards. Upper case for classnames, lowercase for variable names. Your code is confusing to read now.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

sorry to be late but i have a suck internet connection so,i modified the code like this:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;



public class sender {
        ServerSocket MyService;
        Socket clientSocket = null;
        Begin t[]=new Begin[5];
        

         sender() throws LineUnavailableException{
            try {
                    
                        MyService = new ServerSocket(500);
                        
                } catch (IOException e) {

                        e.printStackTrace();
                }
            try{
                while(true){
                    clientSocket=MyService.accept();
                    for(int i=0;i<=4;i++){
                        if(t[i]==null){
                            (t[i]=new Begin(clientSocket,t)).start();
                            break;
                        }
                    }
                   //new Begin(MyService.accept()).start();
                    //MyService.close();
                }
            }
            catch(Exception e){
                System.out.println(e);
            }

        }
        
        public static void main(String s[]) throws LineUnavailableException{
                 sender s2=new sender();
        }


      
        
         class Begin extends Thread{
             Socket clientsocket;
             BufferedInputStream input;
             BufferedOutputStream out;
             Begin t[];
             byte tempBuffer[]=new byte[10000];

             public Begin(Socket sock,Begin[] t){
                 this.clientsocket=sock;
                 this.t=t;
                 System.out.println(clientsocket.getInetAddress());
             }
             public void run(){
                 try{
                 
                        input = new BufferedInputStream(clientsocket.getInputStream());
                        out=new BufferedOutputStream(clientsocket.getOutputStream());

                        
                        input.read(tempBuffer);
                        while(true){

                            for(int i=0;i<=4;i++)
                                if(t[i]!=null)
                                    t[i].out.write(tempBuffer,0,tempBuffer.length);
                        }
                 }
                 catch(Exception e){
                     System.out.println(e);
                     System.exit(0);
                 }
             }
         }

}

but still can't make it work!


what exactly cant you make work?

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You