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.

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

7 Comments

+1 but It is not best option to start a Client inside Server. Consider using 2 main() methods.
so true @Fazovsky..I was suggesting this so that he would understand the concept behind the use of main() function. Cheers
@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.
@Fazovsky: Thank you very much for your input. How would I implement your suggestion?
@user3029329 The solution would be to have two main methods as Faz suggested. here you go: stackoverflow.com/questions/4754058/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.