Results 1 to 3 of 3
  1. #1
    Anoop is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Problem in storing Socket class object in ArrayList array

    :rolleyes: M new to java... So i want to know the problem in my code..
    Im trying to create a public chat server. but facing an error when i compile it.
    Error is:-
    ERROR is:-
    Note: done.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    Code:-

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

    class MyServer
    {
    ServerSocket sskt;
    Socket s;
    ArrayList al=new ArrayList();

    public MyServer()
    {
    try
    {
    sskt=new ServerSocket(10);

    System.out.println("\n\nServer Started");

    while(true)
    {
    s=sskt.accept();
    System.out.println("\n\n\t"+s);
    al.add(s);

    Runnable r=new Mythread(s,al);
    Thread t=new Thread(r);
    t.start();
    }
    }catch(Exception e){System.out.println(e);}

    }//myServer

    public static void main(String s1[])
    {
    new MyServer();
    }

    }//myserver

    class Mythread implements Runnable
    {
    ArrayList al;
    Socket s;



    Mythread(Socket s,ArrayList al)
    {
    this.al=al;
    this.s=s;
    }
    public void run()
    {
    try
    {
    DataInputStream din=new DataInputStream(s.getInputStream());
    String str;
    do
    {
    str=din.readUTF();
    if(!str.equals("stop"))
    telleveryone(str);

    else
    {
    DataOutputStream dout=new DataOutputStream(s.getOutputStream());
    dout.writeUTF(str);
    dout.flush();
    }
    }while(!str.equals("stop"));
    }catch(Exception e)
    {System.out.println(e);}
    }//run

    public void telleveryone(String str)throws IOException
    {
    Iterator i=al.iterator();
    while(i.hasNext())
    {
    try
    {
    Socket s=(Socket)i.next();
    DataOutputStream dout=new DataOutputStream(s.getOutputStream());
    dout.writeUTF(str);
    dout.flush();
    }
    catch(Exception e)
    {System.out.println(e);}
    }//while
    }//telleveryone
    }//mythread



    So please tell me what should I do to solve this error:o

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    That is no error, thats only a note

    Quote Originally Posted by Anoop View Post
    So please tell me what should I do to solve this error:o

    Use generics. Replace each ArrayList with ArrayList<Socket> (or List<Socket) and change Iterator to Iterator<Socket>.

    Hf

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    You're using old fashioned collections, the ones that store and retrieve Objects and nothing else; you should use generic collections, e.g. if you want to store objects of type T in a collection you have to tell the to the compiler: List<Socket>= new ArrayList<Socket>() creates a List that can store objects of type Socket. Read all about it in the 'generics' section(s) of the tutorials.

    kind regards,

    Jos

    edit: darn, too slow again ;-)

Similar Threads

  1. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  2. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  3. Can we generate an array for class object?
    By gaurav2211 in forum Advanced Java
    Replies: 4
    Last Post: 03-21-2009, 04:20 AM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. Storing data from text file in ArrayList
    By tjhodge in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 01:22 PM

Tags for this Thread

Posting Permissions

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