
Questions: 23,720 //
Answers: 52,287 //
Contributing Members: 17,987
This is part of the https://forums.mulesoft.com/questions/105027/example-of-socket-connection-in-mule-4.html, I will closed that one and make the question more specific.
Here is the simple flow I have on Anypoint studio
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:sockets="http://www.mulesoft.org/schema/mule/sockets"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/sockets http://www.mulesoft.org/schema/mule/sockets/current/mule-sockets.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<sockets:listener-config name="Sockets_Listener_config" doc:name="Sockets Listener config" doc:id="86f00bd0-bc23-4a48-ac71-db63f4d534b7" >
<sockets:tcp-listener-connection host="0.0.0.0" port="3301" />
</sockets:listener-config>
<flow name="simplesocketserverflow" doc:id="110f21fb-8f3a-4926-a8f0-48d669b6dfab" >
<sockets:listener doc:name="Listener" doc:id="2b7711c4-5f1a-4423-ac3c-81bfa3ae5fed" config-ref="Sockets_Listener_config" primaryNodeOnly="true">
<repeatable-in-memory-stream />
</sockets:listener>
<logger level="INFO" doc:name="Logger" doc:id="7bf6b0ef-73d2-487c-8ca1-f712c16b231d" message="Should at least give me something? #[payload]"/>
</flow>
</mule>
The flow above treat as a simple socket server, listen for the client and print the message for the client sent.
And here is my client code, which is writing as Java (Run on another machine with firewall open)
package com.itsp.socket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.stream.Collectors;
public class SocketSampleClient {
public static void main(String[] args) {
String hostname = "192.168.2.208";
int port = 3301;
try (Socket socket = new Socket(hostname, port)) {
System.out.println("Connected");
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.write("Hello World!".getBytes());
System.out.println("Success to write!");
DataInputStream in = new DataInputStream(socket.getInputStream());
String line = "";
try (BufferedReader buffer = new BufferedReader(new InputStreamReader( in ))) {
line = buffer.lines().collect(Collectors.joining("\n"));
}
System.out.println(line);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This client is work, because I also write the individual socket server in Java to make sure, here is code for that
package socket;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.stream.Collectors;
public class SocektServer {
// initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
// constructor with port
public SocektServer(int port) {
// starts server and waits for a connection
try {
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String line = "";
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(in))) {
line = buffer.lines().collect(Collectors.joining("\n"));
}
System.out.println(line);
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
} catch (IOException i) {
System.out.println(i);
}
}
public static void main(String args[]) {
SocektServer server = new SocektServer(3301);
}
}
Now the problem is I start the server in mule, that is OK. Run my client code, connection, message is sent, but my mule didn't get any response, the log message is not print. So am I doing something wrong?
Jan 14 at 04:46 PM, zhengye1 answered with:
Already half of week and no one answer and I found that something really weird and I don't understand...
If I want to use mule flow as socket server, and external java client, I need to specify length protocol (because the documentation didn't said what does other protocol do.. )
Also in order to make the scenario I mentioned above work, my client need to tell my flow what is the length you want to write first, then write the data in to that stream
CloudHub connector - Mule 4 2 Answers
how i can send a receive a data from second time using the same socket? 0 Answers
MULE 4 Cookie Store 0 Answers
Example of socket connection in mule 4 2 Answers