Hi guys, I've hit a problem I'm hoping someone can help me with. I'm trying to create a little irc bot application, just to learn how to use sockets in Java.

Everything works fine so far up to the point where I try to pass the data coming from the socket to a class variable. I can do a System.out.println() within the main while loop which reads data from the socket, and see everything in the console, but when i try to pass that same data to a variable outside of the loop i get nothing.

The same happens if i try to use setters and getters. I can do a System.out.printl() on the data being passed to the setter, but the getter returns nothing.

Does anyone have any idea what could be causing this? I'm completely stumped.

Recommended Answers

All 2 Replies

Hi guys, I've hit a problem I'm hoping someone can help me with. I'm trying to create a little irc bot application, just to learn how to use sockets in Java.

Everything works fine so far up to the point where I try to pass the data coming from the socket to a class variable. I can do a System.out.println() within the main while loop which reads data from the socket, and see everything in the console, but when i try to pass that same data to a variable outside of the loop i get nothing.

The same happens if i try to use setters and getters. I can do a System.out.printl() on the data being passed to the setter, but the getter returns nothing.

Does anyone have any idea what could be causing this? I'm completely stumped.

Can we see some problematic code excerpts?

Sorry, my first post was done quickly before heading out to work. Here is the code for my socket class which runs on its own thread:

class SocketThread implements Runnable{
private Thread runner;
private String server = "irc-server";
private int port = 7000;
private String userName = "username";
private String userPass = "password";
private String servdata = "";

	SocketThread()
	{
		if(runner == null) {
			this.runner = new Thread(this);
			this.runner.start();
		}
	}
	
	public void run()
	{


			try {
				ircConnection();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	}
	
	public void ircConnection() throws IOException
	{

		Socket ircSocket = new Socket(this.server,this.port);
		//creat printstream to send data out
		PrintStream out = new PrintStream(ircSocket.getOutputStream());
		//create BufferedReader to read data in
		BufferedReader in = new BufferedReader(new InputStreamReader(ircSocket.getInputStream()));
		
		//send our details to the server
		out.print("PASS "+this.userPass+"\n\r");
		out.print("NICK "+this.userName+"\n\r");
		out.print("USER "+this.userName+" USING Simple Irc Client\n\r");
		
		//read the data sent from the server
		boolean eof = false;
		
		while(!eof)
		{
			if(!eof){
				String readline = in.readLine();
				setServdata(readline);
			}else{
				eof = true;
			}
		}
	}
	
	public String getServdata() {
		return this.servdata;
	}

	public void setServdata(String servdata) {
		this.servdata = servdata;
		
	}

}

If i do a System.out.println within the setServdata function I can see the server output, but when i try to use the getServdata function I get nothing all. Any help would be greatfully appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.