# Simple Thread
```java
package simplethread;
import java.util.Calendar;
public class SimpleThread {
public static void main(String[] args) {
Clock clock = new Clock();
Thread thread = new Thread(clock);
thread.start();
}
}
class Clock implements Runnable {
public void run() {
while (true) {
Calendar rightNow = Calendar.getInstance();
System.out.println(rightNow.get(Calendar.SECOND));
}
}
}
```
# Listener Thread
If you have tried the Socket section, you will probably appreciate the need for a Listener Thread
Here's a threaded client class that will listen to the server class below.
See if you can add a thread to the server class to listen to the client.
## Client
```java
package myclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
*
* @author aballantyne
*/
public class MyClient {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Client client = new Client();
}
}
class Client implements Runnable {
Socket sock;
InputStream in;
OutputStream out;
BufferedReader bin;
PrintWriter pout;
Client() {
try {
//Make sure server is running first
//use ipconfig to find server address
//check the port number on the server program
System.out.println("Attempting to connect to server...");
sock = new Socket("127.0.0.1", 999);
System.out.println("Connected to Server...");
//a lot of code to get buffered input and output streams
in = sock.getInputStream();
out = sock.getOutputStream();
bin = new BufferedReader(new InputStreamReader(in));
pout = new PrintWriter(out, true);
Thread thread = new Thread(this);
thread.start();
Scanner scan = new Scanner(System.in);
String sendMessage;
do
{
sendMessage = scan.nextLine();
pout.println(sendMessage);
}while(!sendMessage.equals("bye"));
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
public void run() {
while (true) {
try {
System.out.println(bin.readLine());
} catch (IOException e) {
System.err.println(e);
}
}
}
}
```
## Server
```java
package myserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
try {
// Note I have used 999 as a port number.
// Make sure the your client does the same.
// Use ipconfig to find the address of the machine the server is running on.
ServerSocket listener = new ServerSocket(999);
System.out.println("Waiting to accept client...");
Socket client = listener.accept();
System.out.println("Client accepted...");
// a lot of code to get buffered input and output streams
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
PrintWriter pout = new PrintWriter(out, true);
// send a message to the client
pout.println("This is the server speaking!");
// read and print messages from the client
String receiveMessage;
do{
receiveMessage = bin.readLine();
System.out.println(receiveMessage);
}while(!receiveMessage.equals("bye"));
} catch (IOException e) {
System.err.println(e);
}
}
}
```