Nor3g Posted January 24, 2019 Share Posted January 24, 2019 (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: 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 January 24, 2019 by Nor3g Quote Link to comment Share on other sites More sharing options...
Explv Posted January 24, 2019 Share Posted January 24, 2019 Show us your server code as well Quote Link to comment Share on other sites More sharing options...
Nor3g Posted January 24, 2019 Author Share Posted January 24, 2019 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 Quote Link to comment Share on other sites More sharing options...
Explv Posted January 24, 2019 Share Posted January 24, 2019 (edited) 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. Edited January 24, 2019 by Explv Quote Link to comment Share on other sites More sharing options...
Nor3g Posted January 24, 2019 Author Share Posted January 24, 2019 (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: Nevermind, I didn't reload properly. Now I get a different error: Its the same one, very many times. But atleast it's progress, right. Edited January 24, 2019 by Nor3g Quote Link to comment Share on other sites More sharing options...