Java tutorial 笔记~2

The java.net package provides support for the two common network protocols −

TCP − TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP.

UDP − UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications.

Socket Programming − This is the most widely used concept in Networking and it has been explained in very detail.

URL Processing − This would be covered separately. Click here to learn about URL Processing in Java language.

Socket Programming
Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server.

When the connection is made, the server creates a socket object on its end of the communication. The client and the server can now communicate by writing to and reading from the socket.

The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them.

The following steps occur when establishing a TCP connection between two computers using sockets −

The server instantiates a ServerSocket object, denoting which port number communication is to occur on.

The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port.

After the server is waiting, a client instantiates a Socket object, specifying the server name and the port number to connect to.

The constructor of the Socket class attempts to connect the client to the specified server and the port number. If communication is established, the client now has a Socket object capable of communicating with the server.

On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client’s socket.

After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client’s OutputStream is connected to the server’s InputStream, and the client’s InputStream is connected to the server’s OutputStream.

TCP is a two-way communication protocol, hence data can be sent across both streams at the same time. Following are the useful classes providing complete set of methods to implement sockets.

ServerSocket Class Methods
The java.net.ServerSocket class is used by server applications to obtain a port and listen for client requests.

The ServerSocket class has four constructors −

Sr.No. Method & Description
1
public ServerSocket(int port) throws IOException

Attempts to create a server socket bound to the specified port. An exception occurs if the port is already bound by another application.

2
public ServerSocket(int port, int backlog) throws IOException

Similar to the previous constructor, the backlog parameter specifies how many incoming clients to store in a wait queue.

3
public ServerSocket(int port, int backlog, InetAddress address) throws IOException

Similar to the previous constructor, the InetAddress parameter specifies the local IP address to bind to. The InetAddress is used for servers that may have multiple IP addresses, allowing the server to specify which of its IP addresses to accept client requests on.

4
public ServerSocket() throws IOException

Creates an unbound server socket. When using this constructor, use the bind() method when you are ready to bind the server socket.

If the ServerSocket constructor does not throw an exception, it means that your application has successfully bound to the specified port and is ready for client requests.

Following are some of the common methods of the ServerSocket class −

Sr.No. Method & Description
1
public int getLocalPort()

Returns the port that the server socket is listening on. This method is useful if you passed in 0 as the port number in a constructor and let the server find a port for you.

2
public Socket accept() throws IOException

Waits for an incoming client. This method blocks until either a client connects to the server on the specified port or the socket times out, assuming that the time-out value has been set using the setSoTimeout() method. Otherwise, this method blocks indefinitely.

3
public void setSoTimeout(int timeout)

Sets the time-out value for how long the server socket waits for a client during the accept().

4
public void bind(SocketAddress host, int backlog)

Binds the socket to the specified server and port in the SocketAddress object. Use this method if you have instantiated the ServerSocket using the no-argument constructor.

When the ServerSocket invokes accept(), the method does not return until a client connects. After a client does connect, the ServerSocket creates a new Socket on an unspecified port and returns a reference to this new Socket. A TCP connection now exists between the client and the server, and communication can begin.

Socket Class Methods
The java.net.Socket class represents the socket that both the client and the server use to communicate with each other. The client obtains a Socket object by instantiating one, whereas the server obtains a Socket object from the return value of the accept() method.

The Socket class has five constructors that a client uses to connect to a server −

Sr.No. Method & Description
1
public Socket(String host, int port) throws UnknownHostException, IOException.

This method attempts to connect to the specified server at the specified port. If this constructor does not throw an exception, the connection is successful and the client is connected to the server.

2
public Socket(InetAddress host, int port) throws IOException

This method is identical to the previous constructor, except that the host is denoted by an InetAddress object.

3
public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException.

Connects to the specified host and port, creating a socket on the local host at the specified address and port.

