Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

1st Year Java Problem :( - Easy Im Sure :D

  • 14-03-2002 11:31PM
    #1
    Posts: 171 ✭✭


    Ok,, thanks for reading. :D

    What I want to do is to ?'thread'? and '?synchronise'? the Input and output sections so that i can run them indendently :D I also want to be able to send multiple lines - Does this involve writing to file? How will I begin and end the lines? Like is ther an if statement i can use to catch a double return or sumthing????

    This is only a little bit of an overall project Im doing, so its not like i want u to do my coursework or anything.............well, it is really ;)
    // Server side of 2-person chat program.
    //
    // Server listens for request for connection on port 6789.
    // When connection open, server reads a one-line message
    // sent by client and displays it to the user.
    // After each line from client, server waits for user
    // to type a one-line reply, which it sends it to the client.
    // Connection closed if either user types 'quit'.
    // (19 Feb 2002)
    
    import java.io.*;
    import java.net.*;
    
    public class Chat2Server
    
    {  private static final int PORT = 6789;
          // Port number for connection.
    
       public static void main(String[] args)
       { 
          try
          {  /* Find the domain name (and address) of the host. */
             InetAddress host;
             try
             {  host = InetAddress.getLocalHost();
             }
             catch (UnknownHostException e)
             {  System.out.println
                   ("Host not recognised. (Connected to Internet?)");
                return;
             }
    
             /* Listen on port 6789 for client requesting a connection. */
             ServerSocket listeningSocket = new ServerSocket(PORT);
             System.out.println("Server waiting on host " + host);
             Socket connectionSocket = listeningSocket.accept();
             System.out.println("Connected to client.");
             System.out.println("Wait for client to send message.\n" +
                                "Then send one-line reply.\n" +
                                "Repeat as often as you like.\n" +
                                "Enter 'bye' to finish.");
    
             /* Create input and output streams for the socket. */
             BufferedReader inFromClient =
                new BufferedReader(
                   new InputStreamReader(
                      connectionSocket.getInputStream()));
             BufferedWriter outToClient =
                new BufferedWriter(
                   new OutputStreamWriter(
                      connectionSocket.getOutputStream()));
             BufferedReader keyboard =
                new BufferedReader(new InputStreamReader(System.in));
    
             while(true)
             {  /* Read message from client and display to user. */
                String line1 = inFromClient.readLine();
                System.out.println("Received from client: " + line1);
                if (line1.equalsIgnoreCase("Bye"))
                   break;
    
                /* Read message from user, and send it to client. */
                System.out.print("Send to client: ");
                String line2 = keyboard.readLine();
                outToClient.write(line2 + "\r\n");
                outToClient.flush();
                if (line2.equalsIgnoreCase("bye"))
                   break;
             } // end while
    
             /* Close down. */
             connectionSocket.close();
             listeningSocket.close();
             System.out.println("\nClosing down.");
             return;
          } // end try
    
          catch (IOException e)
          {  System.out.println("Input/output error: " + e);
             return;
          }
       } // end main
    }
    
    
    


Comments

  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes




  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    For a multithreaded server in Java, what I recommend you do is write a listening class, and a handling class. Let me explain.

    The listening class will have the ServerSocket object waiting for the accept event to be triggered.
    // Listening Class
    ServerSocket ss = new ServerSocket(1234);
    
    while(true){
            new HandlingThread(ss.accept()).start();
           
    }
    
    The HandlingThread is a class that extends Thread, and performs independent functions on behalf of a client connection.

    ;-phobos-)


Advertisement