Managing Multiple connections on a Socket Server?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 7562 Views - Last Post: 17 September 2011 - 08:05 PM Rate Topic: ***-- 2 Votes

#1 CrimsonAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 09

Managing Multiple connections on a Socket Server?

Posted 16 September 2011 - 01:43 PM

OK say i have 10 people connected to my server and i only want to send data to 5 of them, but i want to keep a connection to the other five. How do i specify who receives what data?

edit: im sure this is just something stupid that im missing eh?

This post has been edited by CrimsonAngel: 16 September 2011 - 01:57 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Managing Multiple connections on a Socket Server?

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,171
  • Joined: 05-April 11

Re: Managing Multiple connections on a Socket Server?

Posted 16 September 2011 - 02:21 PM

You have a thread for each of the connections, and they identify themselves with some kind of id?
Was This Post Helpful? 0
  • +
  • -

#3 CrimsonAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 09

Re: Managing Multiple connections on a Socket Server?

Posted 16 September 2011 - 02:27 PM

View PostCasiOo, on 16 September 2011 - 03:21 PM, said:

You have a thread for each of the connections, and they identify themselves with some kind of id?



i do have a thread for each connection and i id them by the ip address right now but i think i know what your getting at... and if im correct i should just filter them before the message sending phase in my run method
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Managing Multiple connections on a Socket Server?

Posted 16 September 2011 - 03:23 PM

View PostCrimsonAngel, on 16 September 2011 - 04:43 PM, said:

OK say i have 10 people connected to my server and i only want to send data to 5 of them,

Based on which criteria ? The first 5 that connected ? The last ones ? The ones is some country ?
Was This Post Helpful? 0
  • +
  • -

#5 CrimsonAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 09

Re: Managing Multiple connections on a Socket Server?

Posted 16 September 2011 - 03:24 PM

View Postpbl, on 16 September 2011 - 04:23 PM, said:

View PostCrimsonAngel, on 16 September 2011 - 04:43 PM, said:

OK say i have 10 people connected to my server and i only want to send data to 5 of them,

Based on which criteria ? The first 5 that connected ? The last ones ? The ones is some country ?



I randomly select ones to send a message too
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Managing Multiple connections on a Socket Server?

Posted 16 September 2011 - 03:36 PM

Have a class Connection that contains each of your client socket, input/output streams,..
Store your connections into an ArrayList
and generate 5 random numbers to pickup this connection
ArrayList<Connection> alSocket;
Random ran = new Random();

void sendTo5(String msg) {
   // build an arrayList of int based on the number of connections I have
   ArrayList<Integer> al = new ArrayList<Integer>();
   // add an entry based on the number of user
   for(int i = 0; i < alSocket.size(); ++i)
      al.add(i);

   // randomly select up to 5 connections
   for(int i = 0; i < 5; i++) {
       if(al.size() == 0) // if less than 5 clients
          return;
       int index = ran.nextInt(al.size());   // select random number
       index = al.remove(index);             // select and remove
       Connection con = alSocket.get(index); // get a random connection
       .... write msg to con.outputStream
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 CrimsonAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 09

Re: Managing Multiple connections on a Socket Server?

Posted 16 September 2011 - 06:56 PM

View Postpbl, on 16 September 2011 - 04:36 PM, said:

Have a class Connection that contains each of your client socket, input/output streams,..
Store your connections into an ArrayList
and generate 5 random numbers to pickup this connection
ArrayList<Connection> alSocket;
Random ran = new Random();

void sendTo5(String msg) {
   // build an arrayList of int based on the number of connections I have
   ArrayList<Integer> al = new ArrayList<Integer>();
   // add an entry based on the number of user
   for(int i = 0; i < alSocket.size(); ++i)
      al.add(i);

   // randomly select up to 5 connections
   for(int i = 0; i < 5; i++) {
       if(al.size() == 0) // if less than 5 clients
          return;
       int index = ran.nextInt(al.size());   // select random number
       index = al.remove(index);             // select and remove
       Connection con = alSocket.get(index); // get a random connection
       .... write msg to con.outputStream
    }
}




This is neat :) though i need to know which addresses im sending data too before i even get to send the first message
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Managing Multiple connections on a Socket Server?

Posted 16 September 2011 - 08:54 PM

Have it part of your Connection object
I mean as in the Connection object instance variables
Was This Post Helpful? 0
  • +
  • -

#9 CrimsonAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 09

Re: Managing Multiple connections on a Socket Server?

Posted 17 September 2011 - 12:49 PM

View Postpbl, on 16 September 2011 - 09:54 PM, said:

Have it part of your Connection object
I mean as in the Connection object instance variables



Thanks for the help :) its a work in progress but im finaly at the point where i think i can start testing it over the network
Was This Post Helpful? 0
  • +
  • -

