13 Replies - 2281 Views - Last Post: 29 November 2012 - 09:10 AM Rate Topic: -----

#1 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Multithreaded Client/Server Program Using Linked Lists

Posted 26 November 2012 - 05:05 PM

hi attempting to create a multithreaded client/server program of chatters with linked lists, i have my chatter class which is runnable, chatroom server which creates the server an creates the thread instance, i have a chatter list class which has the function of adding chatters to a list and removing them but i cant seem to get my thread into a list, not sure if am making a complete mistake or not could do with a push in the right direction so far it just takes a name in, also repeats anything typed in and also counts how many connections there is but my main goal is to get the chatters connecting to the linked list

chatter class

 

import java.io.*;
import java.net.*;

public class Chatter implements Runnable{
	
	private Socket client;
	private ChatterList counter;
	private String chatter;

	ChatterList chl = new ChatterList();
	
	public Chatter(Socket c, ChatterList chl){
		client = c;
		counter = chl;
	}
	
	public void run(){
		
		BufferedReader input = null;
		PrintWriter output = null;
		
		//next.connect(Chatter.this);
		counter.addConnection();
		
		try{
			
			input = new BufferedReader
					(new InputStreamReader
							(client.getInputStream()));
			
			output = new PrintWriter
					(new OutputStreamWriter
							(client.getOutputStream()));
			
			
			
			output.println("Enter Your Name");
			output.flush();
			
			 chatter = input.readLine();
			
			
			output.println("Hello " + chatter);
			output.flush();
			
			String line;
			boolean finish = false;
			
			while (!finish){
				line = input.readLine();
						
				if ((line == null) || (line.trim().equals("BYE"))){
					
					finish = true;
				}
				
				else if (line.trim().equals("END")){
					
					ChatroomServer.shutDown();
					return;
				}
				
				else if (line.equals("0s")) {
					output.println("Connections:  " +
						counter.getConnections());
					output.println("Currently :  " +
						counter.getCurrentConnections());
					output.flush();
				}
				
				else{
					output.println(chatter + ": " + line);
					output.flush();
			}
				
			
			}
		}
		
		catch(IOException e){
			System.err.println(e.getMessage());	
		}
		
		finally{
			
			counter.endSession();
			
			try{
				
				input.close();	
			}
			
			catch(IOException e){	
			}	
			
			
			if (output != null){
				output.close();
			}
			
			if (client != null){
				
			try{	
				client.close();
			}
			
			catch(IOException e){
			}
			
			}
		}
	}
}

 



server class


 import java.io.*;
import java.net.*;

public class ChatroomServer {
	
	private static final int PORT_NUM = 8189;
		
	private static ServerSocket ss;
		
	private static boolean shutDownCall = false;
		
	public static void shutDown(){
			
		shutDownCall = true;
			
		try{
			ss.close();
		}
		
		catch (Exception e){
			System.err.println("Problem shutting down:");
			System.err.println(e.getMessage());
			System.exit(1);
		}
	}
	
	public static void main(String[] args) {
		
		
		Socket incoming;
		
		Thread t;
		
		ChatterList chl = new ChatterList();
		
		try{
			
			ss = new ServerSocket(PORT_NUM);
			
			while(true){
				
				incoming = ss.accept();
				
				t = new Thread(new Chatter(incoming, chl));
				t.start();
				
			}
		}
		
		catch(SocketException se){
			
			if(! shutDownCall){
				System.err.println("Socket problem:");
				System.err.println(se.getMessage());
				System.exit(1);
			}
		}
	
		catch(IOException ioe){
			System.err.println("I/O problem:");
			System.err.println(ioe.getMessage());
			System.exit(1);
		}
		
		finally{
			
			if (ss != null){
				try{
					ss.close();
				}
				
				catch(Exception e){
					System.err.println("closing:" + e.getMessage());
				}
			}
		}
	}
} 




ChatterList class


public class ChatterList{
	
	private int connections = 0;
	private int currentConnections = 0;


	private ChatterList chl = null;

	 public ChatterList(){
	 }  

	 
	   private void add(Chatter c){
		      theList = new ChatterNode(c, theList);
		   }
	 
	 public void connect(Chatter c){
	      add(c);
	   }
	 
	public int getConnections() {
	  return connections;
	}
	
	public int getCurrentConnections() {
	  return currentConnections;
	}
	
	public void addConnection() {
		connections++;
		currentConnections++;
	}
	
