1

I am writing a simple app in Java using JFrame GUI. In GUI I have 2 fields (for ip and the folder) and 1 spinner for port number I want to connect to. I can connect to my server http://147.230.75.240:8080/. But that path is completely empty and I need to go deeper to http://147.230.75.240:8080/sti. But I don't know how to achieve that. As I said there are no folders in the default connection. How can I connect specifically to this part? You can see my code below:

private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Socket socket = null;
        String ip;
        int port;
        String folder;

        ip = ipField.getText();
        port = (int) portField.getValue();
        folder = folderField.getText();

        try {
            File[] files = new File(folder).listFiles();
            System.out.println(files);
            socket = new Socket(InetAddress.getByName(ip), port);
            connectionStatus.setText("Connected");


        } catch (IOException ex) {
            Logger.getLogger(ConnectForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }       

files in this case return null.

user207421
  • 305,947
  • 44
  • 307
  • 483
Arcane
  • 669
  • 1
  • 10
  • 25
  • Are You trying to list the content of a local folder or one served by the HTTP server? –  May 17 '15 at 17:45
  • I am trying to list the content of the server I am connecting to – Arcane May 17 '15 at 17:48
  • `I am trying to list the content of the server I am connecting to` By content, do you mean the webpage content? See https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html – copeg May 17 '15 at 17:49
  • Neither Swing nor NetBeans nor JFrame has anything to do with this question. – user207421 May 17 '15 at 18:31

1 Answers1

2

You might want to take a look at this question.

A Socket does not know what a directory is. You can connect to a server (e.g. http://www.example.com:8080), and that's it. What you do from that point onwards should concern the server itself, and not the socket. Opening a socket just serves the purpose of establishing a connection between the client and server.

This means that if you have a HTTP server, your client should send a request that says "Give me the contents of the server directory" and you should interpret that on the server side, replying with the content of the directory.
The packets will be sent using the Socket that you created.

Finally and just in case you need it, here is a link to a question about setting up your own HTTP Server.

Community
  • 1
  • 1
goncalotomas
  • 1,000
  • 1
  • 12
  • 29