#10 CrimsonAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 09

Re: Managing Multiple connections on a Socket Server?

Posted 17 September 2011 - 03:28 PM

Ok so i have 10 computers running almost the same code (as seen below) The problem im having is that only the first server is printing any thing out past "server UP, Port ...."

**each server is running a client server program**

The first server will print that it has 9 connections and start my working code (though not sure if thats exactly perfect yet) but the others dont even register that they have received any connections

I also have it so that no todo code will happen tell all servers clients have 9 connections

public Server() throws IOException
    {
    	System.out.println("Starting...");
    	Node = new ServerSocket(serverNumber);
    	System.out.println("Server UP, port " + serverNumber +  " is open");
    	this.start();
    	(new Thread(new Client())).start();
    	
    }
    
    public void run()
    {
    	while(true)
    	{
    		try{
    			Socket client = Node.accept();
    			System.out.println("got connection " + client.getInetAddress());
    		
    			//Connect c = new Connect(client);
    			aSockets[connections] = client;
    			connections++;
    			System.out.println("current connection number: " + connections);
    			
    			if (connections == 9)
    			{ 
///todo code



class Connect extends Thread
    {
    	
    	ArrayList<ObjectInputStream> input;
    	ArrayList<ObjectOutputStream> output;
    	
    	public Connect(){}
    	
    	public Connect(Socket [] clientSocket)
    	{
    		//doesnt send anything if it doesnt have all 10 of the connections
		    	// setting connections to 1 for testing
    		System.out.println("im in server side connections");
	    	
    		try{
    			
    			//open streams
    			for(int i = 0; i < 9; i++)
    			{
    				input.add(new ObjectInputStream(clientSocket[i].getInputStream())) ;
    				output.add( new ObjectOutputStream(clientSocket[i].getOutputStream()));
    			}
    			
    			
    		}catch(Exception e){
    			try{
    				
    			}catch(Exception ew)
    			{
    				System.out.println(ew.getMessage());
    			}
  				return;
    		   }
    		 
	    		this.start();
		    	
	    		
    	}



ArrayList<ObjectOutputStream> output = new ArrayList<ObjectOutputStream>();
    ArrayList<ObjectInputStream> input = new ArrayList<ObjectInputStream>();
    ArrayList <Socket> socket = new ArrayList<Socket>();
 
	 public void Client(){
	 	collector.name = name;
	 	
	 }
	 
		 public void run() 
		 {
		 	 
		      
		      try {
		        // open a socket connection
		        //makes sure it has 9 connections before it continues
		        //if it doesnt have 9 connections it clears and checks again
		        while(socket.size() < 9)
		        {
		        	for(int i = 0,j = 0; i < 10; i++, j++)
			        {
			        	if(i == name)
			        	{
			        		j--;
			        		continue;
			        	}
			        	socket.add(new Socket("net1" + i + ".utdallas.edu", 2000 + i));
			            
			             // open I/O streams for objects
				        output.add(new ObjectOutputStream(socket.get(j).getOutputStream()));
				        input.add(new ObjectInputStream(socket.get(j).getInputStream()));
			        }
			        if(socket.size() < 9)
			        {
			        	socket.clear();
			        	output.clear();
			        	input.clear();
			        }
		        }


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,320
  • Joined: 27-December 08

Re: Managing Multiple connections on a Socket Server?

Posted 17 September 2011 - 03:33 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#12 CrimsonAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 09

Re: Managing Multiple connections on a Socket Server?

Posted 17 September 2011 - 03:36 PM

View Postmacosxnerd101, on 17 September 2011 - 04:33 PM, said:

Duplicate threads merged. Please avoid duplicate posting.


well thanks for the assistance... i guess but i dont think that these are the same questions though i suppose it is about the same project
Was This Post Helpful? 0
  • +
  • -

#13 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Managing Multiple connections on a Socket Server?

Posted 17 September 2011 - 04:24 PM

Are these 10 computers virtual machines? If the constructor isn't successful in the server code, it may be that the socket couldn't bind to the port.
Was This Post Helpful? 0
  • +
  • -

#14 CrimsonAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 09

Re: Managing Multiple connections on a Socket Server?

Posted 17 September 2011 - 04:27 PM

View Postblackcompe, on 17 September 2011 - 05:24 PM, said:

Are these 10 computers virtual machines? If the constructor isn't successful in the server code, it may be that the socket couldn't bind to the port.



No these are 10 separate computers on a single network
Was This Post Helpful? 0
  • +
  • -

#15 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Managing Multiple connections on a Socket Server?

Posted 17 September 2011 - 04:30 PM

I can't see why that would be happening. No exception? It just hangs at the constructor? Are you trying to bind to a port between 0 and 1024?

This post has been edited by blackcompe: 17 September 2011 - 04:32 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3