Results 1 to 4 of 4
  1. #1
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Server socket not stopping correctly?!

    Ok so i have a server client socket application it has a simple gui with a stop and start button i can start the socket fine but when stopping the socket it is not correctly stopped becuase when i try and start it back up using the qui it fails to start again can any one give me any advice on how to correctly stop stop the socket using the code i have at the moment

    GUI
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ServerInterFace extends JFrame implements ActionListener{   
        JButton button,stbutton;
        JLabel serverstatus;
        public EchoServer blahserver;
        private boolean running = false;
        Container c;
    
        public ServerInterFace(){
            super("Login form");
    
            c=getContentPane();
            c.setLayout(new FlowLayout());
    
            //extra classes
            button=new JButton("Start");
            stbutton=new JButton("Stop");
            serverstatus = new JLabel("Offline");
    
            //adding actionlistener to the button
            button.addActionListener(this);
            stbutton.addActionListener(this);
            c.add(serverstatus);
            c.add(button);
            c.add(stbutton);
            //visual
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(200,200);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {  //Note 1
                    public void run() {
                        ServerInterFace window = new ServerInterFace();
                        window.setVisible(true);
                    }
                });
        }
        //must implement method
        //This is triggered whenever the user clicks the login button
        public void actionPerformed(ActionEvent ae)
        {
    
            EchoServer blahserver = new EchoServer();
            //checks if the button clicked
            if(ae.getSource()==button)
            {
                try{
                    if(running==false){
                        blahserver.start();
                        serverstatus.setText("Online");
                        running=true;
                    }else{
                        System.out.println("Server Already Online!"); 
                    }
                }catch(Exception e){
                    System.out.println(e);
                }
            }else if(ae.getSource()==stbutton){
                blahserver.stopserver();
                serverstatus.setText("Offline");
                running=false;
            }
        }//method
    }
    Server Socket
    Java Code:
    import java.net.*; 
    import java.io.*;
    import javax.swing.*;
    
    public class EchoServer extends Thread
    {         
        ServerSocket m_ServerSocket; 
        Mysql db;
        public boolean stopped = false;
        public String returntext = "";
    
        public void run()  
        { 
            startserver();
            return;
        }
    
        public void startserver(){
    
            try  
            { 
                // Create the server socket. 
                m_ServerSocket = new ServerSocket(12111); 
            } 
            catch(IOException ioe) 
            { 
                ioe.printStackTrace(); 
                System.out.println("Could not create server socket at 12111. Quitting."); 
                System.exit(-1); 
            } 
            System.out.println("Listening for clients on 12111"); 
            int id = 0; 
            while(stopped!=true) 
            {                         
                try 
                { 
                    // Accept incoming connections. 
                    Socket clientSocket = m_ServerSocket.accept(); 
    
                    // accept() will block until a client connects to the server. 
                    // If execution reaches this point, then it means that a client 
                    // socket has been accepted. 
    
                    // For each client, we will start a service thread to 
                    // service the client requests. This is to demonstrate a 
                    // multithreaded server, although not required for such a 
                    // trivial application. Starting a thread also lets our 
                    // EchoServer accept multiple connections simultaneously. 
    
                    // Start a service thread 
    
                    ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++); 
                    cliThread.start(); 
                } 
                catch(IOException ioe) 
                { 
                    System.out.println("Exception encountered on accept. Ignoring. Stack Trace :"); 
                    ioe.printStackTrace(); 
                } 
            } 
        }
    
        public void stopserver(){
            stopped = true;
            if(m_ServerSocket!=null){
                try{
                    m_ServerSocket.close();
    
                }catch(IOException e){
                    System.out.println(e);
                }
            }
            //System.exit(0); 
        }
    
        public static void main (String[] args) 
        { 
            new EchoServer();     
        } 
    
        class ClientServiceThread extends Thread 
        { 
            Mysql db=new Mysql();
            Socket m_clientSocket;         
            int m_clientID = -1; 
            boolean m_bRunThread = true;
    
            ClientServiceThread(Socket s, int clientID) 
            { 
                m_clientSocket = s; 
                m_clientID = clientID; 
    
            } 
    
            public void run() 
            {       
                // Obtain the input stream and the output stream for the socket 
                // A good practice is to encapsulate them with a BufferedReader 
                // and a PrintWriter as shown below. 
                BufferedReader in = null;  
                PrintWriter out = null; 
    
                // Print out details of this connection 
                System.out.println("Accepted Client : ID - " + m_clientID + " : Address - " +  
                    m_clientSocket.getInetAddress().getHostName()); 
    
                try 
                {        
                    in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream())); 
                    out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream())); 
                    // At this point, we can read for input and reply with appropriate output. 
                    // Run in a loop until m_bRunThread is set to false 
                    while(m_bRunThread) 
                    { 
                        // read incoming stream 
                        String clientCommand = in.readLine(); 
                        //System.out.println(clientCommand);
                        String[] msg = clientCommand.split(",");
    
                        String temp_cmd1 = msg[0];
                        String[] temp_cmd2 = temp_cmd1.split("=");
                        String cmd = temp_cmd2[1];
    
                        if(cmd.equalsIgnoreCase("quit")) 
                        { 
                            // Special command. Quit this thread 
                            m_bRunThread = false;    
                            System.out.print("Stopping client thread for client : " + m_clientID); 
                        }
                        if(cmd.equalsIgnoreCase("login")){
    
                            String[] temp_uname = msg[1].split("=");
                            String uname = temp_uname[1];
    
                            String[] temp_pass = msg[2].split("=");
                            String pwd = temp_pass[1];
                            //out.println(uname+":"+pass);
                            //out.flush();
                            System.out.println("Client " + m_clientID +" Says :" + cmd+" user:"+uname); 
                            int taskman = db.loginplayer(uname,pwd);
                            if(taskman>0){
                                out.println("cmd=loginsuc,pid="+taskman);
                                out.flush();
                            }else{
                                out.println("cmd=loginfail");
                                out.flush();
                            }
    
                        }
                        if(cmd.equalsIgnoreCase("logout")){
                            System.out.println("Client " + m_clientID +" Says :" + cmd); 
                            String[] temp_pid = msg[1].split("=");
                            int playerid = Integer.parseInt(temp_pid[1]);
                            db.logoutuser(playerid);
                            in.close();
                        }
                        if(cmd.equalsIgnoreCase("getplayerdata")){
                            System.out.println("Client " + m_clientID +" Says :" + cmd); 
    
                            String[] temp_pid = msg[1].split("=");
                            int playerid = Integer.parseInt(temp_pid[1]);
    
                            out.println(db.getplayerdata(playerid));
                            out.flush();
    
                        }
                        if(cmd.equalsIgnoreCase("testconnection")){
                            //System.out.println("Client " + m_clientID +" Says :" + cmd); 
    
                            out.println("connected");
                            out.flush();
    
                        }
                        if(cmd.equalsIgnoreCase("getmapdata")){
                            //System.out.println("Client " + m_clientID +" Says :" + cmd); 
    
                            String[] temp_mapid = msg[1].split("=");
                            int mapid = Integer.parseInt(temp_mapid[1]);
    
                            out.println(db.getmapdata(mapid));
                            out.flush();
    
                        }
                        if(cmd.equalsIgnoreCase("getobjectdata")){
                            System.out.println("Client " + m_clientID +" Says :" + cmd); 
    
                            String[] temp_objid = msg[1].split("=");
                            int objid = Integer.parseInt(temp_objid[1]);
    
                            out.println(db.getobjdata(objid));
                            out.flush();
    
                        }
                        if(cmd.equalsIgnoreCase("onlineplayers")){
                            //System.out.println("Client " + m_clientID +" Says :" + cmd); 
                            String[] temp_pid = msg[1].split("=");
                            int pid = Integer.parseInt(temp_pid[1]);
    
                            String[] temp_mapid = msg[2].split("=");
                            int mapid = Integer.parseInt(temp_mapid[1]);
    
                            String onlineplay = db.getonlineplayer(mapid,pid);
    
                            if(!onlineplay.equals("")){
                                out.println(onlineplay);
                                out.flush();
                            }
                        }
                        if(cmd.equalsIgnoreCase("updatePpos")){
                            //System.out.println("Client " + m_clientID +" Says :" + cmd); 
                            String[] temp_pid = msg[1].split("=");
                            int pid = Integer.parseInt(temp_pid[1]);
    
                            String[] temp_xpos = msg[2].split("=");
                            int xpos = Integer.parseInt(temp_xpos[1]);
    
                            String[] temp_ypos = msg[3].split("=");
                            int ypos = Integer.parseInt(temp_ypos[1]);
    
                            db.updateplayerpos(xpos,ypos,pid);
                        }
                        out.flush();
                    }
                } 
                catch(Exception e) 
                { 
                    //e.printStackTrace(); 
                } 
                finally 
                { 
                    // Clean up 
                    try 
                    {   in.close();
                        out.close();
                        m_clientSocket.close();
                        m_bRunThread=false;
                        System.out.println("Client : ID - " + m_clientID +"...Stopped"); 
                    } 
                    catch(IOException ioe) 
                    { 
                        //ioe.printStackTrace(); 
                    } 
                } 
            }
        } 
    }

  2. #2
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: Server socket not stopping correctly?!

    how can i get that socket to close then?

  3. #3
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: Server socket not stopping correctly?!

    I'm sorry but I've made a mistake and have been trying to delete my post. It's a lot of code to look through but I've realized that clientSocket was passed as a parameter and then closed in the thread.
    Last edited by SerbianSergeant; 10-13-2011 at 07:44 PM.

  4. #4
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Server socket not stopping correctly?!

    you are using 1 thread to listen clients. Then for every client you use 1 thread as well. OK then you are closing the GUI you have to terminate other threads as well.

    You can use windoweventlistener and if you want to close the socket with any user, you have to prepare some protocol.
    Server CLOSING
    Server sends :: Bye Client;
    Client gets :: Bye Client;
    Client sends:: Bye Server;
    Client closes connection
    Server gets:: Bye SERVER;
    Server closes connection.

Similar Threads

  1. non-blocking SSL socket server
    By e_scape in forum Networking
    Replies: 0
    Last Post: 04-12-2011, 06:18 PM
  2. events on a server socket
    By newbiejava in forum New To Java
    Replies: 13
    Last Post: 08-03-2010, 09:24 AM
  3. Replies: 0
    Last Post: 04-24-2010, 09:27 PM
  4. Server Socket
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 09-05-2009, 07:08 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
  •