Jump to content

Connecting to a localhost server from within script [SOLVED]


Recommended Posts

Posted (edited)
private void client() {
    try {
        socket = new Socket(address, port);
        log("Connected");
        input = new DataInputStream(System.in);
        output = new DataOutputStream(socket.getOutputStream());
    } catch (IOException u)
    { log(u); }
    String line = "REQUESTING MULE";
    while (!line.equals("Over")){
        try {
            output.writeUTF(line);
            log("Requested mule");
            log ("" + input + "");
        } catch (IOException i)
        { log(i); }
}
        try {
            input.close();
            output.close();
            socket.close();
        }
        catch(IOException i)
        { log(i); }
    }
}

 

I am trying to connect my mule script to a local server on localhost, but I'm getting this error:

image.png.14fb84b2d1d4c27dbf1bd9968e528067.png

 

line 73:
 

output.writeUTF(line);

Line 27 is in onLoop and is just:

client();}

I read somewhere that network connections are blocked. Is that why I'm getting this error or is there something else?

These are the socket and output/input streams:

    private Socket socket            = null;
    private DataInputStream  input   = null;
    private DataOutputStream output     = null;
    private String address = "localhost";
    private int port = 9000;

EDIT: Changed port to 9000 but still the same error.
EDIT2: New error, but now i know they are connected so it's considered solved.

Edited by Nor3g
Posted
1 minute ago, Explv said:

Show us your server code as well

Server:

 

public class MultiThreadedServer implements Runnable{

    protected int          serverPort   = 9000;
    protected ServerSocket serverSocket = null;
    protected boolean      isStopped    = false;
    protected Thread       runningThread= null;

    public MultiThreadedServer(int port){
        this.serverPort = port;
    }

    public void run(){
        synchronized(this){
            this.runningThread = Thread.currentThread();
        }
        openServerSocket();
        while(! isStopped()){
            Socket clientSocket = null;
            try {
                clientSocket = this.serverSocket.accept();
            } catch (IOException e) {
                if(isStopped()) {
                    System.out.println("Server Stopped.1") ;
                    return;
                }
                throw new RuntimeException(
                        "Error accepting client connection", e);
            }
            new Thread(
                    new WorkerRunnable(
                            clientSocket, "Multithreaded Server")
            ).start();
        }
        System.out.println("Server Stopped.2") ;
    }


    private synchronized boolean isStopped() {
        return this.isStopped;
    }

    public synchronized void stop(){
        this.isStopped = true;
        try {
            this.serverSocket.close();
        } catch (IOException e) {
            throw new RuntimeException("Error closing server", e);
        }
    }

    private void openServerSocket() {
        try {
            this.serverSocket = new ServerSocket(this.serverPort);
        } catch (IOException e) {
            throw new RuntimeException("Cannot open port 9000", e);
        }
    }
}

 

Worker Runnable:

 

       import java.io.InputStream;
        import java.io.OutputStream;
        import java.io.IOException;
        import java.net.Socket;

public class WorkerRunnable implements Runnable{

    protected Socket clientSocket = null;
    protected String serverText   = null;

    public WorkerRunnable(Socket clientSocket, String serverText) {
        this.clientSocket = clientSocket;
        this.serverText   = serverText;
    }

    public void run() {
        try {
            InputStream input  = clientSocket.getInputStream();
            OutputStream output = clientSocket.getOutputStream();
            long time = System.currentTimeMillis();
            output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " +
                    this.serverText + " - " + "Hello " +
                    time +
                    " ").getBytes());
            output.close();
            input.close();
            System.out.println("Request processed: " + time);
        } catch (IOException e) {
            //report exception somewhere.
            e.printStackTrace();
        }
    }
}


Start from GUI:

 

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainGUI extends JFrame {
    private JButton startServerButton;
    private JPanel rootPanel;
    private JButton stopServerButton;

    public MainGUI(){

        add(rootPanel);

        setTitle("Server");
        setSize(400, 500);

        startServerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                System.out.println("Starting server...");

                MultiThreadedServer server = new MultiThreadedServer(9000);
                new Thread(server).start();
                System.out.println("Server started");


                try {
                    Thread.sleep(20 * 1000);}
                catch (InterruptedException e) {
                    e.printStackTrace();
                    }
                stopServerButton.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent arg0){
                        System.out.println("Stopping Server");
                        server.stop();
                    }
                })
            ;}
        })
    ;}
}



The server works fine in web broswer, and I can access it from localhost:9000

Posted (edited)
11 minutes ago, Explv said:

So your server is running on port 9000, but your Script is trying to connect to port 8080?

Try changing the port in your Script to 9000

I changed the port but it still shows the exact same error message:
image.png.ee0aa7a59996f70f1a087384b3d2b967.png

 

Nevermind, I didn't reload properly. Now I get a different error:

 

image.png.cd922d357d73f08b0a54a3789fbb21df.png

Its the same one, very many times. But atleast it's progress, right.

Edited by Nor3g

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...