Results 1 to 3 of 3
- 10-28-2010, 02:25 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
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
- 10-28-2010, 02:30 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
- 10-28-2010, 02:33 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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
-
add object to ArrayList (object is from extends other class)
By mBull in forum Java AppletsReplies: 3Last Post: 03-15-2010, 08:44 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Can we generate an array for class object?
By gaurav2211 in forum Advanced JavaReplies: 4Last Post: 03-21-2009, 04:20 AM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Storing data from text file in ArrayList
By tjhodge in forum New To JavaReplies: 1Last Post: 02-12-2009, 01:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks