Problem

Hello!

I have a problem with my code.

I am trying to store the data that i read in the run method of SocketHandler in a singleton class.

But when i try to display it in the findMeeting method it is empty.

I cant get it out of the run method by returning it eighter because the runmethod returns void.

What is wrong in my code?

Can i get out the data from the run method in another way?

import java.net.*;

import java.io.*;

import java.util.*;

publicclass Meeting

{

publicstaticvoid main(String a[])throws IOException

{

new Meeting().listen();

new Meeting().checkTimes(a);

new Meeting().findMeeting();

}

publicvoid listen()throws IOException

{

new ServerSocketHandler().start();

}

publicvoid checkTimes(String a[])throws IOException

{

//for(int i = 1; i < Integer.parseInt(a[0]); i++)

//new SocketHandler(i).start();

new SocketHandler(1).start();

}

publicvoid findMeeting()

{

LinkedList<String> times =new LinkedList<String>();

TimeList timeList = TimeList.getInstance();

times = timeList.getTimes();

int timesSize = times.size();

for(int i = 0; i < timesSize; i++)

System.out.println(times.get(i));

}

}

class ServerSocketHandlerextends Thread

{

int port = 4000;

publicvoid run()

{

try

{

int timesSize = 0;

List times =new LinkedList();

ServerSocket serverSocket =new ServerSocket(port);

Socket socket = serverSocket.accept();

PrintWriter pw =new PrintWriter(socket.getOutputStream(),true);

try

{

RandomAccessFile f =new RandomAccessFile("tider.dat","r");

try

{

f.seek(0);

while(true)

{

times.add(f.readUTF());

timesSize++;

}

}

catch(IOException ioe)

{

// System.out.println("\nEnd of file.");

}

finally

{

try

{

f.close();

}

catch(IOException ioe)

{

System.out.println(ioe);

}

}

}

catch(FileNotFoundException fnfe)

{

System.out.println(fnfe);

}

pw.println(timesSize);

for(int i = 0; i < timesSize; i++)

{

pw.println(times.get(i));

pw.flush();

}

pw.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

}

class SocketHandlerextends Thread

{

int port;

LinkedList<String> times =new LinkedList<String>();

SocketHandler(int i)

{

//this.port = 4000 + i;

this.port = 4000;

}

publicvoid run()

{

try

{

Socket socket =new Socket("localhost", this.port);

InputStream is = socket.getInputStream();

BufferedReader br =new BufferedReader(new InputStreamReader(is));

int timesSize = Integer.parseInt(br.readLine());

for(int i = 0; i < timesSize; i++)

times.add(br.readLine());

TimeList timeList = TimeList.getInstance();

timeList.setTimes(times);

br.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

}

class TimeList

{

privatestatic TimeList instance =new TimeList();

private LinkedList<String> times =new LinkedList<String>();

private TimeList(){}

publicstatic TimeList getInstance()

{

return instance;

}

publicvoid setTimes(LinkedList<String> t)

{

this.times = t;

}

public LinkedList<String> getTimes()

{

return this.times;

}

}

[8082 byte] By [mattias_westerberga] at [2007-11-26 23:47:23]
# 1

At a quick glance, it doesn't look like you are ever putting anything into your time list?

Also, in you're main method, should you be creating a single instance of your Meeting class and calling it's methods? This may be the cause of your problem. e.g

Meeting meeting = new Meeting()

meeting.listen();

meeting.checkTimes(a);

meeting.findMeeting();

TimSparqa at 2007-7-11 15:21:45 > top of Java-index,Java Essentials,Java Programming...
# 2

Just to clarify, you understand that in your main method, you are calling the methods of the Meeting class on a new object each time?

This means that If your time list is populated by one of those other methods (listen or checkTimes), it will be empty when you call

new Meeting().findMeeting();

because you are instantiating a new (empty) Meeting object.

I don't know if this is what you were aiming for, but it seemed a bit strange to me.

TimSparqa at 2007-7-11 15:21:45 > top of Java-index,Java Essentials,Java Programming...