4
public Socket(InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException.

This method is identical to the previous constructor, except that the host is denoted by an InetAddress object instead of a String.

5
public Socket()

Creates an unconnected socket. Use the connect() method to connect this socket to a server.

When the Socket constructor returns, it does not simply instantiate a Socket object but it actually attempts to connect to the specified server and port.

Some methods of interest in the Socket class are listed here. Notice that both the client and the server have a Socket object, so these methods can be invoked by both the client and the server.

Sr.No. Method & Description
1
public void connect(SocketAddress host, int timeout) throws IOException

This method connects the socket to the specified host. This method is needed only when you instantiate the Socket using the no-argument constructor.

2
public InetAddress getInetAddress()

This method returns the address of the other computer that this socket is connected to.

3
public int getPort()

Returns the port the socket is bound to on the remote machine.

4
public int getLocalPort()

Returns the port the socket is bound to on the local machine.

5
public SocketAddress getRemoteSocketAddress()

Returns the address of the remote socket.

6
public InputStream getInputStream() throws IOException

Returns the input stream of the socket. The input stream is connected to the output stream of the remote socket.

7
public OutputStream getOutputStream() throws IOException

Returns the output stream of the socket. The output stream is connected to the input stream of the remote socket.

8
public void close() throws IOException

Closes the socket, which makes this Socket object no longer capable of connecting again to any server.

InetAddress Class Methods
This class represents an Internet Protocol (IP) address. Here are following usefull methods which you would need while doing socket programming −

Sr.No. Method & Description
1
static InetAddress getByAddress(byte[] addr)

Returns an InetAddress object given the raw IP address.

2
static InetAddress getByAddress(String host, byte[] addr)

Creates an InetAddress based on the provided host name and IP address.

3
static InetAddress getByName(String host)

Determines the IP address of a host, given the host’s name.

4
String getHostAddress()

Returns the IP address string in textual presentation.

5
String getHostName()

Gets the host name for this IP address.

6
static InetAddress InetAddress getLocalHost()

Returns the local host.

7
String toString()

Converts this IP address to a String.

Socket Client Example
The following GreetingClient is a client program that connects to a server by using a socket and sends a greeting, and then waits for a response.

Example
import java.net.;
import java.io.
;

public class GreetingClient {

public static void main(String [] args) {
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try {
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);

     System.out.println("Just connected to " + client.getRemoteSocketAddress());
     OutputStream outToServer = client.getOutputStream();
     DataOutputStream out = new DataOutputStream(outToServer);
     
     out.writeUTF("Hello from " + client.getLocalSocketAddress());
     InputStream inFromServer = client.getInputStream();
     DataInputStream in = new DataInputStream(inFromServer);
     
     System.out.println("Server says " + in.readUTF());
     client.close();
  } catch (IOException e) {
     e.printStackTrace();
  }

}
}
Socket Server Example
The following GreetingServer program is an example of a server application that uses the Socket class to listen for clients on a port number specified by a command-line argument −

Example
import java.net.;
import java.io.
;

public class GreetingServer extends Thread {
private ServerSocket serverSocket;

public GreetingServer(int port) throws IOException {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}

public void run() {
while(true) {
try {
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + “…”);
Socket server = serverSocket.accept();

        System.out.println("Just connected to " + server.getRemoteSocketAddress());
        DataInputStream in = new DataInputStream(server.getInputStream());
        
        System.out.println(in.readUTF());
        DataOutputStream out = new DataOutputStream(server.getOutputStream());
        out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
           + "\nGoodbye!");
        server.close();
        
     } catch (SocketTimeoutException s) {
        System.out.println("Socket timed out!");
        break;
     } catch (IOException e) {
        e.printStackTrace();
        break;
     }
  }

}

