Results 1 to 4 of 4
  1. #1
    jairoh_ is offline Member
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Server w/ multiple clients

    i'm currently new to socket programming. and i would appreciate if someone tries to help me.
    here is the case, i'm successful to make this chat program only with 1 to 1. i mean 1 server to i client only, back and forth. and i'm not contented, i wanna learn how 1 server to multiple clients or back and forth works. i've read that i'll use thread for the server to wait for client to connect and another thread
    that start the streams and chatting when the connection has established. i've seen some codes about this one, but it only confuses me, so i try to create my own.
    i think the problem is on the server. here is the code of the server.
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package jairoh.core.java.server;
    
    import jairoh.core.java.ui.ChatUI;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    import javax.swing.SwingUtilities;
    
    /**
     *
     * @author jairoh
     */
    public class Server {
        
        private ServerSocket ss;
        private Socket connection;
        private Scanner in;
        private Writer output;
        private ChatUI ui;
        
        public Server () {
            ui = new ChatUI( "SERVER - END");
            
            
            
            try { 
                
                ss = new ServerSocket( 1234, 100 );
                connection = ss.accept();
    
                in = new Scanner( new InputStreamReader ( connection.getInputStream() ) ); //read stream inputs
                output = new OutputStreamWriter( connection.getOutputStream() ); //sends stream outputs
                
                
                new Connect().start();
                new Streams().start();
              
                 
            } catch ( IOException e ) {
                System.out.println( e.getMessage() );
            }
            
        }
        
        private class Connect extends Thread {
            public void run () {
                while ( true ) {
                    try {
                        connection = ss.accept();
                        Thread.sleep( 500 );
                        
                    } catch ( IOException e ) {
                        System.out.println( e.getMessage() );
                    } catch ( InterruptedException e ) {
                        System.out.println( e.getMessage() );
                    }
                }
            }
        }
        
        private class Streams extends Thread {
            public void run () {
                while ( true ) {
                    try {
                        Thread.sleep( 500 );
                        in = new Scanner( new InputStreamReader ( connection.getInputStream() ) ); //read stream inputs
                        output = new OutputStreamWriter( connection.getOutputStream() ); //sends stream outputs
    
                        ui.output = output;
    
                        //read input streams and append to textarea; infinite loop until connection was lost
                        String msg;
                        while ( ( msg = in.nextLine() ) != null ) {
                            ui.appendText( msg );
                        }
    
                        
                    } catch ( IOException e ) {
                        System.out.println( e.getMessage() );
                    } catch ( InterruptedException e ) {
                        System.out.println( e.getMessage() );
                    }
                }
            }
        }
        
        
        public static void main ( String args [] ) throws IOException {
            new Server();
        }
    }

  2. #2
    jairoh_ is offline Member
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Re: Server w/ multiple clients

    anyone can help. . . .

  3. #3
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: Server w/ multiple clients

    I'm not too sure what you're talking about being confused about mutliple thread, you've just created multiple thread in your code, but you're making things unnecessarily complicated. Here's a simple bit of code.

    Java Code:
    public class Server implements Runnable
    {
    
          Thread thisThread;
    
          private Server()
          {
               thisThread = new Thread(this);
               thisThread.start();
          }
    
          public void run()
          {
    
                try
                {
                      ServerSocket serverSocket = new ServerSocket(11000);
    
                      Socket incomingSocket = null;
    
                      while ((incomingSocket = serverSocket.accept()) != null)
                      {
                            System.out.println("Woah! A socket was accepted!");
                      }
    
                 }
                 catch(Exception e)
                 {
                      System.err.println("This shouldn't be caught this way, but for it is for demonstration purposes only!");
                 }
          }
    
          public static void main(String args[])
          {
                 new Server();
          }
    }
    Last edited by sevoxx; 05-08-2013 at 05:00 PM.

  4. #4
    SlickJava is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Server w/ multiple clients

    Maybe something like this might help:

    Java Code:
    package com.jason.guessSocket1;
    
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Random;
    
    public class ServerTest
    {
    
        private ObjectInputStream input;
    
        private ObjectOutputStream output;
    
        private Socket connection;
    
        private ServerSocket server;
    
        private int counter = 0;
    
        public void startRunning()
        {
            try
            {
                server = new ServerSocket(53);
                while (true)
                {
                    waitForConnection();
                }
            }
            catch (Exception e)
            {
    
            }
        }
    
        public void waitForConnection() throws IOException
        {
            System.out.println("Waiting for connection...\n");
            connection = server.accept();
            new Thread(new Worker()).start();
            counter++;
            System.out.println("Connection " + counter + " recieved from: "
                            + connection.getInetAddress().getHostName());
        }
    
        public class Worker implements Runnable //Put what ever you want the client to do in this thread
        {
    
           
    
            public void setUpStreams() throws IOException
            {
                output = new ObjectOutputStream(connection.getOutputStream());
                output.flush();
                input = new ObjectInputStream(connection.getInputStream());
            }
    
           
    
            @Override
            public void run()
            {
                try
                {
                    setUpStreams();
                    
                }
    
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String args[])
        {
    
            Server s = new Server();
            s.startRunning();
    
        }
    }

Similar Threads

  1. One server, multiple clients question
    By orestis125 in forum New To Java
    Replies: 9
    Last Post: 09-13-2012, 03:34 PM
  2. Have server echo output to multiple clients
    By Wnt2bsleepin in forum New To Java
    Replies: 19
    Last Post: 05-07-2012, 03:26 AM
  3. multiple clients single server via sockets
    By acks in forum Networking
    Replies: 3
    Last Post: 04-12-2011, 08:44 AM
  4. how to connect a server to multiple clients
    By yontan8888 in forum Networking
    Replies: 1
    Last Post: 02-02-2011, 12:42 PM
  5. how to connect a server to multiple clients?
    By azhar in forum Networking
    Replies: 15
    Last Post: 03-22-2010, 01:54 PM

Posting Permissions

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