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?

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Thanks I miss that one
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.