Results 1 to 4 of 4
  1. #1
    baltimore is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Client TCP in Java

    Hi, I can get a tcp server to communicate with a client, and thread this to simultaneously communicate with more clients, but i don't know how to get the clients to be able to hear each other, i cant seem to even loop through clients to send to when i want the server to reply

    Does anyone know how to do this, or have a simple tutorial on it?
    Threads really confuse me.

    Thanks.

  2. #2
    Sircedric88 is offline Member
    Join Date
    Jul 2007
    Location
    Las Vegas
    Posts
    14
    Rep Power
    0

    Default

    Sorry, I am a bit new with Java, and I hope you don't mind me asking, but what's TCP?

    May I suggest a linked list? where each client acts as a node?

  3. #3
    mathias is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Please check this code:

    Java Code:
    public class rServerRegistrar {
    
    	public Thread[] cthreads;
    
    	public rServerRegistrar(Thread thr) {
    		cthreads=new Thread[1];
    		cthreads[0]=thr;
    	}
    	
    	public void registerThread(Thread newthread) {
    		Thread[] tempthreads=new Thread[cthreads.length+1];
    		for(int i=0;i<cthreads.length;i++) {
    			tempthreads[i]=cthreads[i];
    		}
    		tempthreads[cthreads.length]=newthread;
    		cthreads=tempthreads;
    	}
    	
    	public void deregisterThread(Thread delthread) {
    		Thread[] tempthreads=new Thread[cthreads.length-1];
    		int count=0;
    		for(int i=0;i<cthreads.length;i++) {
    			if(delthread.getName()!=cthreads[i].getName()) {
    				tempthreads[count]=cthreads[i];
    				count++;
    			}
    		}
    		cthreads=tempthreads;
    	}
    	
    	private void updateThreads() {
    		int count=0; // First count the number of alive threads.
    		for(int i=0;i<cthreads.length;i++) {
    			if(cthreads[i].isAlive()) count++;
    		}
    		Thread[] temp=new Thread[count];
    		count=0;
    		for(int i=0;i<cthreads.length;i++) {
    			if(cthreads[i].isAlive()) {
    				temp[count]=cthreads[i];
    				count++;
    			}
    		}
    	}
    	
    	public Thread[] getClientThreads() {
    		updateThreads(); // Return all except first thread.
    		Thread[] temp=new Thread[cthreads.length-1];
    		for(int i=0;i<temp.length;i++) {
    			temp[i]=cthreads[i+1];
    		}
    		return temp;
    	}
    
    }

  4. #4
    dmacvittie is offline Member
    Join Date
    Aug 2007
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Sircedric88 View Post
    Sorry, I am a bit new with Java, and I hope you don't mind me asking, but what's TCP?

    May I suggest a linked list? where each client acts as a node?
    TCP is not Java Sircedric88, it's Transfer Control Protocol - one of the basic networking protocols your system uses.

    Don.
    Don MacVittie F5 Networks - DevCentral

Similar Threads

  1. a problem about java mail client program
    By lunarstyle in forum AWT / Swing
    Replies: 9
    Last Post: 12-19-2009, 05:14 PM
  2. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM
  3. PPPoE client programming in java
    By yeadam in forum Networking
    Replies: 0
    Last Post: 12-03-2007, 01:14 AM
  4. java client
    By heyhey in forum Advanced Java
    Replies: 0
    Last Post: 10-29-2007, 06:04 PM
  5. Telnet client Java
    By susan in forum Networking
    Replies: 1
    Last Post: 07-30-2007, 10:59 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
  •