Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ServerSockets and class that Extends Thread help

Status
Not open for further replies.

tkforever

Programmer
Nov 13, 2003
6
US
So basically, i have a PrintWriter and BufferedReader object for each socket. But when I want to go and get user input from telnet client I am getting a null pointer exception. I pass in the writer and reader objects. But should I be passing in the socket into my methods and then creating these streams using the socket object, and then close those streams when the method is done?

any help or comments:)
thanks
 
Yes, your server code should be along these lines :

Code:
public class PortListener {
	public void listen() {
		try {
			ServerSocket ss = new ServerSocket(5555);
			System.err.println("[PortListener] attached to port:5555");
			while (true) {
				new PortListenerHandler(ss.accept()).start();
			}
		} catch (IOException ioe) {
			ioe.printStackTrace(System.err);
		}
	}
	class PortListenerHandler extends Thread {
		Socket s = null;

		public PortListenerHandler(Socket s) {
			this.s = s;
		}

		public void run() {
                  // get IO streams here ...
                }
        } 
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top