0

I'm relatively new to Java and I've been having a recurring problem that's been frustrating me.

I have two class files in the same Project folder: 'Main.java' & 'Client.java'.

'Main.java' is the server ( I run this first). I try to run Client.java to connect to the server. However, it keeps re-launching 'Main.java' regardless of my attempts to fix this. I have tried tried selecting 'Run As' and 'Run Configuration..' but nothing seems to work. This has happened me in several projects and I cannot seem to figure out a solution.

Here is my code:

1: Main.java

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws IOException {
        try {
            final int PORT = 6677;
            ServerSocket server = new ServerSocket(PORT);
            System.out.println("Waiting for clients...");

            while (true) {
                Socket s = server.accept();

                System.out.println("Client connected from "
                        + s.getLocalAddress().getHostName());

                Client chat = new Client(s);
                Thread t = new Thread(chat);
                t.start();
            }
        } catch (Exception e) {
            System.out.println("An error occured.");
            e.printStackTrace();
        }
    }

}

2: Client.java

import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client implements Runnable {

    private Socket socket;

    public Client(Socket s) {
        socket = s;
    }

    @Override
    public void run() {
        try {
            Scanner in = new Scanner(socket.getInputStream());
            PrintWriter out = new PrintWriter(socket.getOutputStream());

            while (true) {
                if (in.hasNext()) {
                    String input = in.nextLine();
                    System.out.println("Client Said: " + input);
                    out.println("You Said: " + input);
                    out.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Any help is greatly appreciated.

Thanks.

Roberto
  • 8,586
  • 3
  • 42
  • 53
mickm
  • 281
  • 2
  • 6
  • 20

1 Answers1

2

That's because the execution of a program (no matter how many classes it has inside) always start with the class containing the "main()" function. As you can see, Main.java is the file holding the main() function and hence execution of this program always starts with that. One of the simplest solutions (not the best) would be to create instances of client in the main function. Hope this helps!

attaboy182
  • 2,039
  • 3
  • 22
  • 28
  • +1 but It is not best option to start a Client inside Server. Consider using 2 main() methods. – FazoM Mar 26 '14 at 20:09
  • so true @Fazovsky..I was suggesting this so that he would understand the concept behind the use of main() function. Cheers – attaboy182 Mar 26 '14 at 20:11
  • @Attaboy182: Thank you very much for your response. This is causing me issues, because it tries to run the server again, when it's already running and gives a "Address already in use" error. This even happens with IBM DeveloperWorks approved source code for another simple server/client program, which is supposed to run out of the box. No matter what I do, it re-launches the server (refusing to launch the client), leaving me unable to connect the two. – mickm Mar 26 '14 at 20:14
  • @Fazovsky: Thank you very much for your input. How would I implement your suggestion? – mickm Mar 26 '14 at 20:15
  • @user3029329 The solution would be to have two main methods as Faz suggested. here you go: http://stackoverflow.com/questions/4754058/multiple-main-methods-in-java – attaboy182 Mar 26 '14 at 20:16
  • Simplest: create main() method in your Client class, (use the same, correct signature). As your existing Client is a Thread - inside that main try something like that: `Client myClient = new Client(6677); myClient.start()` – FazoM Mar 26 '14 at 20:20
  • Also. Your Main class (server?) Is using Client code. Is this correct? – FazoM Mar 26 '14 at 20:23