1

Hello good day to all.

I am creating a networking program in java for my Internet Cafe. I have this code for my server

    import java.io.*;
    import java.net.Socket;
    import java.net.ServerSocket;

public class Server
{
    private String name;
    private int id;

    private Socket socket = null;
    private ServerSocket serversocket = null;
    private ObjectInputStream ois = null;

    public Server()
    {
        new Thread(receive).start();

        while(true)
        {
            try
            {
                serversocket = new ServerSocket(4445);
                socket = serversocket.accept();
                System.out.println("Connected..");
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }

    }

    Runnable send = new Runnable(){
        public void run ()
        {

        }

    };

    Runnable receive = new Runnable(){
        public void run ()
        {
            while(true)
            {
                try
                {
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    User user = (User) ois.readObject();
                    System.out.println(user);
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
                catch(ClassNotFoundException e)
                {
                    e.printStackTrace();
                }
            }
        }
    };

    public static void main(String [] args)
    {
        new Server();
    }
}

My problem is that I cant access my instance variable inside my Runnable interface code.

For example the "socket" instance. is not accessible inside this code

Runnable receive = new Runnable(){
        public void run ()
        {
            while(true)
            {
                try
                {
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    User user = (User) ois.readObject();
                    System.out.println(user);
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
                catch(ClassNotFoundException e)
                {
                    e.printStackTrace();
                }
            }
        }
    };

What is the best approach on this and why is not accessible inside that code?

Yves Gonzaga
  • 1,038
  • 1
  • 16
  • 40
  • possible duplicate of [Why are only final variables accessible in anonymous class?](http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class) – m0skit0 Dec 04 '14 at 12:32

3 Answers3

2

Why? Here is the answer: Why are only final variables accessible in anonymous class?

If you need access to your socket from your server you should declare as a final varible your Socket socket on your Server class.

Community
  • 1
  • 1
DavidGSola
  • 697
  • 5
  • 17
1

Try this

public Server() {
    try {
        serversocket = new ServerSocket(4445);
        while (true) {
            socket = serversocket.accept();
            new Thread(receive).start();
            System.out.println("Connected..");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
outdev
  • 5,249
  • 3
  • 21
  • 38
1

If I recall correctly, you need to make the socket field final. The reason escapes me right now.

ben3000
  • 4,629
  • 6
  • 24
  • 43