1

After merging the code from two files (client and server) into into one, the control never reaches the client code anymore. However, I need to run server code in background and then run my client code.

Here is my code:

 //Server code 

 try {
        ServerSocket serverSocket = null; 

    try { 
         serverSocket = new ServerSocket(10007); 
        } 
    catch (IOException e) 
        { 
         System.err.println("Could not listen on port: 10007."); 
         System.exit(1); 
        } 

    Socket clientSocket = null; 
    System.out.println ("Waiting for connection.....");

    try { 
         clientSocket = serverSocket.accept(); 
        } 
    catch (IOException e) 
        { 
         System.err.println("Accept failed."); 
         System.exit(1); 
        } 

    System.out.println ("Connection successful");
    System.out.println ("Waiting for input.....");

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), 
                                      true); 
    BufferedReader in = new BufferedReader( 
            new InputStreamReader( clientSocket.getInputStream())); 

    String inputLine; 

    while ((inputLine = in.readLine()) != null) 
        { 
         System.out.println ("Server: " + inputLine); 
         out.println(inputLine); 

         if (inputLine.equals("Bye.")) 
             break; 
        } 

    out.close(); 
    in.close(); 
    clientSocket.close(); 
    serverSocket.close(); 

  //Client code 
        String serverHostname = new String ("127.0.0.1");
        System.out.println ("Attemping to connect to host " +
                serverHostname + " on port 10007.");
        Socket echoSocket = null;
        try {
            echoSocket = new Socket(serverHostname, 10007);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + serverHostname);               
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: " + serverHostname);              
        }

        BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
        String userInput;

        System.out.print ("input: ");           
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                System.out.println("echo: " + in.readLine());
                System.out.print ("input: ");
            }
        out.close();
        try {
            in.close();           
            stdIn.close();           
            echoSocket.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }

I don't understand how to run my server code in background and then run my client code.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160

3 Answers3

0

You need to make 2 classes:

  • One Client
  • One Server

Each one will have it's own main() method.

Like this you can start 2 JVM's one server, one Client

Or in one Class:

Create 2 static inner classes the implement Runnable and you start both from inside your main class: (i take the assumption here that your main class is called Starter)

public static main(String args [ ]) {
                new Thread(new Starter.Server()).start();
                new Thread(new Starter.Client()).start();
            }

I will let you do the cleanup code...

Frank
  • 16,476
  • 7
  • 38
  • 51
  • But i need to run both in one class on one click. Is it Possible? – Sandip Armal Patil Nov 08 '12 at 19:23
  • Yes, but then you will have to spawn a thread, you have 2 options... the old way, then you look at the runnable interface, or the new way then you look at the Concurrency framework. – Frank Nov 08 '12 at 19:37
  • @Frank How is Runnable "old" and the concurrency framework "new"? Runnable, as far as I'm aware, is a part if the concurrency framework – MadProgrammer Nov 08 '12 at 20:10
  • @SandipArmalPatil can you elaborate on why it need to run in "one" class? – MadProgrammer Nov 08 '12 at 20:11
  • Runnable has been around for a while since JDK 1.0... there are other way's now – Frank Nov 08 '12 at 20:14
  • @MadProgrammer: i accept ip and port from user in textfields and when i click on run button it should show me output. Therefore i am trying to run it in one class! – Sandip Armal Patil Nov 09 '12 at 05:07
  • @SandipArmalPatil The problem is the socket comms are blocking, so while you're reading/writing to/from the client, you can't connect to the server. They should be separate concepts/classes. They may need a controller to get the two to work together though – MadProgrammer Nov 09 '12 at 05:32
0

Use a public static class Client nested inside the main Server class. Each of them can have its own main method, so this is a way to achieve your goal of everything in one file, yet two separate entry points.

Another choice would be to have a single entry point but make it start two threads, one for the client and one for the server.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Technically you could do this all in one source file by instantiating a thread that invokes the server code like this

    // Start the Server
    new Thread(new Runnable() {
        public void run() {
            // .. all the server code
        }
    }).start();

    // Start the client

    // .. all the client code

Behind the scenes, Java is probably creating anonymous inner classes for things like the new Runnable() { } technique.

creechy
  • 405
  • 2
  • 3