public static void main(String [] args) {
int port = Integer.parseInt(args[0]);
try {
Thread t = new GreetingServer(port);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Compile the client and the server and then start the server as follows −

$ java GreetingServer 6066
Waiting for client on port 6066…
Check the client program as follows −

Output
$ java GreetingClient localhost 6066
Connecting to localhost on port 6066
Just connected to localhost/127.0.0.1:6066
Server says Thank you for connecting to /127.0.0.1:6066
Goodbye!

To send an e-mail using your Java Application is simple enough but to start with you should have JavaMail API and Java Activation Framework (JAF) installed on your machine.

You can download latest version of JavaMail (Version 1.2) from Java’s standard website.

You can download latest version of JAF (Version 1.1.1) from Java’s standard website.

Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add mail.jar and activation.jar files in your CLASSPATH.

Send a Simple E-mail
Here is an example to send a simple e-mail from your machine. It is assumed that your localhost is connected to the Internet and capable enough to send an e-mail.

Example
// File Name SendEmail.java

import java.util.;
import javax.mail.
;
import javax.mail.internet.;
import javax.activation.
;

public class SendEmail {

public static void main(String [] args) {
// Recipient’s email ID needs to be mentioned.
String to = "abcd@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "web@gmail.com";

  // Assuming you are sending email from localhost
  String host = "localhost";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try {
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Now set the actual message
     message.setText("This is actual message");

     // Send message
     Transport.send(message);
     System.out.println("Sent message successfully....");
  } catch (MessagingException mex) {
     mex.printStackTrace();
  }

}
}
Compile and run this program to send a simple e-mail −

Output
$ java SendEmail
Sent message successfully…
If you want to send an e-mail to multiple recipients then the following methods would be used to specify multiple e-mail IDs −

void addRecipients(Message.RecipientType type, Address[] addresses)
throws MessagingException
Here is the description of the parameters −

type − This would be set to TO, CC or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy. Example: Message.RecipientType.TO

addresses − This is an array of e-mail ID. You would need to use InternetAddress() method while specifying email IDs.

Send an HTML E-mail
Here is an example to send an HTML e-mail from your machine. Here it is assumed that your localhost is connected to the Internet and capable enough to send an e-mail.

This example is very similar to the previous one, except here we are using setContent() method to set content whose second argument is “text/html” to specify that the HTML content is included in the message.

Using this example, you can send as big as HTML content you like.

Example
// File Name SendHTMLEmail.java

import java.util.;
import javax.mail.
;
import javax.mail.internet.;
import javax.activation.
;

public class SendHTMLEmail {

public static void main(String [] args) {
// Recipient’s email ID needs to be mentioned.
String to = "abcd@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "web@gmail.com";

  // Assuming you are sending email from localhost
  String host = "localhost";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try {
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Send the actual HTML message, as big as you like
     message.setContent("<h1>This is actual message</h1>", "text/html");

     // Send message
     Transport.send(message);
     System.out.println("Sent message successfully....");
  } catch (MessagingException mex) {
     mex.printStackTrace();
  }

}
}
Compile and run this program to send an HTML e-mail −

Output
$ java SendHTMLEmail
Sent message successfully…
Send Attachment in E-mail
Here is an example to send an e-mail with attachment from your machine. Here it is assumed that your localhost is connected to the internet and capable enough to send an e-mail.

Example
// File Name SendFileEmail.java

import java.util.;
import javax.mail.
;
import javax.mail.internet.;
import javax.activation.
;

public class SendFileEmail {

public static void main(String [] args) {
// Recipient’s email ID needs to be mentioned.
String to = "abcd@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "web@gmail.com";

  // Assuming you are sending email from localhost
  String host = "localhost";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try {
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Create the message part 
     BodyPart messageBodyPart = new MimeBodyPart();

     // Fill the message
     messageBodyPart.setText("This is message body");
     
     // Create a multipar message
     Multipart multipart = new MimeMultipart();

     // Set text message part
     multipart.addBodyPart(messageBodyPart);

     // Part two is attachment
     messageBodyPart = new MimeBodyPart();
     String filename = "file.txt";
     DataSource source = new FileDataSource(filename);
     messageBodyPart.setDataHandler(new DataHandler(source));
     messageBodyPart.setFileName(filename);
     multipart.addBodyPart(messageBodyPart);

     // Send the complete message parts
     message.setContent(multipart );

     // Send message
     Transport.send(message);
     System.out.println("Sent message successfully....");
  } catch (MessagingException mex) {
     mex.printStackTrace();
  }

}
}
Compile and run this program to send an HTML e-mail −

Output
$ java SendFileEmail
Sent message successfully…
User Authentication Part
If it is required to provide user ID and Password to the e-mail server for authentication purpose, then you can set these properties as follows −

props.setProperty(“mail.user”, “myuser”);
props.setProperty(“mail.password”, “mypwd”);
Rest of the e-mail sending mechanism would remain as explained above.

Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.

By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application.

Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program.

Life Cycle of a Thread
A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.

Java Thread
Following are the stages of the life cycle −

New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.

Thread Priorities
Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.

Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).

Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and are very much platform dependent.

Create a Thread by Implementing a Runnable Interface
If your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface. You will need to follow three basic steps −

Step 1
As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. Following is a simple syntax of the run() method −

public void run( )
Step 2
As a second step, you will instantiate a Thread object using the following constructor −

Thread(Runnable threadObj, String threadName);
Where, threadObj is an instance of a class that implements the Runnable interface and threadName is the name given to the new thread.

Step 3
Once a Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. Following is a simple syntax of start() method −

void start();
Example
Here is an example that creates a new thread and starts running it −

Live Demo
class RunnableDemo implements Runnable {
private Thread t;
private String threadName;

RunnableDemo( String name) {
threadName = name;
System.out.println("Creating " + threadName );
}

public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i–) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println(“Thread " + threadName + " interrupted.”);
}
System.out.println(“Thread " + threadName + " exiting.”);
}

public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}

public class TestThread {

public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo( “Thread-1”);
R1.start();

  RunnableDemo R2 = new RunnableDemo( "Thread-2");
  R2.start();

}
}
This will produce the following result −

Output
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Create a Thread by Extending a Thread Class
The second way to create a thread is to create a new class that extends Thread class using the following two simple steps. This approach provides more flexibility in handling multiple threads created using available methods in Thread class.

