Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Arraylist Help

I have an array, and thought it would be much better if i changed it to an Arraylist because Arraylists can grow dynamically!

I have tried to convert my array to an arraylist, but it does not seem to work.

i want to have an unlimited ammount of users that can connect possibly. at the minute only five can connect because that is what is declared in the array. i know i could just increase the array, but i want it to add and remove users when they log out.

here is my code, any help would be greatly appreciated.

Thannks.

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

class Connection
{
public Socket s;
public PrintWriter o;
public BufferedReader i;
}

class TCPChatServerThreadTask extends Thread
{
Connection c;

public TCPChatServerThreadTask (ServerSocket serverSocket) throws IOException
{
c = new Connection ();

c.s = serverSocket.accept ();
c.o = new PrintWriter (c.s.getOutputStream (), true);
c.i = new BufferedReader (new InputStreamReader (c.s.getInputStream ()));

System.out.println (c.s.getInetAddress () + ":" + c.s.getPort () + " Connected");

this.start ();
}

public void run ()
{
String fromClient = ">";

try
{
do
{
fromClient = c.i.readLine ();

System.out.println (c.s.getInetAddress () + ":" + c.s.getPort () + "> " + fromClient);

for (int i = 0; i < TCPChatServerThread.taskCount - 1; i ++)
{
TCPChatServerThread.task[i].c.o.println (c.s.getInetAddress () + ":" + c.s.getPort () + "> " + fromClient);
}
}
while (fromClient != "quit");

System.out.println (c.s.getInetAddress () + ":" + c.s.getPort () + " Disconnected");

c.o.close ();
c.i.close ();
c.s.close ();
}
catch (IOException e)
{
}
}
}

public class TCPChatServerThread
{
ServerSocket serverSocket = null;
static public String str = "?";
static public TCPChatServerThreadTask[] task = new TCPChatServerThreadTask[10];
static public int taskCount = 0;

public TCPChatServerThread () throws IOException
{
try
{
serverSocket = new ServerSocket (4455);
}
catch (IOException e)
{
System.err.println ("Server: Could not listen on port: ");
System.exit (1);
}

System.out.println ("Server: Listening on port: ");

while (true)
{
task[taskCount ++] = new TCPChatServerThreadTask (serverSocket);
}
}

public static void main (String[] args) throws IOException
{
TCPChatServerThread e = new TCPChatServerThread ();
}
}
[2873 byte] By [G_MAN] at [2007-11-11 7:37:27]
# 1 Re: Arraylist Help
If you are going to use ArrayList you need to declare your list and use the ArrayList methods. You add by using task.add( xxxx ) [adds at the end of the list]. Use get( index) to get the object stored at the indicated index, use remove( index ) to delete the object at the indicated index from the list.
nspils at 2007-11-11 22:37:54 >
# 2 Re: Arraylist Help
Many thanks for your reply! it was much appreciated!

would it be at all possible for you to show me how to do what you said?

thank you :)
G_MAN at 2007-11-11 22:38:48 >
# 3 Re: Arraylist Help
static public
ArrayList<TCPChatServerThreadTask> task = new ArrayList<TCPChatServerThreadTask>();

//...

task.add(new TCPChatServerThreadTask(serverSocket));

// etc.
destin at 2007-11-11 22:39:47 >
# 4 Re: Arraylist Help
You might re-think which class you want to use - if you are going to treat this like a queue (First In, First Out), where you add to the back and pop off the front, think about using the LinkedList class - it implements the Queue interface, has the benefits of a list - so can grow, etc., without the addressing issues of the ArrayList if you are going to be using the list dynamically ...
nspils at 2007-11-11 22:40:52 >