	public void endSession() {
		currentConnections--;
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Multithreaded Client/Server Program Using Linked Lists

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,901
  • Joined: 20-September 08

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 26 November 2012 - 05:38 PM

Why does the class Chatter own ChatterList? Some class closer the entry point of your app should own ChatterList, which would (presumably) own List<Chatter>
Was This Post Helpful? 0
  • +
  • -

#3 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 28 November 2012 - 01:09 PM

literally hit a brick wall to be honest mate the required functionality should be

Quote

chatter will add itself to the chatroom's list of chatters (see method connect(Chatter), the Chatter will therefore need a reference to the ChatterList instance created in the main method of class ChatroomServer);


also worth mentioning

Quote

One solution would be to "merge" the Node and Chatter classes; to be more precise, move the "pointers" to the Chatter class and not use the node class at all. Chatters then have a field that stores the next Chatter in the list; you'll probably need a setTail(Chatter) method.


ive changed my code around since the post but still nothing on the functionality specified above
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 28 November 2012 - 01:12 PM

Can't be a coincidence :)
You have to be in the same classroom
http://www.dreaminco...hatroom-server/
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 28 November 2012 - 01:13 PM

I do not like the design your assignment requests you to make.....
Was This Post Helpful? 0
  • +
  • -

#6 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 28 November 2012 - 01:26 PM

sounds like he on the same problems as me, dont know who he is though course has over 100 people, assignment is annoying i cant move anywhere until i figure out how to fix my problem
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 28 November 2012 - 01:43 PM

View PostCasiOo, on 28 November 2012 - 04:13 PM, said:

I do not like the design your assignment requests you to make.....

me neither but teachers are always right :)
Was This Post Helpful? 0
  • +
  • -

#8 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 28 November 2012 - 02:08 PM

would be more than thankful to any suggestions, head is really battered deadline is next week an i have been stuck on this for few days now, cant really turn anywhere else tutors dont help at all
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,901
  • Joined: 20-September 08

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 28 November 2012 - 02:16 PM

Quote

chatter will add itself to the chatroom's list of chatters (see method connect(Chatter), the Chatter will therefore need a reference to the ChatterList instance created in the main method of class ChatroomServer);
Not brilliant, but doable
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 28 November 2012 - 08:18 PM

but I will vomit tryng to implement that crap
Was This Post Helpful? 0
  • +
  • -

#11 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 29 November 2012 - 04:31 AM

is there any one else who knows more about programming that can help on this post cos i am stuck on pretty much the same problem, it is a hard assignment, and so far not even experienced programmers can implement.

help would be greatly appreciated, as there nowhere else that deals with this problem, may have to start post on new website if no help? let us know how u get on mate?

many thanks for anyone trying to help.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 29 November 2012 - 05:00 AM

View Postdandansmi, on 29 November 2012 - 07:31 AM, said:

is there any one else who knows more about programming that can help on this post cos i am stuck on pretty much the same problem, it is a hard assignment, and so far not even experienced programmers can implement.

This is kind of insulting...
A lot of people here can do your assignment if 5 minutes or even less but you will benefit from nothing if we do so
So back to work son.

Fix your LinkedList problem in a console mode application having nothing to do with your Thread and TCP connections.
When you'll masterized that way of organizing data then incorporate it to your application.

Divide ut reg
as old Julius Caesar said. No reason to debug two different threads at the same time
Was This Post Helpful? 0
  • +
  • -

#13 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 29 November 2012 - 05:39 AM

number 1, its not really insulting is it, when you say you would rather vomit than help that is insulting .
number 2 we are not looking for you to do the assignment we are nearly looking for help on a curtain bit of it as we have specified.
number 3, this is just a tiny bit of the coed we still need to make it thread safe, add cached pools and various other methods.
i have taken wt you said on board and the linked list adds, i have also made the program work with arrays, however this is not what the lecturer wants.

i did not mean to be insulting as i understand you are only trying to help it is just frustrating when you are stuck on a aspect of a program and cant get any further as i am sure you will have had in the past.

thanks
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Client/Server Program Using Linked Lists

Posted 29 November 2012 - 09:10 AM

Start by fixing your Linked List
Linked List is one of the most discussed subject in this forum so yoiu will have hundred of examples

There is no reason to have a ChatterList object both in your main class and one also in the ChatterList class.

A ChatterList should contain a Chatter object, probabl;y named "head" that contains the first Chatter

It is in the Cahtter class that you need a forward pointer to the next Chatter

class Chatter {
    Chatter next;

    Chatter(... {
        next = null;
        ...
    }
}


class ChatterList {
     Chatter head;

     ChatterList() {
         head = null;
     }

     void add(Chatter chatter) {
          if(head == null) {      // first one easy it is the head
              head = chatter;
              return;
          }
          Chatter list = head;       // pass from one to the other
          while(list.next != null)   // until the last one, its next will be null
             list = list.next;
          list.next = chatter;       // append the one received as param
     }

     void broaccast(String msg) {     // send a message to all chatters
         if(head == null)
            return;      

         Chatter list = head;
         do {
           ...send msg to list
           list = list.next;
            } while(list != null);
     }
}



Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1