Results 1 to 11 of 11
  1. #1
    sana'a is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default server to serve multi clients

    Hi,
    i want to implement a client /server codes in the following fashion :
    the client A should enter the IP of of the server and clients B and C enters the same IP,so the three clients can communication with each other through the server.
    The server could omit any client from the service.



    any good tutorials or examples?
    I appreciate any help.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  3. #3
    sana'a is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: server to serve multi clients

    thanks so much,but still have one problem
    how can i remove a specific client from the server by entering the client IP?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: server to serve multi clients

    Quote Originally Posted by sana'a View Post
    thanks so much,but still have one problem
    how can i remove a specific client from the server by entering the client IP?
    That question is so far out of context as to be unanswerable. If you wish your question answered, then I recommend providing more context

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: server to serve multi clients

    how can i remove a specific client from the server by entering the client IP?
    If you have code on the server that can receive the IP to be removed from some source: GUI or administrative client,
    and if you have a list of clients' with their status and data such as their IP,
    then you could have a method that searches the list and removes the client based on their IP.

  6. #6
    sana'a is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: server to serve multi clients

    and if you have a list of clients' with their status and data such as their IP,
    then you could have a method that searches the list and removes the client based on their IP.
    and how to create such a list and close the connection with a client from that list?
    please, i need some code examples ,i appreciate that.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: server to serve multi clients

    Post your code so we can see where and how you are getting and saving the necessary information about the connections

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: server to serve multi clients

    Quote Originally Posted by doWhile View Post
    That question is so far out of context as to be unanswerable. If you wish your question answered, then I recommend providing more context
    I guess I stand corrected.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: server to serve multi clients

    Often its hard to understand what OPs are asking. I always try to come up with some approach to get the OP to define what he's doing. Hoping eventually he'll give enough info that some one will understand what the problem is.

  10. #10
    sana'a is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: server to serve multi clients

    this is the server code.i didn't write all the code but i modified it
    Java Code:
    
    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();
                            
                            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);
                     }
                 }
             }
    
    }
    so i can get the IP of all the clients in the Begin thread by the getInetAddress() method,(that's how i can create a list of the clients define them by IP when i switch to GUI)
    Let us say I have a button (Remove),when i click Remove an input dialog would appear and i should write the IP of the client that i want to remove him
    from the server.How to close the connection with that client without affect the other clients communication?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: server to serve multi clients

    Change the code so that the socket is saved in a list before passing the socket to the Begin class.

Similar Threads

  1. Server/clients connection periodically
    By akis3110 in forum New To Java
    Replies: 4
    Last Post: 06-28-2011, 03:03 PM
  2. How to connect n clients to a server?
    By chyrl in forum Networking
    Replies: 21
    Last Post: 07-25-2010, 04:01 PM
  3. multi clients connect to udp server
    By bachma7 in forum Networking
    Replies: 5
    Last Post: 12-12-2008, 08:56 PM
  4. SSL Server with authentication of clients
    By zoltan in forum Networking
    Replies: 0
    Last Post: 10-14-2008, 11:27 PM
  5. blocked between server and clients
    By ibtehal in forum Networking
    Replies: 6
    Last Post: 07-17-2008, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •