Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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

  • 14-03-2002 10:31pm
    #1
    Posts: 0


    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