What's the best way to clear a Socket?
I have a very basic server-client connection going on my laptops. It works fine but what is a good way to clear the socket once the server application stops?
Code:
import java.net.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class RandomLineServer
{
public static void main(String[] args)
{
int port = 9999;
RandomLineGenerator rlg = new RandomLineGenerator();
try
{
JOptionPane.showMessageDialog(null, "Random Line Server 1.0\nListening On Port "+port);
ServerSocket ss = new ServerSocket(port);
Socket s;
s = ss.accept();
String client;
client = s.getInetAddress().toString();
System.out.println("Connected to " + client);
Scanner in;
in = new Scanner(s.getInputStream());
PrintWriter out;
out = new PrintWriter(s.getOutputStream(), true);
out.println("Welcome to Random Line Server 1.0");
out.println("Enter GET to retrieve a Random Line, or BYE to exit");
while (true)
{
String input = in.nextLine();
if (input.equalsIgnoreCase("bye"))
{
break;
}
else if (input.equalsIgnoreCase("get"))
{
out.println(rlg.getQuoteRandomly());
System.out.println("Serving " +client);
}
else
{
System.out.println("Huh?");
}
}
out.println("Goodbye");
s.close();
System.out.println("Connection "+ client + " ceased");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}