Hey guys,
Can anyone tell me what I missed in the following code? I've tried everything that came up to my mind, but I can't pass this error.
Let me give you some context. My friend and I are making this program that is supposed to send message through command line. It's a simple computer-to-computer connection through our IPs.
My friend sent me this source code that worked pretty well on his IDE. But I'm having some problem with it still. I'm using NetBeans on Windows Vista.
Here's the source code:
package messengerserver;
import java.util.*;
import java.io.*;
import java.net.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
try
{
ServerSocket ss = new ServerSocket (1234);
Socket s;
PrintWriter p;
Scanner sc;
Thread t = new Thread();
while(true)
{
s = ss.accept();
p = new PrintWriter(s.getOutputStream(),true);
printAll.addWriter(p);
sc = new Scanner(s.getInputStream());
t = new Listener(sc);
t.start();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static class printAll
{
private printAll () {}
private static ArrayList<PrintWriter> p = new ArrayList<PrintWriter>();
//private static ArrayList<PrintWriter> p;
//p = new ArrayList<PrintWriter>();
public static void addWriter(PrintWriter pr)
{
p.add(pr);
}
public static synchronized void printToAll(String text)
{
for(PrintWriter out : p) out.println(text);
}
}
/*public*/ class Listener //implements Runnable//extends Thread
{
private Scanner sc;
private String text;
public Listener(Scanner sc)
{
this.sc = sc;
}
public void run ()
{
while(true)
{
text = sc.nextLine();
printAll.printToAll(text);
}
}
}
}
And here's the line that's giving me some trouble: t = new Listener(sc);
Thank you in advance, all help is welcome.
-CrazyPixel