Step 1
You will need to override run( ) method available in Thread class. This method provides an entry point for the thread and you will put your complete business logic inside this method. Following is a simple syntax of run() method −

public void run( )
Step 2
Once Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. Following is a simple syntax of start() method −

void start( );
Example
Here is the preceding program rewritten to extend the Thread −

Live Demo
class ThreadDemo extends Thread {
private Thread t;
private String threadName;

ThreadDemo( String name) {
threadName = name;
System.out.println("Creating " + threadName );
}

public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i–) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println(“Thread " + threadName + " interrupted.”);
}
System.out.println(“Thread " + threadName + " exiting.”);
}

public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}

public class TestThread {

public static void main(String args[]) {
ThreadDemo T1 = new ThreadDemo( “Thread-1”);
T1.start();

  ThreadDemo T2 = new ThreadDemo( "Thread-2");
  T2.start();

}
}
This will produce the following result −

Output
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Thread Methods
Following is the list of important methods available in the Thread class.

Sr.No. Method & Description
1
public void start()

Starts the thread in a separate path of execution, then invokes the run() method on this Thread object.

2
public void run()

If this Thread object was instantiated using a separate Runnable target, the run() method is invoked on that Runnable object.

3
public final void setName(String name)

Changes the name of the Thread object. There is also a getName() method for retrieving the name.

4
public final void setPriority(int priority)

Sets the priority of this Thread object. The possible values are between 1 and 10.

5
public final void setDaemon(boolean on)

A parameter of true denotes this Thread as a daemon thread.

6
public final void join(long millisec)

The current thread invokes this method on a second thread, causing the current thread to block until the second thread terminates or the specified number of milliseconds passes.

7
public void interrupt()

Interrupts this thread, causing it to continue execution if it was blocked for any reason.

8
public final boolean isAlive()

Returns true if the thread is alive, which is any time after the thread has been started but before it runs to completion.

The previous methods are invoked on a particular Thread object. The following methods in the Thread class are static. Invoking one of the static methods performs the operation on the currently running thread.

Sr.No. Method & Description
1
public static void yield()

Causes the currently running thread to yield to any other threads of the same priority that are waiting to be scheduled.

2
public static void sleep(long millisec)

Causes the currently running thread to block for at least the specified number of milliseconds.

3
public static boolean holdsLock(Object x)

Returns true if the current thread holds the lock on the given Object.

4
public static Thread currentThread()

Returns a reference to the currently running thread, which is the thread that invokes this method.

5
public static void dumpStack()

Prints the stack trace for the currently running thread, which is useful when debugging a multithreaded application.

Example
The following ThreadClassDemo program demonstrates some of these methods of the Thread class. Consider a class DisplayMessage which implements Runnable −

// Create a thread to implement Runnable

public class DisplayMessage implements Runnable {
private String message;

public DisplayMessage(String message) {
this.message = message;
}

public void run() {
while(true) {
System.out.println(message);
}
}
}
Following is another class which extends the Thread class −

// Create a thread to extentd Thread

public class GuessANumber extends Thread {
private int number;
public GuessANumber(int number) {
this.number = number;
}

public void run() {
int counter = 0;
int guess = 0;
do {
guess = (int) (Math.random() * 100 + 1);
System.out.println(this.getName() + " guesses " + guess);
counter++;
} while(guess != number);
System.out.println("** Correct!" + this.getName() + “in” + counter + “guesses.**”);
}
}
Following is the main program, which makes use of the above-defined classes −

public class ThreadClassDemo {

public static void main(String [] args) {
Runnable hello = new DisplayMessage(“Hello”);
Thread thread1 = new Thread(hello);
thread1.setDaemon(true);
thread1.setName(“hello”);
System.out.println(“Starting hello thread…”);
thread1.start();

  Runnable bye = new DisplayMessage("Goodbye");
  Thread thread2 = new Thread(bye);
  thread2.setPriority(Thread.MIN_PRIORITY);
  thread2.setDaemon(true);
  System.out.println("Starting goodbye thread...");
  thread2.start();

  System.out.println("Starting thread3...");
  Thread thread3 = new GuessANumber(27);
  thread3.start();
  try {
     thread3.join();
  } catch (InterruptedException e) {
     System.out.println("Thread interrupted.");
  }
  System.out.println("Starting thread4...");
  Thread thread4 = new GuessANumber(75);
  
  thread4.start();
  System.out.println("main() is ending...");

}
}
This will produce the following result. You can try this example again and again and you will get a different result every time.

Output
Starting hello thread…
Starting goodbye thread…
Hello
Hello

Goodbye
Goodbye