4 Replies - 289 Views - Last Post: 18 October 2012 - 12:26 PM Rate Topic: -----

#1 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 88
  • Joined: 26-April 12

Weird closing method problem

Posted 18 October 2012 - 05:10 AM

i ve been trying to program using socket, trying to send and receive msgs from a client to a server.

everything is working well but when i try to create a method that closes the connection, i can´t close it.

the WEIRD part is, there is a //system.out.println(running); if i put this in the while loop (not as a comment, of course) of the main method, the close method works perfectly.

What should i think about?


package neogame;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NeoGame {

   private static DataInputStream in;
   private static DataOutputStream out;
   private static boolean running = false;
   private static String str;
   private static String resposta;
   private static Reading reading;
   private static Writing writing;
   private static Scanner scanner;
   public static Socket socket;
    
   private static class Reading extends Thread
        {
        
        private static boolean running;
        
        @Override
            public void run()
                {
                    this.running = true;
                    System.out.println("reading ok");
                    while(this.running)
                        {
                            try{
                            resposta = in.readUTF();
                            }catch(Exception e)
                            {
                                System.out.println(e.toString());
                            }

                            if(resposta != null)
                                {
                                System.out.println(resposta);
                                if("/exit /ok".equals(resposta))
                                    {
                                        NeoGame.close();
                                    }
                                }
                        }
                    System.out.println("Reading :"+running);
                }
        
             public void close()
             {
                 running = false;
             }
        }
        
   private static class Writing extends Thread
        {
            private static boolean running;
            public void run()
                {
                    this.running = true;
                    System.out.println("writing ok");
                    while(this.running)
                        {
                        try {
                            System.out.printf("MSG> ");
                            str = scanner.nextLine();
                            out.writeUTF(str);
                            out.flush();
                        } catch (IOException ex) {
                            Logger.getLogger(NeoGame.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                    System.out.println("Writing :"+running);
                }
            
            public void close()
                {
                    running = false;
                }
        }
    public static void close() throws IOException
        {
            System.out.println("Fechando jogo");
            reading.close();
            writing.close();
            running = false;
        } 
  
   public static void main(String[] args) throws UnknownHostException, IOException{
        
        running = true;
        System.out.println("connecting");
        socket = new Socket("187.104.105.249", 6800);
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
            scanner  = new Scanner(System.in);
            if(socket.isConnected())
                {
                    reading = new Reading();
                    reading.start();
                    writing = new Writing();
                    writing.start();
                    System.out.println("Conectado");
                    while(running)
                        {
                            //System.out.println(running);
                        }
                    System.out.println("Desconectado");
                }
             else
                {
                    System.out.println("Não Conectado");
                }
        
     
        
    }
}



This post has been edited by el_pancho: 18 October 2012 - 05:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Weird closing method problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Weird closing method problem

Posted 18 October 2012 - 05:21 AM

First, to be at least a bit OO remove all your static fields and methods
This is not the way it works and you won't be able to run miore than one client at the same time

The only thing that should be static in this whole class is the main() method

using this.xxx for a static variable is also really misleading

Really don't see how the commented/uncommented System.out.println() can change something
the while loop is there
You have a loop to many variables named running here, this is again very confusing
Was This Post Helpful? 1
  • +
  • -

#3 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 88
  • Joined: 26-April 12

Re: Weird closing method problem

Posted 18 October 2012 - 05:42 AM

View Postpbl, on 18 October 2012 - 05:21 AM, said:

First, to be at least a bit OO remove all your static fields and methods
This is not the way it works and you won't be able to run miore than one client at the same time

The only thing that should be static in this whole class is the main() method

using this.xxx for a static variable is also really misleading

Really don't see how the commented/uncommented System.out.println() can change something
the while loop is there
You have a loop to many variables named running here, this is again very confusing


Check now



package neogame;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NeoGame {

    
   private static Reading reading;
   private static Writing writing;
   private static boolean running = false;
   public boolean wRun = false;
   public boolean rRun = false;
    
   private static class Reading extends Thread
        {
        
        private boolean rRun;
        private  DataInputStream in;
        private String resposta;
        private Socket socket;
        
        public Reading(Socket soc)
            {
                socket = soc;
            }
        @Override
        public void run()
                {
                    try {
                        in = new DataInputStream(socket.getInputStream());
                    } catch (IOException ex) {
                        Logger.getLogger(NeoGame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    rRun = true;
                    System.out.println("reading ok");
                    while(rRun)
                        {
                            
                            try{
                            resposta = in.readUTF();
                            }catch(Exception e)
                            {
                                System.out.println(e.toString());
                            }

                            if(resposta != null)
                                {
                                System.out.println(resposta);
                                if("/exit /ok".equals(resposta))
                                    {
                        try {
                           
                            NeoGame.close();
                            
                        } catch (IOException ex) {
                            Logger.getLogger(NeoGame.class.getName()).log(Level.SEVERE, null, ex);
                            System.out.println(ex.toString());
                        }
                                    }
                                }
                        }
                    System.out.println("Reading :"+rRun);
                }
        
        public boolean close()
        {
            rRun = false;
            return true;
        }
        }
        
   private static class Writing extends Thread
        {
          private boolean wRun;
          private String str;
          private  Scanner scanner;
          private DataOutputStream out;
          private Socket socket;
          
          public Writing(Socket soc)
            {
                socket = soc;
          }
          
        @Override
          public void run()
                {
                    try {
                        out = new DataOutputStream(socket.getOutputStream());
                    } catch (IOException ex) {
                        Logger.getLogger(NeoGame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    scanner  = new Scanner(System.in);
                    wRun = true;
                    System.out.println("writing ok");
                    while(wRun)
                        {
                        try {
                            System.out.printf("MSG> ");
                            str = scanner.nextLine();
                            out.writeUTF(str);
                            out.flush();
                        } catch (IOException ex) {
                            Logger.getLogger(NeoGame.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                    System.out.println("Writing :"+wRun);
                }
            
            public boolean close()
                {
                    wRun = false; 
                    return true;
                }
        }
    public static void close() throws IOException
        {
            System.out.println("Fechando jogo");
            reading.close();
            writing.close();
            running = false;
        } 
  
   public static void main(String[] args) throws UnknownHostException, IOException{
        
       
        Socket socket;
        running = true;
        System.out.println("connecting");
        socket = new Socket("187.104.105.249", 6800);
        if(socket.isConnected())
                {
                    reading = new Reading(socket);
                    reading.start();
                    writing = new Writing(socket);
                    writing.start();
                    System.out.println("Conectado");
                    while(running)
                        {
                            
                        }
                    System.out.println("Desconectado");
                }
             else
                {
                    System.out.println("Não Conectado");
                }
        
     
        
    }
}



Was This Post Helpful? 0
  • +
  • -

#4 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 88
  • Joined: 26-April 12

Re: Weird closing method problem

Posted 18 October 2012 - 05:52 AM

Thank you, now its working, i removed the while(running) loop from the main class, because i was using it when the Socket socket was static, now that i am passing the Socket as parameter of the writing and reading constructors, i did not need the while loop of the main anymore, and it is closing perfectly.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Weird closing method problem

Posted 18 October 2012 - 12:26 PM

:^:

still a few useless static variables that shouldn't be there :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1