Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By travishein

Thread: What's the best way to clear a Socket?

  1. #1
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default 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?

    Java 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();
            }
        }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    do the close() in a finally block.
    Java 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;
            
            [b]ServerSocket ss = null;
            Socket s = null;[/b]
    
            RandomLineGenerator rlg = new RandomLineGenerator();
            
            try
            {
                JOptionPane.showMessageDialog(null, "Random Line Server 1.0\nListening On Port "+port);
                
                ss = new ServerSocket(port);
                
                            
                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");
                
                System.out.println("Connection "+ client + " ceased");
            }
            
            catch (Exception e)
            {
                e.printStackTrace();
            }
           [b] finally {
              if (s != null) {
                try {
                  s.close();
                } catch (IOException ex) { /* ignore */ }
              }
              if (ss != null) {
                try {
                  ss.close();
                } catch (IOException ex) { /* ignore */ }
              }
            }[/b]
        }
    }
    Fubarable likes this.

  3. #3
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Alright, thanks! So much easier now instead of closing every instance of the stupid thing. I appreciate it Travishein.

Similar Threads

  1. Clear JSP usebean
    By abbeywRCS in forum Advanced Java
    Replies: 1
    Last Post: 05-16-2011, 09:18 PM
  2. Replies: 1
    Last Post: 07-02-2010, 02:18 PM
  3. please clear my doubt
    By syedabu in forum Web Frameworks
    Replies: 1
    Last Post: 02-11-2010, 11:11 AM
  4. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 08:02 PM
  5. Need For Clear Screen
    By adithya4u4ever in forum New To Java
    Replies: 8
    Last Post: 03-08-2009, 09:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •