• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java program question

Rusky

Limp Gawd
Joined
Jan 4, 2005
Messages
380
Within 1 java program I have 2 classes - MultiThread and Client
When I made the MultiThread class I checked off public static void main(String[] args)

The command "ask user1 ip1 user2 ip2" gets sent to the MultiThread class
When the user types accept which is defined in the Client class I need the Client part to return the word "accept" along with user1 and user2. The MultiThread class knows about them but Client does not.

Pretty much I need to somehow make a variable in Client that I can assign to user1 and user 2 from MultiThread

Does anyone have any ideas?
If you need more code then I will post it.

MultiThread
Code:
else if (command.equals("ask")) {
        		System.out.println("ask command received");
        		
        		String name1 = st.nextToken();
        		String ip1 = st.nextToken();
        		String name2 = st.nextToken();
        		String ip2 = st.nextToken();

        		System.out.println("name1=" + name1 + " " + "ip1=" + ip1 + " " + "name2=" + name2 + " " + "ip2=" + ip2);
        		System.out.println("Do you want to add " + name1 + "(accept/reject)");
        	}
Client
Code:
else if ("accept".equals(input)) {
                System.out.println("accept command received\n");

                Socket Socket = null;
                PrintWriter out = null;
                BufferedReader in = null;

                try {
                    Socket = new Socket(server, port);
                    out = new PrintWriter(Socket.getOutputStream(), true);
                    in = new BufferedReader(new InputStreamReader(Socket.getInputStream()));
                } catch (UnknownHostException e) {
                    System.err.println("Don't know about host: " + server);
                    System.exit(1);
                } catch (IOException e) {
                    System.err.println("Couldn't get I/O for the connection to: " + server);
                    System.exit(1);
                }
                
                out.println("accept");
                
                out.close();
                in.close();
                Socket.close();
            }
 
What do you mean you "checked off" public static.....
My guess is that the OP is using some IDE with a checkbox in the class creation window to add a main() template.

@ the OP:
If you really want to assign these "user" strings to a variable in Client, you can do it by declaring some "String s;" at the top of Client, and setting it from MultiThread with "c.s = user1;", where c is the name of your Client object. Though usually when you want to pass information like this, you'd do it through function arguments / returns.

I think the rest of the code would help a lot... It's not really clear from here what's going on (in particular, how these classes communicate with each other). There are also a couple of things which look a bit questionable, though without any context, it's hard to tell whether there are good reasons behind them.
 
I believe the OP has an assignment to write a multi-threaded system to simply learn about message passing and/or shared memory between threads. There are plenty of good ways to do this in Java.

However, this seems like a homework assignment, so unless the OP can share more information about exactly where he's running into problems, I'm not going to waste my time trying to read his mind.
 
Back
Top